==========for Loop and for nesting =========-For loop Syntax format: for (expression 1; expression 2; expression 3) {loop-body;} Function: When entering a for statement, execute expression 1 first (only once!) ), and then to the expression 2 true and false judgment, if True (if False then Exit For loop), then execute the loop body content, after the execution of expression 3, expression 3 after the execution, the expression 2 is re-false judgment. For example:
#include <stdio.h>
int main ()
{
int x;
for (x=0; x<5; x + +)
{
printf ("x =%d\n", i);
}
return 0;
}
The above program outputs the results in vc++6.0:
x = 0
x = 1
x = 2
x = 3
x = 4
Press any key to continue
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
==========for Nesting ===========
When the For loop body is also a for statement, it is called A For loop nesting
For example:
for (expression 1; expression 2; expression 3) {for (expression A; expression b; expression b) {loop body 2; }} instance:
#include <stdio.h>
#define UCHAR unsigned char
#define UINT unsigned int
int main ()
{
Uchar x, y;
for (x=0; x<5; x + +)
{
printf ("x =%d,", x);
for (y=0; y<3; y++)
{
printf ("y =%d,", y);
}
printf ("\ n");
}
return 0;
}
The above program outputs the results in vc++6.0:
x = 0,y = 0,y = 1,y = 2,
x = 1,y = 0,y = 1,y = 2,
x = 2,y = 0,y = 1,y = 2,
x = 3,y = 0,y = 1,y = 2,
x = 4,y = 0,y = 1,y = 2,
Press any key to continue
This shows that x changes every time, y changes 3 times