Home > Article > Backend Development > What is the execution process of for statement in C language?
The execution process of the for statement in C language is: 1. It will first determine whether the conditional expression is true. If the condition is true, the intermediate loop body will be executed. After the intermediate loop body is executed, the final loop body will be executed; 2. After the final loop body is executed, the conditional expression is evaluated again. If the condition is still true, the middle loop body will continue to be repeated. When the condition is not true, it will jump out.
The execution process of the for statement in C language is:
The general form of the for statement is: for (single Secondary expression; conditional expression; end loop body) {intermediate loop body;}.
When the for loop is executed, it will first determine whether the conditional expression is true. If the condition is true, the intermediate loop body will be executed. After the intermediate loop body is executed, the final loop body will be executed. After the final loop body is executed, the conditional expression is judged again. If the condition is still true, the middle loop body will continue to be repeated. When the condition is not true, the for loop will be jumped out.
The intermediate loop body executed in the for loop can be one statement or multiple statements. When the intermediate loop body has only one statement, its braces {} can be omitted.
Extended information
Notes on the for statement:
1. The variables in the for loop can be One can also be multiple. Don't be limited to processing one variable to the end, which can sometimes be very monotonous. When the program is suitable for multiple variables to form a loop, it is a bit wasteful to use multiple for statements to express it.
2. The statement in the for loop can be a statement body. Add braces "{}" outside the statement body to enclose the statements participating in the loop. The three expressions of the for loop are all comma expressions. In this way, each semicolon expression can actually be composed of several comma expressions, separated by "," and finally ended with ";".
3. "Single expression", "conditional expression" and "loop body statement" in the for loop are all options, that is, they can be defaulted to empty statements with only ";" left. The reason why ";" cannot be defaulted is that the compilation format is such that if it is defaulted, the compiler will not recognize it and will report an error.
Related learning recommendations: C video tutorial
The above is the detailed content of What is the execution process of for statement in C language?. For more information, please follow other related articles on the PHP Chinese website!