A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
for (expression1; expression2; expression3)
statement
expression1 initializes a loop count variable once at start of loop (e.g., i = 0). expression2 is the test condition – the loop will continue while this is true (e.g., i <= 10). expression3 is executed at the end of each iteration – usually to modify the loop count variable (e.g., i++).
Example
The expected output for this for loop is:
Loop iteration 0
Loop iteration 1
Loop iteration 2
Loop iteration 3
Loop iteration 4
It is important to mention that any or all of the three expressions in a for loop may be left blank (semi-colons must remain). If expression1 or expression3 are missing, their actions simply disappear. If expression2 is missing, it is assumed to always be true.