Skip to content

Foreach Loops

Foreach loops can be used to iterate over containers, that offer iterator implementations like Vector, ArrayList, etc. as well as arrays of arbitrary elements and number ranges.


You can write foreach loops in two different ways:


If you only need the item to work with, the recommended way is to use the ordinary foreach loop:

int[7] myIntArray = [ 1, 5, 4, 0, 12, 12345, 9 ];
foreach (const int item : myIntArray) {
    printf("Item: %d", item);

Optional parentheses

As with the if statement, for loop and while loop, the parentheses around the head of the foreach loop are optional.


As soon as you need to access the index of the current item as well, you can use the indexed foreach loop:

dyn welcomeMessage = Vector<string>();
foreach long idx, string word : welcomeMessage {
    printf("Word no. %d: %s\n", idx, word);

Usage of loop alternatives

Foreach loops should only be used when you have a container data structure and want to iterate over its items. If this is not the case, we recommend using the for loop, while loop or do-while loop instead.