Nesting of C-language loops

Source: Internet
Author: User

Note: refer to the network resources to prepare, such as the same please forgive me
Nesting of loops:
A loop body statement also contains another loop statement, called a loop nesting.
Nesting considerations:
1. When using loop nesting, the loop control variables for the inner and outer loops cannot be the same.
2. It is best to use the "right indent" format in order to reflect the relationship between loops and nested structures.
3. Try to avoid too many and too deep loop nesting structures
Use:
Looping nesting can help us solve a lot of problems that are often used in C to output data in a row-and-column manner, for example:

////////////////  //99 multiplication Table///////////////#include <stdio.h>#defineROWS 9intMain () {intI, J;  for(i =1; I <= ROWS; ++i)//Outer loop control output line Count    {           for(j =1; J <= I; ++J)//internal loop control output column Count{printf ("%d", I * j);//Output Product} printf ("\ n");//line Break    }      return 0; }  

Program Description:
In this example, the outer loop loops 9 times (that is, it outputs nine rows), and when I equals 10 o'clock the loop terminates. Each cycle of the outer loop performs an inner loop, and the number of cycles within each cycle of the outer loop is different. Because each cycle of the outer loop will cause I to increase by 1, and the value of J will also be re-assigned to 1, and the end condition of the inner loop is J <= I, and the inner cycle of each cycle of J only increased by 1, so the outer cycle of each cycle, the number of cycles in the loop is increased once: the first cycle of the outer loop, the number of cycles in the loop is 1; the second round of the outer loop, the number of cycles in the loop is 2, the third round of the outer loop, the number of cycles in the loop is 3 ... And so on The following is a partial simulation of the running process of this example.
1. Outer circulation first round cycle
The value of I is 1 (hereinafter abbreviated as I = 1), so I <= ROWS is set up to enter the loop body:
1). The first round cycle of the inner loop
j = 1, therefore J <= I is established, into the loop body:
Outputs the product of I * j (that is, 1 * 1) and a space, or 1.
++j-J = 2,j <= i is not set, the inner loop ends.
Output
Line break

++i-i = 2, so I <= ROWS is set up to start the second round cycle.

2. Outer circulation second round cycle
1). The first round cycle of the inner loop
j = 1,j <= I was established to enter the loop body:
Output I * J (2 * 1) and a space, that is, 2.
++j-J = 2,j <= i is still set up to start the second round cycle.
2). The second round cycle of the inner loop
Output I * J (2 * 2) and a space, that is, 4.
++j-J = 3,j <= i is not set, the inner loop ends.
Output
Line break.
++i-i = 3,i <= ROWS is set up to start the third round of loops.
At this point, the output is:
1
2 4
The third cycle of the outer loop to the nineth round of circulation withheld, please simulate it yourself.

9. Outer circulation Nineth Round cycle
/* ... ... */
++i-i = 10,i <= ROWS not set, outer loop ends.

The resulting output is:
1
2 4
3 6 9
4 8 12 16
5 10 15) 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
8 16 24 32 40 48 56 64
9 18 27 36 45 54 63 72 81

Nesting of C-language loops

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.