A for loop has this format:
for (initialization; condition; change of condition) {
statement;
statement;
...
}
The brackets after for statement must contain three parts, separated by semicolons. It’s very important to understand the
relationship between these parts:
- The initialization sets up the initial value of a looping variable, for example $i = 0 or $i = $v+1. This expression is run only once before the loop starts.
- The condition is checked at the start of each iteration of the loop. If it’s true, the block executes. If it’s false, the
loop ends and execution continues after the loop. For example $i < 5 or $i < size($words).
- The change of condition is run at the end of each iteration of the loop. This expression should make some change that gets
each iteration closer to the end goal of the loop. For example $i++ or $i += 5.
A for loop evaluates the termination condition before executing each statement. The condition compares variable, attribute, or constant values.
int $i;
for ($i = 10; $i > 0; $i--) {
print($i+"...\n");
}
print("Blastoff!!!");
Except where otherwise noted, this work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License