The choice structure and cyclic structure of C language

Source: Internet
Author: User

Basic understanding of 1.if

         /* If Judgment statement format     if (conditional expression 1)     {        statement 1 ...     }     else if (conditional expression 2)     {        Statement 2 ...     }     else     {        Statement 3 ...     }          If the conditional expression 1 is true, only statement 1 is executed.     if the condition Expression 2 is established, only executes statement 2,      if none of the above conditions are true, execute statement 3          //if can be nested using     if (conditional expression 1)     {        if (conditional expression)        {             }     }     else     {          }          */

Let's have a little practice practiced hand.

    int a = ten;    if (a >= 2)         {            if (a <= 5) {                printf ("A is a number of one 2~5 \ n");            }        else            {                printf ("A is not a number between 2~5 \ n");}        }    else        {            printf ("A is not a number between 2~5 \ n");        }            

2.switch

/*switch (conditional expression)    {case        A:            statement 1 break            ;        Case B:            statement 2 break            ;            ....        Default:            statement 3 break            ;    }    If the return value of the expression is a, then EXECUTE statement 1, if the return value is B, then execute statement 2, if none, then execute Statement 3.        Note: The    return value of the conditional expression of 1.switch must be shaped, or can be converted to an integer type (character type)    2.case can only be the return value of an integer type/integer/can be converted to an integer type can also (character is the essence of ASCII code    3. If break is not written, it will cause penetration and execute the statement sequentially until it encounters a break.    4. No matter what the default is, it is the last execution */

Let's have a little practice.

    int a = 3;    Switch (a)    {case        1:            printf ("a = 1");            break;        Case 2:            printf ("a = 2");            break;        Case 3:            printf ("a = 3");            break;        Case 4:            printf ("a = 4");            break;        Default:            printf ("All Wrong");            break;    }

3.while

    /*    while format     while (conditional expression)     {        statement (loop body);        ......     }     When the return value of the conditional expression is true, the statement in the while is executed, and when the statement in while is executed, the expression is judged again and again, and the statement continues to execute until the conditional expression is false     */

Go on a little exercise

    int number = 0;    int count = 0;    while (number <=)    {        if (numbers% 7 = = 0)        {            //proves that the current count is a multiple of 7            count++;//similar counter            printf (" A multiple of 7 is%i\n ", number);        }        number++;    }

4 Do While

    /* do    {        statement;        ...    } while (conditional expression);        The difference between do and while: the     first thing to judge the true and false of the conditional expression is to do the loop doing while     : Run the loop first, then judge the true or false of the expression.     

The choice structure and cyclic structure of 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.