For loop nesting, for nesting

Source: Internet
Author: User

For loop nesting, for nesting

After a week of Study on c #, I found that many students are a little dizzy in the loop. In fact, as long as the steps and conditions of execution are analyzed step by step, the idea is clear and I naturally understand it;

In addition, in many cases, the for loop is nested in the for loop. If the idea is unclear, the for Loop will certainly become awesome;

Below I will use a few examples to talk about the for loop and loop nesting, hoping to help you;

1. Five columns are output;

Let's analyze the next loop process:

1. when I = 0, execute the inner loop j = 0, output a * number, and then j ++. At this time, j = 1, output the second * number, then j ++ ;......; when j = 4, the fifth * number is output, and then j ++. At this time, j = 5 does not meet j <5, ends the inner loop. Then, the following line feed code is executed, then execute the outer I ++;

2. at this time I = 1, execute the inner loop again ..................................... when j = 5, the system jumps out of the inner loop, performs the line feed operation, and then executes I ++;

.......

4. when I = 4, it is obvious that the inner layer loops are executed for a total of 5 times. Each time you enter 5 * numbers, the line breaks and a total of five asterisks are entered. Execute I ++;

5. When I = 5, if the I <5 condition is not met, the outer loop will jump out. The result displayed on the console is the * Number of the Five columns of the five elements;

From the simple example above, it is not difficult to see that as long as you analyze the cycle process and when to jump out of the cycle, it is easy to clarify your own ideas, it won't be overwhelmed by loop nesting.

② Right triangle in the lower left corner

From the perspective of this column, if the output is still five rows, the outer loop will still have to be iterated five times. The difference is that the inner loop is 1, 2, 3, 4, 5, here we can understand that the number of inner loops is changing. Since the number of loops is changing, it must be the condition for controlling the number of loops, we can use the variable "outer I" to control the changes in the inner loop, which is often used in nested loops.

1. when I = 0, the inner layer is j = 0; j <0 + 1; j ++; obviously, after an inner loop is executed, j ++ is executed, only when j = 1 does not meet j <1; naturally, the inner loop exists;

2 when I ++ and I = 1, the inner layer is j = 0; j <1 + 1; j ++; the inner layer is cyclically executed twice.

...

In this way, when I = 4, that is, the fifth line, the inner loop is j = 0; j <4 + 1; j ++; the inner layer outputs five * numbers;

③ Output an isosceles triangle

Although two for loops are nested here, it seems very complicated for beginners. In fact, it is still easy to understand as long as you clarify your ideas.

In the first row, enter four spaces and then one space *;

It is not hard to see that the space pattern is 4, 3, 2, 1, and loops four times;

The asterisks are an equal-difference sequence of, and 9;

As mentioned in example 2, to control the number of for loops each time, you only need to change the loop condition,

1. therefore, the cyclic condition for entering spaces is j <4-i. As I increments, j <4, j <3 ..... j <0; previously defined j = 0, which naturally exists and terminates this for loop.

2. let's take a look at the input Asterisk. We only need to define the condition as k <2i + 1; a simple arithmetic difference series, k <1, and execute a loop; k <3 executes 3 cycles ;....... in the end, k <2*4 + 1 executes 9 cycles. Then the isosceles triangle is output.

Conclusion: 1 through the analysis of these examples, we can illustrate whether loops or nested loops are good. In fact, when the cycle conditions are met, we can execute the loop body, terminate the loop body when the cycle conditions are not met;

2. the number of inner loops is usually controlled by the loop variable of the outer loop. Therefore, the number of inner loops must change, the method we often use is j <(a simple calculation method written with the outer variable I)

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.