Switch ... case statement usage (ii)

Source: Internet
Author: User
Tags case statement

In summary, the use of switch is to determine if the expression after the case matches the expression after switch, and once the case is matched, the subsequent program code is executed, regardless of whether the following case matches until the break was met . Do not match to find default. No matter where default is placed. As soon as a junction (case or default) enters, it executes the following code in sequence, knowing that a break is encountered

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 statement layers, The procedure is not only large but also difficult to understand. Deeply nested else-if statements tend to be syntactically correct, but logically do not correctly reflect the programmer's intentions. For example, an incorrect else-if match can easily be ignored. Adding new conditional and logical relationships, or making other changes to statements, makes it difficult to guarantee correctness. As a result, the C + + language provides a conditional selection statement specifically for processing a multi-branch structure called a switch statement, also known as a switch statement. It is easy to implement deep nested if/else logic.

Use the switch statement to process multiple branches directly (including, of course, two branches). The 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 switch statement is executed by first calculating the value of the expression in parentheses after the switch, and then comparing it to the constant expression of each case in turn, and executing the statement that follows it if the value of the expression in the parentheses is equal to the value of the constant expression following the Exit the switch statement when the break statement is executed, and if the value of the expression in the parentheses is not equal to the constant expression after all cases, execute the statement n+1 after default, then exit the switch statement, and the program flow switches to the next statement of the switch statement.

The above is the formal wording of Switch-case, the default statement is always written in the end. But what happens if the default statement is in the middle of the case? The author tested a few representative cases, the results are briefly 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 is the most structured case, and default is written in the middle with the same effect written in the final.

B. No break after 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 inputs are 1, 2, 3, 4, respectively, the corresponding output is 1, 2, 3, default 2 (line break omitted, the actual run with a newline), that is, in this case to follow the order of execution and the general labeling rules.

C. The last case has 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
}
In this case, the inputs are 1, 2, 3, 4, respectively, the corresponding output is 1, 2, 3, default. The actual running effect is not the same as moving the default statement to the last effect. Otherwise, input is 3 o'clock and the output should be 3 default.

D. Neither default nor the last case has a 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 results of the above a,b,c three cases, we can measure the result of the operation of this case d. When input is 1, 2, 3, 4 o'clock, the output is 1, 2, 3, default 2. The result is the same as the B case.


All of the above results run output on the Visual C + + 2008 platform.

Switch ... case statement usage (ii)

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.