Two nested for loop execution order

Source: Internet
Author: User

1. The outer loop is judged by the loop condition, which satisfies the outer circulation body and executes externally.

2. The inner layer to determine the cycle conditions, to meet the internal circulation body

3. Internal Loop execution

4. The inner loop variable accumulates, returns to 2 executes, until does not satisfy 2

5. The outer loop variable accumulates until the condition is not met and exits the loop completely

Print multiplication table: for (int i=1,i<=9,i++)

{

for (int j=1,j<=i,j++)//j<=i;

{Console.Write ("{0}*{1}={2}", i,j,i*j);

}

Console.WriteLine ();

}

1. First assign the value i = 1, then judge I <=9, satisfy the condition, enter the outer loop body
2. Assign a value j=1, Judge J<=i, (at this point two values are the same), into the inner loop, output 1*1=1
3. Internal Loop J Variable accumulation j=2 does not meet j<=2, execution console. Wriet () outputs a line break symbol
4. External loop variable accumulation i=2, satisfies execution j=1, satisfies, executes j=2, satisfies, executes j=3, does not satisfy, outputs a newline
6, repeat 3~6,i = 3 o'clock, print 3*1=33*2=6,i=4, print 4*1=44*2=84*3=12, when i=9 the last loop, 9*1=9....9*8=72, then accumulate i = 10, do not meet the outer loop, completely exit

Two nested for loop execution order

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.