C language
-
The general form of the for statement
-
For — loop structure
-
for( :)
-
int a, sum = 0; for (a = 1; a <= 100; a++) { sum = sum + a; } printf("sum=%d", sum);
-
A = 1 in parentheses is the initial value of the cyclic variable, a < = 100 is the condition for the end of the cyclic variable, a + + is the added value of the cyclic variable
-
The expression is 1, which can be omitted
-
int a, sum = 0; a = 1;// You can assign a value to it first - the same usage as the previous one For (; a < = 100; a + +) // must have; If there is no sign, it will fail. It is only to omit expression 1, that is, a = 1 { sum = sum + a; } printf("sum=%d", sum);
-
Expression 2 can also be omitted, but the end of break must be added, otherwise it will loop indefinitely
-
int a, sum = 0; a = 1; For (;; a + +) // must have; If no, it will fail { sum = sum + a; If (a > = 100) // conditions for ending the loop break; // The purpose is to end the cycle } printf("sum=%d", sum);
-
Effect omitted from expression 3
-
int a, sum; for (sum = 0,a = 1; a <= 100; )// Must have; If no, it will fail { sum = sum + a; a++;// Omit expression 3 to ensure that the for loop can end normally } printf("sum=%d", sum);
-
sum = 0,a = 1;
1. Sum = 0, - comma expression 2. sum = 0,a = 1; —— Expression 1. There is a comma expression in expression 1
-
Expression 1, expression 2 and expression 3 in for can be omitted
-
-
For statement actual
-
Main description of the for statement
-
、