For loop format:
For (expression 1; expression 2; expression 3)
{
Looping statements
}
1. First, the value of expression 1 is evaluated.
2, then calculate the value of the expression 2, if the value is true (not 0) the execution of the loop statement 1 times, or jump out of the loop.
3, then the value of the expression 3 is evaluated, back to expression 2 repeated execution.
(the expression 1 is calculated 1 times throughout the for loop, and expression 2 and expression 3 may be evaluated multiple times.) )
for (;;) is a dead loop statement, we only use break, jump out of the loop. Break can only terminate the statement in which it resides, and cannot terminate the entire program.
The FOR Loop statement is used for the number of loops. Enter a number and then ask for the display from large to small.
int arr[12]={1,5,9,8,7,4,2,3,6,0,45,52}; //define an array
int i= 0;
for (; i<=11;i++) //Outer loop control how many numbers need to be compared
{
int j=0;
for (; j<11;j++) //Internal loop controls the number of comparisons per count
{
if (arr[j]>arr[j+1])
{
int temp =arr [j]; //Two number for Exchange
ARR[J]=ARR[J+1];
Arr[j+1]=temp;
}
}
}
for (i=0;i<12;i++)
{
printf ("%d,", arr[i]); //Display results
}
The above is the bubble sort, is two number comparison, if the condition becomes, then two number is exchanged.
For Loop statement