Home > Article > Backend Development > Does a for loop execute the loop body statement first and then evaluate the expression?
No, the for loop first judges the expression and then executes the loop body statement. The general form is "for (expression 1; expression 2; expression 3) {loop body}"; first execute "expression 1"; then execute "expression 2", if its value is true (non-0), Then execute the loop body, otherwise end the loop; execute "expression 3" after executing the loop body.
The general form of a for loop is:
for(表达式1; 表达式2; 表达式3){ 语句块 }
Its running process is:
1) Execute "Expression 1" first.
2) Execute "expression 2" again. If its value is true (non-0), execute the loop body, otherwise end the loop.
3) Execute "expression 3" after executing the loop body.
4) Repeat steps 2) and 3) until the value of "expression 2" is false, then end the loop.
In the above steps, 2) and 3) are a loop and will be executed repeatedly. The main function of the for statement is to continuously execute steps 2) and 3).
"Expression 1" is only executed during the first loop and will not be executed again in the future. This can be considered an initialization statement. "Expression 2" is generally a relational expression, which determines whether to continue the next loop, which is called the "loop condition". "Expression 3" is often an expression with an increment or decrement operation, so that the loop condition gradually becomes "not true".
The execution process of the for loop can be represented by the following figure:
Let’s analyze the “calculation of adding from 1 to 100 and" code:
#include <stdio.h> int main(){ int i, sum=0; for(i=1; i<=100; i++){ sum+=i; } printf("%d\n",sum); return 0; }
Running result:
5050
Code analysis:
1) When executing the for statement, first assign an initial value to i 1. Determine whether i
2) During the second loop, the value of i is 2, i
3) Repeat step 2) until the 101st loop. At this time, the value of i is 101, and i
From this we can summarize the general form of the for loop:
for(初始化语句; 循环条件; 自增或自减){ 语句块 }
The three expressions in the for loop
" in the for loop "Expression 1 (initialization condition)", "Expression 2 (loop condition)" and "Expression 3 (self-increment or self-decrement)" are all optional and can be omitted (but the semicolon;
must be retained).
1) Modify the code for "sum from 1 to 100" and omit "expression 1 (initialization condition)":
int i = 1, sum = 0; for( ; i<=100; i++){ sum+=i; }
You can see that i=1
Moved outside the for loop.
2) If "expression 2 (loop condition)" is omitted, it will become an infinite loop if no other processing is done. For example:
for(i=1; ; i++) sum=sum+i;
is equivalent to:
i=1; while(1){ sum=sum+i; i++; }
The so-called infinite loop means that the loop condition is always true, and the loop will continue and never end. Infinite loops are very harmful to the program and must be avoided.
3) If "expression 3 (self-increment or self-decrement)" is omitted, the variables in "expression 2 (loop condition)" will not be modified. In this case, the modified variables can be added to the loop body statement. For example:
for( i=1; i<=100; ){ sum=sum+i; i++; }
4) "Expression 1 (initialization statement)" and "Expression 3 (self-increment or self-decrement)" are omitted. For example:
for( ; i<=100 ; ){ sum=sum+i; i++; }
is equivalent to:
while(i<=100){ sum=sum+i; i++; }
5) 3 expressions can be omitted at the same time. For example:
for( ; ; ) 语句
is equivalent to:
while(1) 语句
6) "Expression 1" can be an initialization statement or other statements. For example:
for( sum=0; i<=100; i++ ) sum=sum+i;
7) "Expression 1" and "Expression 3" can be a simple expression or a comma expression.
for( sum=0,i=1; i<=100; i++ ) sum=sum+i;
or:
for( i=0,j=100; i<=100; i++,j-- ) k=i+j;
8) "Expression 2" is generally a relational expression or a logical expression, but it can also be a numerical value or character. As long as its value is non-zero, the loop body will be executed. . For example:
for( i=0; (c=getchar())!='\n'; i+=c );
Another example:
for( ; (c=getchar())!='\n' ; ) printf("%c",c);
Related recommendations: "c Language Tutorial"
The above is the detailed content of Does a for loop execute the loop body statement first and then evaluate the expression?. For more information, please follow other related articles on the PHP Chinese website!