Detailed explanation of switch case usage in c language
switch means "switch", it is also a "choice" ” statement, but its usage is very simple. switch is a multi-branch selection statement. To put it simply, multiple branches are multiple ifs.
Recommended learning: C language video tutorial
Functionally speaking, the switch statement and the if statement can completely replace each other. But from a programming perspective, they each have their own characteristics, so so far it cannot be said that one can completely replace the other.
When there are relatively few nested ifs (within three), it will be simpler to write a program using if. However, when there are many branches selected, there will be many levels of nested if statements, resulting in a lengthy program and reduced readability. Therefore, C language provides switch statements to handle multiple branch selections. So if and switch can be said to have a clear division of labor. In many large projects, multi-branch selection situations are often encountered, so switch statements are still used quite often.
The general form of switch is as follows:
switch (表达式) { case 常量表达式1: 语句1 case 常量表达式2: 语句2 ┇ case 常量表达式n: 语句n default: 语句n+1 }
Instructions:
1) The "expression" in parentheses after switch must be of integer type. That is to say, it can be an int type variable, a char type variable, or directly an integer or character constant, even a negative number. But it must not be a real number. Float type variables, double type variables, and decimal constants are all not allowed. They are all syntax errors.
2) The case and default under switch must be enclosed by a pair of curly brackets {}.
3) When the value of the "expression" in parentheses after the switch is equal to the value of the "constant expression" after a case, the statement following the case will be executed. After executing the statement following a case, flow control transfers to the next case to continue execution. If you only want to execute this case statement and do not want to execute other cases, then you need to add break after this case statement to jump out of the switch statement.
Let me reiterate: switch is a "selection" statement, not a "loop" statement. Many novices think that break is a loop statement when they see it, because break generally gives us the impression of jumping out of a "loop", but break has another use, which is to jump out of switch.
4) If the values of the constant expressions in all cases are not equal to the value of the "expression" in the brackets after the switch, the statement after default will be executed. Default means "default". If default is the last statement, then there is no need to add break after it, because since it is the last statement, switch will naturally exit after execution.
5) The value of the "constant expression" behind each case must be different from each other, otherwise there will be conflicts with each other, and this writing will cause grammatical errors.
6) The "case constant expression" only serves as a statement label and is not used for judgment. When executing the switch statement, the matching entry label is found based on the value of the expression behind the switch, and execution starts from this label without further judgment.
7) The order of occurrence of each case and default does not affect the execution result. But from a reading perspective, it is best to write in alphabetical or numerical order.
8) Of course, you can also do without the default statement, just like if...else does not require the else statement at the end. But it’s best to add it, you can write nothing afterwards. This can prevent others from mistakenly thinking that you forgot to perform default processing, and can remind others that the switch is over.
But it should be noted that you can write nothing after default, but the following colon and semicolon must not be omitted. If they are omitted, it is a grammatical error. Many newbies can easily make mistakes here, either forgetting a semicolon or a colon, so be careful!
Let me write a program for you and let’s take a look at it.
# include <stdio.h> int main(void) { int val; //variable的缩写, “变量”的意思 printf("请输入您想去的楼层:"); scanf("%d", &val); switch (val) { case 1: printf("1层开!\n"); break; case 2: printf("2层开!\n"); break; case 3: printf("3层开!\n"); break; default: printf("该层不存在, 请重新输入\n"); } return 0; }
The last "This layer does not exist, please re-enter" in this program does not work now. This will have to be implemented using loops when learning loop statements.
The above is the detailed content of Detailed explanation of switch case usage in C language. For more information, please follow other related articles on the PHP Chinese website!