1. Preface
for loops are often used in C, but the most familiar way to use them is in the classical, common form shown below.
int i; for (i = 0; i < 5; i++) { printf("%d\n",i); }
The following author combines his own experience to summarize some forms of use in C language for loop.
2. The Influence of Different C Languages on the Form of for Loop
In C89, our loop variable i must be defined before the loop body, so in the C89 specification, our loop should be in the following format
int i; for (i = 0; i < 5; i++) { printf("%d\n",i); }
If you do this below, you will make a mistake
for (int i = 0; i < 5; i++) { printf("%d\n",i); }
In later C99 specifications, our loops can be written in the following format
for (int i = 0; i < 5; i++) { printf("%d\n",i); }
3. General form of C language loop
for (Expression 1; Expression 2; Expression 3) { Loop statement; } for The loop is executed as follows 1. Execute Expression 1 First,Normally, here we assign an initial value to a circular variable; 2. Re-execute expression 2,To determine whether it is true or not, execute a loop statement,Execute expression 3 after executing loop statement, end loop if false,Go to Step 5; 3. Execute Expression 3; 4. Go back to step 2 above to continue; 5. Loop End, Execute for Statement below the statement.
The simplest and most understandable form of a for statement is as follows:
For (Loop variable initial value; Loop condition; Loop variable increment) statement
Assigning an initial value to a loop variable is always an assignment statement that assigns an initial value to a loop control variable; a loop condition is a relational expression that determines when to exit the loop; a loop variable increment defines how the loop control variable changes after each loop. The three parts are separated by a semicolon (;).
for( i=1; i<=100; i++ ) sum=sum+i;
First assign an initial value of 1 to I to determine if I is less than or equal to 100, then execute the statement, and then increase the value by 1. Then re-evaluate until the condition is false, i>100, and end the cycle. Equivalent to:
i=1; while(i<=100){ sum=sum+i; i++; }
The general form of a statement in a for loop is the following while loop:
Expression 1;
While (expression 2) {
Sentence
Expression 3;
}
Use the for statement to note that:
-
The expressions 1 (loop variable assignment initial value), 2 (loop condition), and 3 (loop variable increment) in the for loop are all selections, which can be defaulted, but the semicolon (;) cannot be defaulted.
-
"Expression 1 (initial value for loop variable) is omitted to indicate that no initial value is assigned to loop control variable."
-
The expression 2 (cyclic condition) is omitted and becomes a dead loop when nothing else is done. For example:
for( i=1; ; i++ ) sum=sum+i;
Amount to:
i=1; while(1){ sum=sum+i; i++; }
- If expression 3 (increment of loop variable) is omitted, the loop control variable is not manipulated, and a statement modifying the loop control variable can be added to the body of the statement. For example:
for( i=1; i<=100 ; ){ sum=sum+i; i++; }
- "Expressions 1 (loop variable assigns initial value) and 3 (loop variable increment) are omitted." For example:
for( ; i<=100 ; ){ sum=sum+i; i++; }
Amount to:
while(i<=100){ sum=sum+i; i++; }
-
All three expressions can be omitted. For example:
Statement for(;;)
Amount to:
while(1) statement -
Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression. For example:
for( sum=0; i<=100; i++ ) sum=sum+i;
- Expressions 1 and 3 can be either 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;
- Expression 2 is generally a relational or logical expression, but it can also be a numeric or character expression that executes a loop as long as its value is not zero. For example:
for( i=0; (c=getchar())!='\n'; i+=c );
Another example is:
for( ; (c=getchar())!='\n' ; ) printf("%c",c);
Nested Loop
#include <stdio.h> int main(void){ int i, j, k; printf("i j k\n"); for (i=0; i<2; i++) for(j=0; j<2; j++) for(k=0; k<2; k++) printf("%d %d %d\n", i, j, k); return 0; }
4. Common C Language for Loop Paradigm
From the above introduction of the general form of for loop usage, we should be familiar with the general use of for loop. Let's take a look at several common ways of using for loop.
1. Each increment of the loop variable is not 1
For example, to print odd numbers within 100
#include "stdio.h" int main() { for (int i = 1; i < 100; i+=2) { printf("%d\n",i); } }
Of course, we can also write in the following form:
#include "stdio.h" int main() { for (int i = 1; i < 100; ) { printf("%d\n",i); i+=2; } }
2. Control multiple cyclic variables simultaneously
#include "stdio.h" int main() { for (int i = 1, j =2; i < 100; i+=2, j+=2) { printf("i:%d\n",i); printf("j:%d\n",j); } }
Note that int i = 1, j =2 in an expression cannot be written as int i = 1, int j =2, otherwise an error will occur, and we can also see from this that if a type definition (such as int in this article) was used before, then the subsequent variable types must be identical to that type.
If you want to use two different variable types, you should not define variables in Expression 1, but you should define them before that.
#include "stdio.h" int main() { int i; char j; for (i = 1, j =2; i < 100; i+=2, j+=2) { printf("i:%d\n",i); printf("j:%c\n",j); } }
3.The form of true and false judgment in expression 2 can be more diverse
In general, when using for loops, we often judge whether a loop variable or a value is true or false, but in fact, an expression or a logical expression can be put in Expression 2, so that we can set the termination condition of the loop more flexibly.