Use of For loops in C language

Source: Internet
Author: User

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.

    1. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.