Talk about the sixth PHP bullet ---- use the for loop to output the 9-9 multiplication table

Source: Internet
Author: User

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:
<? Php
For (\$ I = 1; \$ I <= 9; \$ I ++ ){
For (\$ j = 1; \$ j <= \$ I; \$ j ++ ){
Echo "\$ j x \$ I =". \$ j * \$ I ."";
}
Echo "<br> ";
}
?>
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 "<br/>" 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 "<br> ";

\$ 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 "<br/>"

\$ 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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.