Three basic structures of C language programs

Source: Internet
Author: User
Tags case statement switch case

1, the program structure: in the C language program, a total of three kinds of program structure: The order structure, the choice structure (branch structure), the circulation structure;

Sequential structure: From beginning to end a sentence followed by the execution down, until the execution of the last sentence;

Select structure: After a certain node, according to the results of a judgement to decide which branch direction to execute;

Loop structure: Loop structure has a loop body, the loop body is a piece of code. For the circular structure, the key is to decide how many times the loop body executes according to the result of judgment.

Note: There is a logical bool type (also called a Boolean type, Boolean type) with only two values, that is, true and false. The final value of the judgment expression of C is a type of bool, and the bool value of the judgment expression determines how the selection structure is chosen and how the cyclic structure loops.

2, the order structure: The order structure is very simple, generally we encountered in addition to the choice structure and the circular structure, are the sequential structure;

3, choose the structure: C language commonly used in the choice of structure mainly has the following two kinds:

(1) If else: Introduce keyword: if else else if

if (bool value)//If the bool value is True, execute code Snippet 1, otherwise execute code snippet 2

{

Code Snippet 1

}

Else

{

Code Snippet 2

}

if (bool value 1)//if BOOL value 1 is true, execute code Snippet 1, otherwise determine if bool value 2 is True

{

Code Snippet 1

}

else if (bool value 2)//if BOOL value 2 is true then code Snippet 2 is executed, otherwise code snippet 3 is executed directly

There can be only one for the if and at the end of the {///, but the intermediate else if can have a lot of

Code Snippet 2

}

Else

{

Code Snippet 3

}

1#include <stdio.h>2 3 intMainvoid)4 {5     intA, B, Max;6     7A =456;8b = $;9     Ten     if(A > B)//Judging Expression One     { AMax =A; -printf"true.\n"); -     } the     Else if(A = =b) -     { -Max =A; -printf"A = = b.\n"); +     } -     Else +     { AMax =b; atprintf"false.\n"); -     } -      -printf"max =%d.\n", max); -      -     return 0; in}

(2) Switch case: Introducing the keyword: switch case break default

Switch (variable) //Execute to this sentence, the value of the variable is known

when executing the {//switch case statement, the value of the variable is compared to the constant after each case, trying to find the first match , and after the matching item is found,

    CASE constant 1: // go to the code snippet that corresponds to it , and if it is not found, continue to the next, until the default

    code Snippet 1; //If the previous case does not match, then the default match.

    Break

     Case constant 2:

    Code snippet 2;

    Break

     ...

    Default

    Code snippet N;

    Break

}

Note: First, the case must be constant, and must be plastic;

Second, in general, each case must have a break after the code snippet;

Third, the case after the general will have the default, although the syntax allows no default, but it is recommended to write the code must write;

1 //Structured Design-Select Structure Sample code switch case Demo2#include <stdio.h>3 4 intMainvoid)5 {6     intnum;7     8num =1;9     Ten     Switch(num) One     { A          Case 1: -printf"a\n"); -              Break; the          Case 2: -printf"b\n"); -              Break; -          Case 3: +printf"c\n"); -              Break; +          Case 4: Aprintf"d\n"); at              Break; -         default: -printf"-\n"); -              Break; -     } -      in     return 0; -}

(3) The difference between if else and switch case: if else is more complex to compare, but less branching, the switch case is suitable for those cases where the comparison is simple, but the branching is more;

As a general rule, switch case is preferred in situations where switch cases are appropriate, if not appropriate, if else is used;

4, the circulation structure: C language commonly used in the circulation structure has the following three kinds:

(1) for:

for (loop control variable initialization; loop termination condition; loop control variable increment)

{

Loop body

}

Loop execution steps: First, the loop control variables are initialized first;

Second, the execution of the cyclic termination condition, if the judgment result is true, then enter the third step, if the false then the loop terminates and exits;

Third, the implementation of the loop body;

Finally, the increment of cyclic control variable is executed, and the second step is transferred;

Note: The three parts of the For loop () can be omitted in addition to the loop termination condition, but in the standard for loop, the initialization of the loop control variable, the increment is placed in the (), and the loop control variable should never be changed in the loop body;

1 //calculate 1+2+3+ +102#include <stdio.h>3 4 intMainvoid)5 {6     inti, sum;7     8printf"i =%d.\n", sum);9     Ten      for(i=0, sum=0; i<=Ten; i++) One     { ASum + =i; -     } -printf"sum =%d.\n", sum); the      -     return 0; -}

(2) While:

loop control Variable Initialization

while (loop termination condition)

{

Loop body

loop control variable Increment

}

Loop execution Step: First, the loop control variable is initialized (before the while);

Second, judge the cycle termination condition, if the judgment result is true, then enters the third step, if is false then does not carry on the loop body;

Third, the implementation of the loop body;

Finally, the increment of cyclic control variable is executed, and the second step is transferred;

1 //calculates all the odd numbers within 100 and2#include <stdio.h>3 4 intMainvoid)5 {6     inti, sum;7     8i =1;9sum =0;Ten      One      while(I < -) A     { -printf"i =%d.\n", i); -Sum + =i; thei + =2; -     } -      -printf"sum =%d.\n", sum); +      -     return 0; +}

(3) Do While:

loop control Variable Initialization

Do

{

Loop body

loop control variable Increment

}while (cyclic termination conditions);

Loop execution Step: First, the loop control variable is initialized (before do);

Second, the implementation of the loop body;

Thirdly, the increment of cyclic control variable is executed;

The third is to judge the cyclic termination condition, if the result is true, the second step is returned, and if False, the loop exits directly;

1 //calculates all the odd numbers within 100 and2#include <stdio.h>3 4 intMainvoid)5 {6     inti, sum;7     8i =1;9sum =0;Ten      One      Do A     { -printf"i =%d.\n", i); -Sum + =i; thei + =2; -} while(I < -); -      -printf"sum =%d.\n", sum); +      -     return 0; +}

(4) Summary: No matter what kind of loop structure, can not lack of cyclic control condition initialization, termination conditions, cyclic control variable increment, the loop body four parts;

While loop is the first to judge after execution, do While loop is executed first after judgment, and so on after the cycle, in fact, are the same;

(The above content for the study of teacher Zhu's embedded course after reviewing notes obtained, the purpose is to review and consolidate knowledge, at the same time to share their knowledge learned. The ability is limited, the level is general, if has the mistake, welcome correction, thanks! )

2017-02-26 17:52:18

Three basic structures of C language programs

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.