Branching structure in C language

Source: Internet
Author: User

<a href= "http://www.goodprogrammer.org/" target= "blank" >ios training </A>------My C language notes, look forward to communicating with you!

In real life we often need to make different choices based on different conditions. In the program design, it is also necessary to choose different programs according to the conditions, which is called the branch structure.

The main control branch structure in C language is the IF statement and the switch statement. First, say the IF statement:

The single-branched structure of the IF statement: if (condition e) {statement S;}. When the condition E is satisfied, the statement s in curly braces are executed. Note (when the curly braces are just a single statement, that is, there is only one statement,

Curly braces can be omitted, and of course not omitted.

The double-branched structure of the IF statement: if (condition e) {statement S1;} else {statement s2;}. When the condition E is satisfied, the statement S1 is executed and the statement S2 under the Else branch is executed when the condition is not satisfied;

Multi-branched form of if statement: if (condition 1) {statement 1;}  ElseIf (condition 2) {statement 2;}  ElseIf (condition 3) {statement 3;} ... elseif (condition N) {statement n;} else {statement n+1;}

The multi-branch conditional statement here means that if condition 1 is true, statement 1 is executed, otherwise, if condition 2 is true, Statement 2 is executed. Otherwise, if condition n is true, the statement n is executed,

Otherwise, if the preceding conditions are not true, the statement n+1 under the Else branch is executed;

If statements can also be nested, such as:

if (condition 1)

{

if (condition 2)

{Statement 1;}

Else

{Statement 2;}

}

Else

{

if (condition 3)

{Statement 3;}

Else

{Statement 4;}

}

The basic rules are the same as those described earlier.

Next, let's introduce the switch branching structure:

Basic format:

switch (expression)

{

CASE constant 1: statement sequence 1;break;

CASE constant 2: statement sequence 2;break;

.....

CASE constant N: statement sequence n;break;

Default: Statement sequence n+1;

}

The meaning of this structure is that when the value of an expression matches a constant in the following case, the sequence of statements under that scenario is executed, and if the value of the expression does not match the constants in all cases,

Executes the sequence of statements under default;

We see that the last face of each case structure ends with a break. The effect of break is to forcibly jump out of the switch structure after executing the case branch.


The above is my understanding of the two branches of C language Structure!

Branching structure in C language

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.