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.
Usage¶
You can write foreach
loops in two different ways:
Ordinary¶
If you only need the item to work with, the recommended way is to use the ordinary foreach
loop:
Spice | |
---|---|
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 current item as well, you can use the indexed foreach
loop:
Spice | |
---|---|
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.