C switch Statement
The switch statement is a restricted control-flow statement used to execute different blocks of code based on the value of an expression.
A switch The statement allows testing a variable against several values; each value is called a case, and the tested variable is compared with each switch case .
Syntax
In the C language switch statement syntax:
switch
(
expression
)
{
case
constant
expression
:
statement
(
s
)
;
break
;
/*
Optional
*/
case
constant
expression
:
statement
(
s
)
;
break
;
/*
Optional
*/
/*
You can have any number of case statements
*/
default
:
/*
Optional
*/
statement
(
s
)
;
}
Notes on the switch statement:
- The value of the expression after switch is compared with the constant value after each case until a match is found or default is reached (if present).
- If a match is found, the code block after the corresponding case is executed, and then the switch statement is exited.
- If no match is found and a default exists, the code block after default is executed.
- If no match is found and there is no default, the entire switch statement is skipped to its end.
switch statements must follow these rules:
-
Type of the switch expression: The expression in a switch statement must be of an integer type (char, short, int, or enum), or an expression that can be implicitly converted to an integer type.
-
Uniqueness of case labels: In a switch statement, each case label must be unique; duplicate values are not allowed.
-
Optionality of the default case: The default label in a switch statement is optional. If no case label matches, the code block under the default label is executed (if present).
-
Constant values in case labels: The value after a case label must be a constant expression, meaning its value can be determined at compile time.
-
Order of case labels: The order of case labels in a switch statement does not matter; they can be written in any order. The program matches them in the order in which the case labels appear.
-
Use of the break statement: A break statement is usually needed at the end of the code block for each case label to terminate the switch statement. Without a break statement, the program continues executing the code in the next case label until a break statement is encountered or the switch statement ends.
-
Nesting of switch statements: A switch statement can be nested inside another switch statement, but you should pay attention to code readability and complexity.
-
Range of case labels and expressions: The case labels of a switch statement can be integer constant expressions, but not floating-point numbers or strings.
Flowchart
Example
#include
<
stdio.h
int
main
(
)
{
/*Local variable definition
*/
char
grade
= '
B
';
switch
(
grade
)
{
case
'
A
' :
printf
(
"
Great!
\
n
"
)
;
break
;
case
'
B
' :
case
'
C
' :
printf
(
"
Well done
\
n
"
)
;
break
;
case
'
D
' :
printf
(
"
You passed
\
n
"
)
;
break
;
case
'
F
' :
printf
(
"
Better try again
\
n
"
)
;
break
;
default
:
printf
(
"
Invalid score
\
n
"
)
;
}
printf
(
"
Your grade is %c
\
n
"
,
grade
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
做得好
您的成绩是 B
C Judgment