Summary of Switch-case usage in C + +

Source: Internet
Author: User

The If statement handles two branches and uses the IF-ELSE-IF structure when dealing with multiple branches, but if there are more branches, the more nested IF statements layer, The procedure is not only large but also difficult to understand. Deep nested else-if statements are often syntactically correct, but logically do not correctly reflect the programmer's intentions. For example, an incorrect else-if match can easily be ignored. It is difficult to ensure correctness by adding new conditions and logical relationships, or by making other changes to the statement. As a result, the C + + language provides a conditional selection statement specifically for dealing with multiple branching structures, known as switch statements, or switching statements. It is easy to implement deep nested if/else logic.

Use the switch statement to directly handle multiple branches (two branches, of course). Its general form is:

switch (expression)
{
case constant Expression 1:
Statement 1;
Break

Case constant Expression 2:
Statement 2;
Break

......
case constant Expression N:
Statement N;
Break

Default
Statement n+1;
Break
}

The execution process for a switch statement is to first compute the value of the expression in the parentheses after the switch, and then use this value to compare the constant expressions in each case, and then execute the statement following this case if the value of the expression in the parentheses is equal to the value of the constant expression following the case. After execution, the break statement exits the switch statement, and if the value of the expression in parentheses is unequal to the constant expression after all cases, execute the statement n+1 after default, and then exit the switch statement, and the program flow turns to the next statement of the switch statement.

The above is the formal writing of Switch-case, the default statement is always written at the end. However, if the default statement between the case in the middle, the implementation of the result of what it is. The author has tested several representative cases, the results are simply listed as follows:

A. Break neatly in each statement
Switch (c)
{
Case ' 1 ':
printf ("1/n");
Break
Default
printf ("default/n");
Break
Case ' 2 ':
printf ("2/n");
Break
Case ' 3 ':
printf ("3/n");
Break
}


This kind of situation is most regular, default write in the middle with write in the final effect consistent.

B. No break after the default statement
Switch (c)
{
Case ' 1 ':
printf ("1/n");
Break
Default
printf ("default/n");
Break
Case ' 2 ':
printf ("2/n");
Break
Case ' 3 ':
printf ("3/n");
Break
}
In this case, the input is 1, 2, 3, 4, the corresponding output is 1, 2, 3, default 2 (line break omitted, the actual operation has a newline); In this case, follow the execution order of a and the general labeling rules.

C. There is no break in the last case
Switch (c)
{
Case ' 1 ':
printf ("1/n");
Break
Default
printf ("default/n");
Break
Case ' 2 ':
printf ("2/n");
Break
Case ' 3 ':
printf ("3/n");
Break
}
In this case, the input is 1, 2, 3, 4 respectively, the corresponding output is 1, 2, 3, default. It is obvious that the actual running effect is not the same as moving the default statement to the last effect. Otherwise, the output should be 3 default if entered as 3 o'clock.

D. Default and Last Case no break
Switch (c)
{
Case ' 1 ':
printf ("1/n");
Break
Default
printf ("default/n");
Break
Case ' 2 ':
printf ("2/n");
Break
Case ' 3 ':
printf ("3/n");
Break
}
From the above a,b,c three kinds of conditions of the operation results, we can be extrapolated D this situation of the operation results. When the input is 1, 2, 3, 4 o'clock, the output is 1, 2, 3, default 2. The result is the same as in B.


These results run the output on the Visual C + + 2008 platform.

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.