Foreach Loops
Foreach loops can be used to iterate over an array of arbitrary elements.
Usage¶
You can write foreach
loops in two different ways:
Normal¶
If you only need the array item to work with, the recommended way is to use the normal 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.
Indexed¶
As soon as you need to access the index of the array item as well, you can use the indexed
string[3] welcomeMessage = { "Hello", "Spice", "programmers!" };
foreach int i, string word : welcomeMessage {
printf("Word no. %d: %s\n", i, word);
}
Skipping array items
Indexed foreach
loops allow you to set the start index and to modify the index within the loop. Here's an example:
int[10] intArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
foreach int i = 3, int item : intArray {
printf("Array item no. %d: %d, ", i, item);
i += 2;
}
Array item no. 3: 4, Array item no. 6: 7, Array item no. 9: 10,
. In the loop head, we set the start index to 3
and skip 3 items (2+1) each round. Usage of loop alternatives
Foreach loops should only be used when you have an array data structure and want to access its items. If this is not the case, we recommend using the for loop or while loop instead.