The purpose of the For loop:
Note: To primarily meet conditions one and two after the statement is executed first, then the condition three is executed.
- Simple and repetitive output
for (int i=0;i<10;i++) {
printf ("Simple repetition of output 10 for a sentence");
printf ("\ n");
}
Its basic structure: (as above) for (the initial value of the loop variable; loop condition; loop increment) {... }
Note: In this piece of the loop condition, it is possible to have more than one expression depending on the condition.
2. Bubble sort
Principle: For the array with n elements R[n], the most N-1 wheel comparison;
The first round, by comparison (R[1], r[2]), (r[2], r[3]), (R[3], r[4]), ... (R[n-1], r[n]); The largest element is moved to R[n].
The second round, comparison (r[1], r[2]), (r[2], r[3]), (R[3], r[4]), ... (R[n-2], r[n-1]); The second largest element is moved to r[n-1].
。。。。
And so on, until the entire array is sorted from small to large.
3. Implement the selection sort.
Principle: Select sort to sort the unordered finite array of size N r[n] and make the N-1 wheel selection process. The first wheel selects the small number of I and places it in the first position. When the first N-1 is completed, the number of the nth small (that is, the largest) is naturally in the final position.
Use of For loops in C language