| Contents | Up | Previous | Next |
The for loop has another syntax which can be used to encapsulate while-like loops in a more compact way, which also has some advantages.
The syntax for this for loop is for({Initial Statements} ; {Condition} ; {Iteration Commands}) { ... }, where {Condition} is a boolean condition expression, and {Initial Statements} and {Iteration Commands} may contain one or more commands separated by commas.
When The interpreter encounters a for loop it executes its initial statements, and then executes the commands within its block for as long as the condition is met. {Iteration Commands} are executed at the end of each iteration.
The following simple example prints the mulitplication board:
for($row = 1 ; $row <= 10 ; $row++)
{
for($column = 1 ; $column <= 10 ; $column++)
{
$result = $row*$column;
$pad = ( " " x (4-length($result)) );
print $pad, $result;
}
print "\n";
}
|
| Contents | Up | Previous | Next |