The For loop is one of the most recent looping statements, and in any language, there is this looping statement, which is also a common loop method in our work.
Syntax rules:
for (EXPR1; expr2; expr3) {
The code to execute
}
Expr1: Indicates where the loop begins
EXPR2: The condition of the loop, if the value is TRUE, the loop continues, and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
EXPR3: evaluated (and executed) after each loop.
Writing a bit obscure, we write the simplest for loop demo!
For loop Demo1:
<?phpfor ($n =1; $n <20; $n + +) { echo ' for Loop statement ' executes ' $n. ' Times <br> ";}
Execution Result:
The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time
The FOR Loop statement executes the 10th time
The FOR Loop statement executes the 11th time
The FOR Loop statement executes the 12th time
The FOR Loop statement executes the 13th time
The FOR Loop statement executes the 14th time
The FOR Loop statement executes the 15th time
The FOR Loop statement executes the 16th time
The FOR Loop statement executes the 17th time
The FOR Loop statement executes the 18th time
The FOR Loop statement executes the 19th time
It can be seen that when the $n<20 condition is not satisfied, it does not output $n.
For Loop statement Demo2, use break to jump out for the For loop:
<?phpfor ($n =1; $n <20; $n + +) { if ($n ==10) {break ; } The echo ' For Loop statement executes the '. $n. ' Times <br> ";}
Output Result:
The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time
When n equals 10, jumps out of the loop and does not continue. If we just want to jump out of 10, the rest of the execution, can be written like this:
<?phpfor ($n =1; $n <20; $n + +) { if ($n ==10) { continue; } The echo ' For Loop statement executes the '. $n. ' Times <br> ";}
So we just jump out of a loop and the results are as follows:
The FOR Loop statement executes the 1th time
The FOR Loop statement executes the 2nd time
The FOR Loop statement executes the 3rd time
The FOR Loop statement executes the 4th time
The FOR Loop statement executes the 5th time
The FOR Loop statement executes the 6th time
The FOR Loop statement executes the 7th time
The FOR Loop statement executes the 8th time
The FOR Loop statement executes the 9th time
The FOR Loop statement executes the 11th time
The FOR Loop statement executes the 12th time
The FOR Loop statement executes the 13th time
The FOR Loop statement executes the 14th time
The FOR Loop statement executes the 15th time
The FOR Loop statement executes the 16th time
The FOR Loop statement executes the 17th time
The FOR Loop statement executes the 18th time
The FOR Loop statement executes the 19th time
I blog: PHP for loop Writing and example
PHP for loop notation and examples