For Loops
For loops are designed to execute a block of code n times. That means that you can specify a variable, set an initial value to it and mutate this variable after each loop run, until a certain condition is met. For loops can not only work with integer counters.
Usage¶
Here is an example of a simple for
loop:
Optional parentheses
As with the if statement, foreach loop and while loop, the parentheses around the head of the for
loop are optional.
Usage of loop alternatives
For loops should only be used when it is foreseeable how often a block of code will run. If this is not the case, it is recommended to use the while loop or do-while loop instead. If you want to iterate over a container of items, consider using the foreach loop.