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:
- 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 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
- 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 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
- 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 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.
- 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 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.
- 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;
- }
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.