CompileProgramUsually, the for loop is used. How is the for statement executed?
Of course, anyone with some programming experience will know that the for statement is a program block that implements loop execution. The for structure is generally roughly as follows:
For (expression 1; expression 2; expression 3) statement;
Or
For (expression 1; expression 2; expression 3 ){
Statement block;
}
Expression 1 is generally used for variable initialization; expression 2 is mainly used to control the loop. If expression 2 calculates true, it continues to execute the loop body; otherwise, the loop ends; expression 3 is generally used for variable auto-increment operations;
For example, a simple output is 1 to 10. Program:
For (INT I = 1; I <= 10; I ++) printf ("% d", I );
In the preceding programFor (INT I = 1; I <= 10; I ++)Is the expression 1, 2, and 3 executed in parentheses? Or how is the execution?
The following is an example program:
Int x = 10; int y = 10; int I = 10;
For (I = 0; x> 8; y = I ++ ){
Printf ("% d, % d, % d \ n", X --, I, y );
}
The execution result is:
10, 0, 10
9, 1, 0
Obviously, the result in the first line is visible, I = 0, executed; x> 8 is executed before the result is output; but y = 10, y = I ++ is not executed. The result of the second line shows I = 1, y = 0.
Actually fromFor (INT I = 1; I <= 10; I ++) printf ("% d", I );Output 1, 2, 3 ,......, 10. For example, when I = 1 is output in the first loop, the visible expression 3I ++ is not executed yet.
Cause:
The true execution process of for loop statements is:
1) first solve expression 1;
2) solution expression 2. if the result is true, execute the statement or statement block of the loop body. Otherwise, the loop ends;
3) solution expression 3;
4) jump back to step 2) Continue the execution;
5) after the loop ends, execute the for statement.
From the above, expression 3 is executed only after the loop body statement or statement block is executed. This is why the example program does not execute y = I ++.