Today, I learned mainly about cyclic structures.
cyclic structure 1-while cycle
The format is as follows:
1 while (condition) 2 {3 statement 1; 4 statement 2; 5 .... 6 }
1) If the condition is true, the statement in the loop body is executed (the "loop Body" is the contents of the curly brace {} after the while). Then judge the condition again and repeat the process until the condition is not set to end the while loop
2) The characteristics of the while loop: if the condition in the while is not in the beginning, then the statement in the loop body will never be executed
For example:
1 /*2 output 10 times Hello World3 */4 intMain () {5 6 inti =0;7 8 while(I <Ten) {9 Tenprintf"Hello World!----%d\n", i); Onei++; A - } - the}
If the i++,count of the 11th line is always I, then i<10 is always set up, and the while loop will fall into the "dead loop", repeating the 10th line of code.
Cyclic structure 2-do while loop
The format is as follows:
1 Do {2 statement 1; 3 statement 2; 4 ... 5 while (conditions);
1) Note the 5th line, followed by a semicolon;
2) When executing to the Do-while loop, the statement in the loop body is executed first (the "loop Body" is the contents of the curly brace {} after do). Then the condition in the while is judged, and if the condition is true, the statement in the loop body is executed. Then judge the condition again and repeat the process until the condition is not set to end the while loop
3) features of the Do-while loop: The statement in the loop body is executed at least once, regardless of whether the condition in the while is true
4) In fact, the use of the Do While loop is similar to the while loop, and this is not an example.
Cyclic structure 3-for cycle
The For loop is the most complex of all loop structures.
1 for (statement 1; condition; statement 2) {2 statement 3;3 statement 4;4 ... 5}
1) When the For loop starts, statement 1 is executed and only one statement is executed during the entire loop 1
2) then judge the condition and, if the condition is true, executes the statement in the loop body (the "loop Body" is the contents of the brace {})
3) After the completion of the loop body, the next will execute the statement 2, and then judge the condition again, repeat the process until the condition is not set to end the For loop
For example:
1 intMain () {2 /*3 using the loop output:4 15 1 26 1 2 37 */8 for(inti =1; I <=3; i++) {9 for(intj =1; J <= I; J + +) {Tenprintf"%d", j); One } Aprintf"\ n"); - - } the - return 0; -}
The output is:
1 1 2 1 2 3
It is important to note that the scope of the variable i is line 8th to 14th. Once you leave this first-level for loop, the variable i is invalidated, the variable J is scoped to the 9~11 line, and the variable J is invalidated once it leaves the internal for loop.
Summarize:
The 1.for is most commonly used to know the loop of the number of cycles.
2.while is also very common, often used for loops that do not know the number of cycles.
Break and Continue1.break
A break has been used in the switch statement before, and its function is to jump out of the switch statement. It can also be used in the loop structure, when it is the function of jumping out of the entire loop statement.
Here, for example, break can also be used in a while loop, Do-while loop.
1 for ( int i = 0 ; I<5 ; I++ 2 printf ( " i=%d \ n " , I); 3 if
(I>2 5 ; 6 7 }
The above code means that when i>2, it jumps out of the entire for loop, which is the end of the for loop, so the result is:
i=0 I=1 i=2 i=3
For loop nesting
Let's take a look at a for-loop nesting example, where nesting means: A For loop inside a for cycle
1 for (int0; x<2; x + +) {2 for (int0; y<2; y++) {3 printf ("x=%d, y=% d \ n", x, y); 4 }5 }
The output is:
x=0, y=0 x=0, y=1 x=1, y=0 x=1, y=1
This time if a break is added to the for loop, does this break jump out of the inside or outside for loop?
1 for(intx =0; x<2; X + +) {2 for(inty =0; y<2; y++) {3printf"x=%d, y=%d \ n", x, y);4 5 Break;6 }7}
Notice the break in line 5th, which is the function of jumping out of the for loop, not the outside for loop. So the output is:
If you change the position of the break
1 for(intx =0; x<2; X + +) {2 for(inty =0; y<2; y++) {3printf"x=%d, y=%d \ n", x, y);4 }5 6 Break;7}
Notice the break in line 6th, which is the function of jumping out of the for loop, not the for loop inside. So the output is:
The rule is already obvious: break only affects the for loop where it resides.
2.continue
Continue can only be used in a looping structure, and its role is to skip this cycle and go directly to the next loop.
Here for the For Loop example, continue can also be used in the while loop, Do-while loop.
1 for ( int x = 0 ; X<10 ; X++ If (X%2 ==0 con Tinue ; 4 5 " x=%d \ n 7 }
Note the 2nd line, when X%2==0, that is, when X is a multiple of 2, skips the loop, does not execute the 6th line statement, and goes directly to the next loop. Output Result:
Like break, continue only affects the For loop where it resides.
Lan C language study the third day