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.
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); Apple Banana Cherry
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); Sat Sun
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); D-W-S-c-r-i-p-t-
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); Red Green Blue
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); A: 10 B: 20
For standard numeric range loops (e.g. 1 to 10), see Basic Loops.