The use of switch in C language

1. The [expression] in the parentheses behind switch, which is permitted by the ANSI standard for any type;

2. If the value of an expression is equal to the value of a constant expression after a case, the statement after that case is executed; otherwise, the statement after default is executed.

** Switch statements are multi-branch select statements. If statements have only two branches to choose from, and multi-branch selection is often used in practical problems. For example, student grades are classified (90 as “A”,80-89 as “B”,70-90 as “C”……) Of course, all of these can be handled by nested if statements, but if there are more branches, the nested if statements will have more layers, and the program will be bulky and less readable. C provides multiple branch selection directly outside the switch statement, which is equivalent to the CASE statement in PASCAL.

! [](https://upload-images.jianshu.io/upload_images/24563956-229599d57d9c3bc0.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Form: switch(expression)

! [](https://upload-images.jianshu.io/upload_images/24563956-14ad1e01bc9471b1.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

For example, to print a percentage score segment according to the grade of the test score, you can use the switch statement:

! [](https://upload-images.jianshu.io/upload_images/24563956-7d96a14a241cc9e3.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Description:

(1) The “expression” in bracket after switch, which the ANSI standard allows for any type.

(2) When the value of the expression is equal to the value of the constant expression behind a certain case, the statement after this case is executed. If the value of the constant expression in all cases does not match the value of the expression, the statement after default is executed.

! [](https://upload-images.jianshu.io/upload_images/24563956-a7fc7a7867dd35db.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

(3) The values of constant expressions in each case must be different, otherwise there will be contradictory phenomena (for the same value of the expression, there are two or more execution schemes).

(4) The order of occurrence of each case and default does not affect the execution result. For example, you can start with “default:…” Case ‘D’:… And then “Case ‘A’:…” .

(5) After the execution of a statement following a case, flow control is transferred to the next case to continue execution.” Case constant expression “is only used as a statement label, not a condition judgment at this point. When the sWITH statement is executed, the matching entry label is found according to the value of the expression after the switch, and the execution continues from this label without judgment. For example, in the above example, if the value of grade is equal to ‘A’, it will print continuously:

! [](https://upload-images.jianshu.io/upload_images/24563956-ddd10daaf4b316d5.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

Therefore, the process should be taken out of the Switch structure after executing a case branch, terminating the execution of the switch statement.

You can do this with a break statement. Rewrite the switch structure as follows:

! [](https://upload-images.jianshu.io/upload_images/24563956-099cbced4ebac3ce.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The last branch (default) can be used without a break statement. If grade has a value of ‘B’, only “70-84” is printed.

In the case behind although contains more than one execution statement, but can not use parentheses, will automatically order the execution of all statements after the case. You can also add curly brackets.

(6) Multiple cases can share a group of execution statements, such as:

! [](https://upload-images.jianshu.io/upload_images/24563956-1c990c99ea6e5783.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

The same set of statements is executed when grade has the value ‘A’,’B’, or ‘C’.

! [](https://upload-images.jianshu.io/upload_images/24563956-6cecabc0adfd52ce.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

2. Switch statement use skills

1) Minimize the number of case statements

According to the C89 standard of C language, a switch statement should support at least 257 case statements. In THE C99 standard, the number of branches is required to be increased to 1023. In practical programming applications, the number of case statements should be reduced as much as possible in order to improve the readability and execution efficiency of programs. Convert long Switch statements into nested switch statements as much as possible. That is, you can place frequently executed statements in a case statement as the outermost layer of nested Switch statements. Place the less frequently executed case statements in another switch statement, inside the nested switch statement.

2) Do not forget to add a break statement at the end of the case statement

In switch statements, don’t forget to add a break statement at the end of each case statement, otherwise multiple branches will overlap unless you intend to.

3) Pay attention to the ordering of case statements

Usually in alphabetical or numeric order; If there are multiple normal and abnormal statements in the switch statement, the normal statement should be listed first. You can also sort by execution frequency. If you can predict the approximate execution frequency of each case statement, you can rank the statements with the highest frequency first.

! [](https://upload-images.jianshu.io/upload_images/24563956-9ead1113e2f159e7.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

If you want to improve your programming ability, learn c /C++ programming knowledge! Then you are in luck

Join C /C++ penguin Circle 686521686 and share some interesting things you may not know.