How to use C ++ switch-case statements

Source: Internet
Author: User

In the C ++ programming language, some judgment statements exist, such as the if and C ++ switch-case statements. In this article, we will introduce the basic usage of the C ++ switch-case statement in detail, hoping to help you.

  • Detailed description of how C ++ implements WPF Animation
  • Detailed description of C ++ Chinese character-related application methods
  • What benefits do C ++ fictitious functions bring to us?
  • Detailed description of C ++ bit operations
  • How to use C ++ Endian

If statements process two branches, the if-else-if structure must be used when processing multiple branches. However, if there are many branches, the more nested if statement layers, the program is not only huge but also difficult to understand. deep nested else-if statements are often syntactically correct, but do not logically reflect the programmer's intent. For example, the error else-if match is easily ignored.

Adding new conditions and logical relationships, or making other modifications to statements, makes it difficult to ensure correctness. Therefore, the C/C ++ language provides a conditional selection statement specifically used to process multi-branch structures, called a switch statement or a switch statement. it can easily implement the nested if/else logic.

Use the C ++ switch-case statement to directly process multiple branches (including two branches of course). The general form is:

 
 
  1. Switch (expression)
  2. {
  3. Case constant expression 1:
  4. Statement 1;
  5. Break;
  6. Case constant expression 2:
  7. Statement 2;
  8. Break;
  9. ......
  10. Case constant expression n:
  11. Statement n;
  12. Break;
  13. Default:
  14. Statement n + 1;
  15. Break;
  16. }

The execution process of the C ++ switch-case statement is: first, calculate the value of the expression in parentheses after the switch, and then use this value to compare it with the constant expressions of each case in sequence, if the value of the expression in parentheses is equal to the value of the constant expression after a case, execute the statement after this case, and then exit the switch statement when the break statement is executed; if the expression value in parentheses is different from the constant expression after all cases, execute the statement n + 1 after default and exit the switch statement. The program flow turns to the next statement of the switch statement.

The above is the regular expression of switch-case, and the default statement is always written at the end. However, if the default statement is placed in the middle of case, what is the execution result? I tested several representative cases and listed the results as follows:

A. break consolidation in each statement

 
 
  1. switch(c)  
  2. {  
  3. case '1':   
  4. printf("1\n");  
  5. break;  
  6. default:   
  7. printf("default\n");  
  8. break;  
  9. case '2':  
  10. printf("2\n");  
  11. break;  
  12. case '3':  
  13. printf("3\n");  
  14. break;  

This situation is the most regular, and the effect of default is the same as that of the last one.

B. No break after the default statement

 
 
  1. switch(c)  
  2. {  
  3. case '1':   
  4. printf("1\n");  
  5. break;  
  6. default:   
  7. printf("default\n");  
  8. // break;  
  9. case '2':  
  10. printf("2\n");  
  11. break;  
  12. case '3':  
  13. printf("3\n");  
  14. break;  

In this case, the input values are 1, 2, 3, and 4 respectively, and the corresponding output values are 1, 2, 3, and default 2 respectively (the line feed is omitted, and there is a line feed during actual operation ); in this case, the execution sequence and general labeling rules in A are followed.

C. There is no break in the last case.

 
 
  1. switch(c)  
  2. {  
  3. case '1':   
  4. printf("1\n");  
  5. break;  
  6. default:   
  7. printf("default\n");  
  8. break;  
  9. case '2':  
  10. printf("2\n");  
  11. break;  
  12. case '3':  
  13. printf("3\n");  
  14. // break;  

In this case, the input values are 1, 2, 3, and 4, and the corresponding output values are 1, 2, 3, and default. it can be seen that the actual running effect is not equivalent to moving the default Statement to the Final running effect. Otherwise, when the input is 3, the output should be 3 default.

D. Neither the default nor the last case has a break.

 
 
  1. switch(c)  
  2. {  
  3. case '1':   
  4. printf("1\n");  
  5. break;  
  6. default:   
  7. printf("default\n");  
  8. // break;  
  9. case '2':  
  10. printf("2\n");  
  11. break;  
  12. case '3':  
  13. printf("3\n");  
  14. // break;  

Based on the running results of the above three cases: A, B, and C, we can deduce the running results of the Case D. When the input values are 1, 2, 3, and 4, the output values are 1, 2, 3, and default 2. The results are the same as those of B.

The above is an introduction to the concepts related to C ++ switch-case statements.

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.