# 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.
```pascal
var fruits := ['Apple', 'Banana', 'Cherry'];
for var fruit in fruits do
PrintLn(fruit);
// OUTPUT
// 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.
```pascal
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);
// OUTPUT
// Sat
// Sun
```
## Strings
Iterating over a string returns each **full Unicode codepoint**. Note that the loop variable `c` is of type **String** (containing the full character, which may be a surrogate pair), not `Char`.
```pascal
var msg := 'Ready 🚀';
for var c in msg do
Print(c + '|');
// OUTPUT
// R|e|a|d|y| |🚀|
```
This correctly handles multi-byte characters like emojis, treating them as a single logical unit.
## Ranges
You can iterate over numeric ranges directly using the bracketed range syntax.
```pascal
for var i in [1..5] do
Print(i.ToString + ' ');
// OUTPUT
// 1 2 3 4 5
```
## Type Enumerations
You can iterate over an entire enumeration type to visit all possible values.
```pascal
type TColor = (Red, Green, Blue);
for var c in TColor do
PrintLn(c.Name);
// OUTPUT
// Red
// Green
// Blue
```
## Associative Arrays (Dictionaries)
Direct iteration over an associative array is **not supported**. This is an intentional design choice to avoid ambiguity; iterating directly would yield key-value tuples, whereas most users only need the keys or the values. Forcing the use of `.Keys` makes the code's intent explicit.
```pascal
var data: array [String] of Integer;
data['A'] := 10;
data['B'] := 20;
// Explicitly iterate over keys
var keys := data.Keys;
keys.Sort;
for var k in keys do
PrintLn(k + ': ' + data[k].ToString);
// OUTPUT
// A: 10
// B: 20
```
:::info
For standard numeric range loops (e.g. `1 to 10`), see **[Basic Loops](/lang/control_flow/loops)**.
:::
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'];forvar 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:setof TDays :=[Sun, Sat];forvar day in weekend do
PrintLn(day.Name);
Result
Sat
Sun
Strings
Iterating over a string returns each full Unicode codepoint. Note that the loop variable c is of type String (containing the full character, which may be a surrogate pair), not Char.
var msg :='Ready 🚀';forvar c in msg do
Print(c +'|');
Result
R|e|a|d|y| |🚀|
This correctly handles multi-byte characters like emojis, treating them as a single logical unit.
Ranges
You can iterate over numeric ranges directly using the bracketed range syntax.
forvar i in[1..5]do
Print(i.ToString +' ');
Result
1 2 3 4 5
Type Enumerations
You can iterate over an entire enumeration type to visit all possible values.
type TColor =(Red, Green, Blue);forvar c in TColor do
PrintLn(c.Name);
Result
Red
Green
Blue
Associative Arrays (Dictionaries)
Direct iteration over an associative array is not supported. This is an intentional design choice to avoid ambiguity; iterating directly would yield key-value tuples, whereas most users only need the keys or the values. Forcing the use of .Keys makes the code's intent explicit.
var data:array[String]ofInteger;
data['A']:=10;
data['B']:=20;// Explicitly iterate over keysvar keys := data.Keys;
keys.Sort;forvar 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.