For..in Iteration

The for..in loop is the modern way to iterate over collections in DWScript. It is more readable than standard range loops and automatically handles the underlying iteration logic.

Arrays

Iterating over a dynamic or static array returns each element in order.

var fruits := ['Apple', 'Banana', 'Cherry'];

for var fruit in fruits do
  PrintLn(fruit);
Result
Apple
Banana
Cherry

Sets

When iterating over a set, elements are returned in their ordinal order (the order defined in the enumeration), regardless of the order they were added to the set.

type TDays = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
var weekend: set of TDays := [Sun, Sat];

for var day in weekend do
  PrintLn(day.Name);
Result
Sat
Sun

Strings

Iterating over a string returns each character as a single-character string.

var msg := 'DWScript';
var result := '';

for var c in msg do
  result := result + c + '-';

PrintLn(result);
Result
D-W-S-c-r-i-p-t-

Type Enumerations

You can iterate over an entire enumeration type to visit all possible values.

type TColor = (Red, Green, Blue);

for var c in TColor do
  PrintLn(c.Name);
Result
Red
Green
Blue

Associative Arrays (Dictionaries)

Direct iteration over an associative array is not supported. You must iterate over the keys using the .Keys property.

var data: array [String] of Integer;
data['A'] := 10;
data['B'] := 20;

// Get keys, sort them, then iterate
var keys := data.Keys;
keys.Sort;

for var k in keys do
  PrintLn(k + ': ' + data[k].ToString);
Result
A: 10
B: 20

For standard numeric range loops (e.g. 1 to 10), see Basic Loops.

On this page