C-language loop structure

Source: Internet
Author: User

Loop structure:

1.goto Unconditional Transfer Statement

goto//Unconditional Transfer Statement////label://goto Label;//goto Union if statement use//try not to use goto//only within the current function to jump int main (int argc, const char * argv[]) { C0/>int i=0;print://    printf ("Hello world\n");    i++;    if (i<10) {        goto print;    }        return 0;}

Example: Goto implementation 1-100 Add

#include <stdio.h>int main (int argc,const char *argv[]) {    int sum=0;    int I=1;label:    sum+=i;    i++;    if (i<=100) {        goto label;    }    printf ("sum =%d\n", sum);    return 0;}

2.while Cycle Structure

While//while (expression)//{//    loop body Statement list,//}//expression true, execution loop body, false exit Loop # include <stdio.h>int main (int argc,const char * Argv[]) {    int i=1;    int sum = 0;    while (i<=100) {        sum+=i;        i++;    }    printf ("sum =%d\n", sum);        return 0;}

Example: Statistics input a line of characters from the keyboard

Method One:

int main (int argc,const char *argv[]) {    int cnt=0;//statistic count    Char ch;    while (scanf ("%c", &ch), ch!= ' \ n ') {//stdin        cnt++;    }    printf ("cnt =%d\n", cnt);        return 0;}

Method Two:

int main (int argc,const char *argv[]) {    int cnt=0;    while (GetChar ()! = ' \ n ') {//getchar reads a character from the standard input file each time and returns the character to        cnt++;    }    printf ("cnt =%d\n", cnt);//stdout standard output file    fprintf (stdout, "cnt =%d\n", cnt);        return 0;}

3.do...while

do//{//    loop body;//}//while (conditional expression);//execute the Loop body first, and then determine whether the expression is set up//1-100 odd and (do While) int main (int argc, const char *ARGV []) {    int sum =0;    int i=1;    Do    {        if (i%2)        {            sum + = i;        }        i++;    }    while (i<=100);    printf ("sum =%d\n", sum);    return 0;}

4. For loop structure

For//for (Assignment statement; conditional expression; Change statement) {//    loop body;//}int Main (int argc,const char *argv[]) {    int i;        for (i = 0; i<10; i++) {        printf ("Hello world\n");    }    return 0;}

Example: printing all odd numbers within 1-100 (for implementation)

int main (int argc,const char *argv[]) {    for (int i=1; i<=100; i++) {        if (i%2) {            printf ("%d", I);        }    }    printf ("\ n");    Putchar (' \ n ');//output one character        return 0;}

eg. Continue,break keyword Usage

int main (int argc,const char *argv[]) {    for (int i=0; i<10; i++) {        if (i==5) {            continue;//ends this loop, continues the next loop 
   //break;//jumps out of the whole loop        }        printf ("i =%d Hello world\n", i);    }    return 0;}

Example: Find all the primes in 1-100

All primes in 1~100/*int main (int argc,const char *argv[]) {    int i,j;    For (I=1, i<=100; i++) {for        (j=2; j<i; J + +) {//i% (2 ~ i-1)            if (i%j==0) {break                ;            }        }        if (i!=j) {            continue;        }        printf ("%d", I);    }    return 0;} */

C-language loop structure

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.