Two for loops

Source: Internet
Author: User
I want to explain two for loops. this is an example of using * to output inverted right triangle. I am not very White about these two for loops, especially the one in it. can you explain it? thank you ~
    for($i=1;$i<15;$i++){        for($j=$i;$j<15;$j++){            echo "*";        }       echo "
"; }


Reply to discussion (solution)

Why do we still need a variable $ j which is equal to the variable $ I in the outer layer, repeating it again?

First, you need to understand the meaning of the order sending for loop! If there is no more than one loop, there will be no 3 corners, but 14 vertical *

$ J = $ I; // make the starting value of the inner loop equal to the value of the outer loop // output one less at a time * // The first loop of the outer layer, the inner layer outputs 14 * // the second loop of the outer layer, and the inner layer outputs 13 * // and so on until the end of the loop.

$ J = $ I;
Let me explain these two variables to see if you can understand them.
$ I represents the number of rows, that is, the first loop represents the first row, and so on, a total of fifteen rows.
$ J indicates the number of columns, that is, the number of columns in each row.

After slightly modifying the code, output a rectangle to help you understand it. Change Location: $ j = 1.

 ";    }?> 

Understanding the for loop
In fact, four right triangles can be output.

$ I can be viewed as a row, and $ j can be viewed as a column.
First, let's look at $ I = 1; $ j = 1; j <15, that is, the for in will loop 14 times, and the first line will show 14 *, then, the for loop starts from $ I = 2 ;.
Then, when you see $ I = 2; $ j = 2; j <15, the for in the loop will be 13 times, 13 * appears, and then begin to execute $ I = 3;
............
In a loop, 14 rows in the first line are obtained, 14 rows in total, and the last row is *. I wonder if it is clear.

Multi-Training 9-9 multiplication table output and diamond output

I can understand this. Alas, when I = 1, the second for Loop 1 <15 is it true that the loop is 14 stars when I = 2, and so on.

$ I controls the number of rows executed.
$ J controls the number of * outputs for each row.
It's easy to understand.

Because from the first line, the number of your asterisks will decrease, and you can easily understand the rule by combining the code. The code can also be written in this way.

for($i=1;$i<15;$i++){        for($j=0;$j<15-$i;$j++){             echo "*";        }       echo "
"; }
Related Article

Contact Us

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.