Last renewed! These days I found that some children's shoes still have some problems in outputting the multiplication table loop. today I will give you a detailed explanation... SyntaxHighlighter.
Last renewed!
These days I found that some children's shoes still have some problems in outputting the multiplication table. today I will give you a detailed explanation...
First, let's look at the code:
For ($ I = 1; $ I <= 9; $ I ++ ){
For ($ j = 1; $ j <= $ I; $ j ++ ){
Echo "$ j x $ I =". $ j * $ I ."";
}
Echo"
";
}
?>
Output result:
Well, let's further analyze:
We call the outermost loop a "trip ":
First trip:
$ I is 1, meeting $ I <= 9,
While the inner loop $ j satisfies $ j <= $ I and outputs 1x1 = 1.
$ J auto-Increment 1, no longer meet $ j <= $ I, no output
Output"
"Line feed,
$ I Auto-Increment 2
This trip ends.
Second trip:
If $ I is 2 and $ I <= 9 is met, the second loop is executed.
The condition of the inner loop becomes:
For ($ j = 1; $ j <= 2; $ j ++ ){
........
}
The inner layer loops output twice, respectively:
1x2 = 2 and 2x2 = 4
Similarly, when $ j in the inner loop is added to 3, the inner loop is no longer executed.
Output"
";
$ I Auto-increment is 3
This trip is over!
Third trip:
Same as above .....
And so on:
Until the ninth:
$ I has been added as 9
$ I <= 9 is still valid
Execute the inner loop of the ninth round:
At this time, the inner loop changes
For ($ j = 1; $ j <= 9; $ j ++ ){
...........
}
The code in the loop body is executed cyclically 9 times,
Are
1x9 = 1 2x9 = 18 ...... 9 = 81
That is, the last line of the result.
At this time, $ j no longer meets $ j <= 9, and the execution of the inner loop ends.
Output"
"
$ I Auto-increment is 10
This trip ends.
By the time of the tenth round, $ I is 10 and no longer meets $ I <= 9. at this point, this round robin will not be executed.
The displayed 9-9 multiplication table is output.
I don't know if you understand it. if you understand it, try to output the 9-9 multiplication table in turn! The principle is the same!
Author: zdrjlamp