Home > Backend Development > C#.Net Tutorial > What is the usage of switch in C language?

What is the usage of switch in C language?

coldplay.xixi
Release: 2020-07-27 13:08:04
Original
41417 people have browsed it

The usage of switch in C language is: 1. The [expression] in brackets after switch, the ANSI standard allows it to be of any type; 2. When the value of the expression matches the constant expression after a case When the values ​​are equal, the statement following this case is executed; otherwise, the statement following default is executed.

What is the usage of switch in C language?

The usage of switch in C language is:

Function: The switch statement is a multi-branch selection statement. Used Implement a multi-branch selection structure. The if statement has only two branches to choose from, but multi-branch selection is often used in practical problems. For example, student performance classification (90 is "A", etc., 80-89 is divided into 'B' etc., 70-90 is divided into 'C', etc...). Of course, these can be handled with nested if statements, but if there are many branches, there will be many layers of nested if statements, and the program The redundancy is long and the readability is reduced. The C language provides the switch statement to directly handle multi-branch selections, which is equivalent to the CASE statement in the PASCAL language.

Form: switch (expression)

{
case 常量表达式 1:语句 1
case 常量表达式 2:语句 2
.
.
.
case 常量表达式 n:语句 n
default:语句 n+1
}
Copy after login

For example, if you want to print out the hundred-point score segment according to the test score level, you can use the switch statement:

switch(grade)
{
case 'A':printf("85-100\n");
case 'B':printf("70-84\n");
case 'C':printf("60-69\n");
case &#39;D&#39;:printf("<60\n");
default:printf("error\n");
}
Copy after login

Description:

(1) The "expression" in the brackets after the switch, ANSI The standard allows it to be of any type.

(2) When the value of the expression is equal to the value of the constant expression following a case, the statement following the case will be executed. If the constants in all cases If the value of the expression does not match the expression, the statement after default will be executed.

(3) The value of the constant expression in each case must be different from each other, otherwise there will be conflicts. Phenomenon (there are two or more execution plans for the same value of an expression).

(4) The order in which defaults appear in each case does not affect the execution results. For example, "default: ..." can appear first, then "case 'D': ...", and then "case 'A': ...".

(5) After execution After the statement following a case, the flow control is transferred to the next case to continue execution. The "case constant expression" only serves as a statement label, and does not perform conditional judgment there. When executing the switch statement, according to the expression following the switch If the matching entry label is found, the execution will start from this label without further judgment. For example, in the above example, if the value of grade is equal to 'A', it will be continuously output:

85-100
70-84
60-69
<60
error
Copy after login

Therefore, it should After executing a case branch, make the process jump out of the switch structure, that is, terminate the execution of the switch statement.

You can use a break statement to achieve this purpose. Rewrite the above switch structure as follows:

switch(grade)
{
case &#39;A&#39;:printf("85-100\n"); break;
case &#39;B&#39;:printf("70-84\n"); break;
case &#39;C&#39;:printf("60-69\n"); break;
case &#39;D&#39;:printf("<60\n"); break;
default:printf("error\n");
}
Copy after login

The last branch (default) does not need to add a break statement. If the value of grade is 'B', only "70-84" will be output.

Although there is more than one execution statement behind the case, it can There is no need to enclose it in curly brackets, all execution statements following this case will be automatically executed sequentially. Of course, curly brackets can also be added.

(6) Multiple cases can share a set of execution statements, for example:

case &#39;A&#39;:
case &#39;B&#39;:
case &#39;C&#39;: printf(">60\n");break;
.
.
Copy after login

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

Related learning recommendations: C video tutorial

The above is the detailed content of What is the usage of switch in C language?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template