-
Definition of a for loop.
for(the initial value of the variable; termination of operating conditions; The initial value of the first cycle i is 0, and it runs until i=3 stops i=2, and the condition is still satisfied, and the loop body is still executed, and the step size is 1+1 each time
The initial value of the second loop j is 2, and it runs until the outer loop of j is executed 3 times, and when it is executed to the fourth time, i = 3, the loop condition is no longer satisfied, and the inner loop is not executed. 0 1 2
The first outer loop i=0 array is not what it looks like.
Inner circulation, run 3 times, t=2+1+0
The second outer loop i=1
The inner loop is run 2 times t=2+1
The third external circulation i=2
The inner loop runs 1 time t=2
The fourth i=3
If the conditions are not met, the inner loop will no longer be executed.
Run to see if the value of t is 8
The program asks for the output t,
-
This is the first statement, so all 6 elements of the triangle on the upper right side of the 3-row 3-column two-dimensional array are added to t.
for(i=0;i<3;i++)
for(j=2;j>=i;j--)t+=b[i][j];
Here is the second statement, which outputs the 6 elements that have been added up so far.
printf("%d",t);
-
This is the simplest for loop, i=0, add 3 numbers in the first row, i=1, add 2 numbers in the second row, and add 1 number in the third row when i=3.
-
Because you "for(j=2; j>=i;j--)t+=b[i][j] >> t has an extra + after him to go to the line, the plus sign behind t means that every time for b[i][i] is added once, so the result becomes 0+1+2=3.
main()
int b[3][3]=, i,j,t=0;
for(i=0;i<3;i++)
for(j=2;j>=i;j--)t+=b[i][j];Just change this place to for(j=2; j>=i;j--)t+=b[i][j];
printf("%d",t);
You won't be asking me if you try.
-
t+=b[i][j] is t=t+b[i][j], which can be understood.
-
The execution logic of the for loop is:
Perform the initialization part, which is i = 0;
Judge whether the condition is satisfied, if the condition is not met, it will jump out of the loop directly, the condition is that a[i] executes the for loop body, here is an empty statement;
Perform a variable update, in this case i++;
Skip to Part 2;
Among them, when the second part jumps out, the program directly jumps to the fifth step and continues to execute, that is, i++ is not executed;
So, when i is 3, the value is 4, and a[i] so in the end, the value of i is 3
-
Array subscripts start at 0.
Initial conditions: s=4, i=0, a[0]=1, a[1]=2, a[3]=3, a[4]=4......
The first cycle i=0, a[0]<4 holds, the second cycle i=1, a[1]<4 holds, the third cycle i=2, a[2]<4 holds, the fourth cycle i=3, a[3]<4 does not hold, exit the loop, and i=3 at this time
-
Note that the subscript of the array starts at 0, and when i=3, a[3] is 4, and a[3] is judged
-
a[3]=4 Ah, the subscript starts at 0, and i certainly shouldn't be 4
-
For Statement Usage in C Language:
for is a keyword in C that is used to control the execution of loop statements.
Example: int i; for(i=0; i<3; i++)
i=0 is the initialization part; i<3 is the part of the circular judgment condition (when this condition is met, the statement in the for loop will be executed); i++ is the operation after the loop statement has been executed.
The above for statement is to control the output of helloworld 3 times.
The for loop is a kind of loop statement in the programming language, and the loop statement is composed of two parts: the loop body and the judgment condition of the loop, and its expression is: for (single expression; conditional expressions; Loop body at the end).
1.The shortest form of the statement is: for( ;
2.The general form is: for (single expression; conditional expressions; Loop body at the end).
Among them, the expression can be omitted, but the semicolon cannot be omitted, because "; It can represent an empty statement, and after omitting it, the statement is reduced, that is, the statement format changes, and the compiler cannot recognize it and cannot compile it.
for the first one in parentheses of the loop"; It is a single-time expression that does not participate in the loop, which can be used as an initial assignment statement for a variable, and is used to assign an initial value to the loop control variable. It can also be used to evaluate an expression that is not related to the for loop but precedes the loop section.
The conditional expression is a relational expression that is the formal beginning of the loop, and when the conditional expression is true, an intermediate loop body is executed.
The executed intermediate loop can be a single statement or multiple statements, and when the intermediate loop has only one statement, its curly braces {} can be omitted, and the end loop can be executed after the middle loop is executed.
After executing the end loop body, the condition will be judged again, and if the condition is still true, the above cycle will be repeated, and when the condition is not true, the current for loop will be jumped.
-
The most flexible, simplest, and best understood loop usage.
for(Expression1; Expression 2; Expression 3).
These three expressions can be both, or none, or they can be composed of any one or both of them, and this is where the flexibility of for lies.
Explain what the three expressions mean.
Expression 1, which is usually an initialization condition, e.g. i=1. Before the first semicolon, you can use one, or several, separated by commas.
Expression 2, this thing loop condition, if it is satisfied, it will continue to execute the loop body, and if it is not satisfied, it will jump out of the loop. This is usually a logical judgment statement.
Expression 3, which is generally where the condition of the loop is changed, such as i++.
The order of execution is that the initialization is carried out first, that is, the operation expression 1, and after the initialization is completed, this expression is useless.
Then determine whether the loop condition is satisfied, that is, operate expression 2, and if it is true, execute the loop body.
Finally, Expression 3 is operated to change the loop condition. This completes the first cycle.
Then the second loop, i.e., Equation 2, is used to see if the loop condition is satisfied, and if Expression 2 is still satisfied (i.e., true), the loop body is executed. After completing the operation of Expression 3,.
This is repeated until expression 2 is not satisfied, and then the loop is broken.
It's worth noting here that, for example.
For example, for(i=0; i<10;i++), when i=9, the loop is satisfied, and after the execution, i will add 1 by itself, that is, i++, so that the value of i is 10, and when i is judged to be < 10, so when the loop condition is not satisfied, the value of i is 10, not 9
-
If you are compiling on one of the above versions, you should press crt+f5 instead of f5 when debugging
-
For example, for(i=0; i<10;i++)
The first one in for is the initial condition, which can be initialized in front of for without writing, such as i=0; for(;i<10;i++)
The second is the judgment condition, that is, the condition for terminating the cycle;
The third is to change the conditions;
The main three conditions are separated by semicolons.
-
Except for what was said on the first floor.
The first and third conditions can be left unwritten, and can be written in parentheses; for(;i
-
The for statement in C++ is the most widely used and flexible, and can be used not only when the number of loops is determined, but also when the number of loops is uncertain and only the loop end condition is given, which can completely replace the while statement.
The general format of a for statement is:
for(Expression1; Expression 2; Expression 3) statement.
The execution process of the for statement is as follows:
Solve Expression 1 first.
Solve expression 2, and if its value is true (the value is non-0), execute the inline statement specified in the for statement, and then execute step (3) below. If false (value is 0), end the loop and go to step (5).
Solve Expression 3.
Go back to step (2) above and continue.
When the loop ends, execute the statement following the for statement.
The simplest form of the diagram for statement is also the easiest to understand format is as follows:
for(Initial value of cyclic variable; cyclic conditions; Loop variable value-added) statement.
For example: for(i=1; i<=100;i++)sum=sum+i;
It is equivalent to the following statement:
i=1;while(i<=100)
i=1;while(i<=100)
Obviously, using the for statement is simple and convenient.
There are many techniques for the use of for statements, and if you master and use for statements skillfully, you can make the program concise and concise.
A few notes about the for statement:
"Expression 1" can be omitted in the general format of the for statement, in which case the initial value of the loop variable should be assigned before the for statement.
If Expression 2 is omitted, the loop condition is not judged, and the loop continues without termination. That is, expression 2 is always considered to be true.
Expression 3 can also be omitted, but the programmer should make an additional effort to ensure that the loop ends gracefully.
Expressions 1 and 3 can be omitted and only Expression 2 is given, i.e. only the loop condition is given.
All 3 expressions can be omitted.
Expression 1 can be an assignment expression that sets the initial value of a loop variable, or it can be another expression that is not related to a loop variable.
Expressions are generally relational expressions (e.g., i<=100) or logical expressions (e.g., for statements in AC++) that are much more powerful than circular statements in other languages. You can also appear as Expression 1 or Expression 3 for the loop body and some operations that are not related to loop control, so that the program can be short and concise. However, overexploiting this feature will make the for statement appear cluttered and less readable, so it is recommended not to put content that is not related to loop control into the for statement.
-
for statement.
The for statement is also used to implement bai"Dang type"loop, duits general format is: zhì
daofor (initialization;termination;iteration)
When the statement is executed, the initialization operation is first executed, then the termination condition is satisfied, if so, the statement in the loop body is executed, and finally the iteration part is executed. After a cycle is completed, the termination condition is re-evaluated.
2.You can declare a variable in the initialization part of the for statement, and its scope is a for statement.
Statements are typically used to perform cases where the number of loops is determined, such as when operating on an array element, or where the number of loops is indeterminate depending on the loop end condition.
4.In the initialization and iteration sections, you can use comma statements to perform multiple actions. A comma statement is a sequence of statements separated by a comma. For example:
for(i=0,j=10;i5.The initialization, termination, and iteration parts can all be empty statements (), and when all three are empty, it is equivalent to an infinite loop, such as:
for(i=0;;i++)
main() [main function main program].
int i,j,k;【Define integer data i,j,k】for(i=1; i<=6;i++) main loop, i from 1 to 6, increasing to 1] for(j=1; j<=20-2*i;j++) subcycle, j from 1 to 20-2*i >>>More
a1.For the literal constant 4, the compiler thinks that it is int and double, so although the value of 4 3 is equal to, when it is converted to an integer, it is 1, and for that expression, its result is double, so the decimal part can be preserved. The knowledge involved in this question is an implicit conversion of types. >>>More
This problem requires understanding that the system allocates memory to static variables when compiling, and the memory units occupied by them are not released after the function call ends, that is, the value of the variable is the value of the previous function call at the next function call. >>>More
It's called the [batch] program, in fact, don't think that the program you mentioned above to deal with garbage is really powerful, it's amazing, when you have learned DOS, that program can be said to be a very simple thing; >>>More
Scope. You static char *chh;
static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More