C++ switch Statement
In C++, the switch statement is used to execute different blocks of code based on different conditions; it is usually used to replace a series of if-else statements, making the code clearer and more readable.
A switch The statement allows testing a variable against multiple values. Each value is called a case, and the tested variable is checked against each switch case .
Syntax
In C++ 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
)
;
}
switch statements must follow these rules:
- switch The expression in the statement must be an integral or enumeration type, or a class type where the class has a single conversion function to convert it to an integral or enumeration type.
- There can be any number of case statements in a switch. Each case is followed by a value to compare and a colon.
- The case constant-expression must have the same data type as the variable in the switch, and must be a constant or a literal.
- When the tested variable equals the constant in the case, the statements following the case are executed until break is encountered.
- When break is encountered, the switch terminates and control flow jumps to the line after the switch statement.
- Not every case needs to contain break. If a case statement does not contain break, control flow will continue to the following cases until a break is encountered.
- A switch statement can have an optional default case, appearing at the end of the switch. The default case can be used to perform a task when none of the above cases are true. The break in the default case break statement is not required.
Flowchart
Example
Example
#include
int
main
(
)
{
int
day
=
4
;
switch
(
day
)
{
case
1
:
std
::
cout
<<
"Monday"
<<
std
::
endl
;
break
;
case
2
:
std
::
cout
<<
"Tuesday"
<<
std
::
endl
;
break
;
case
3
:
std
::
cout
<<
"Wednesday"
<<
std
::
endl
;
break
;
case
4
:
std
::
cout
<<
"Thursday"
<<
std
::
endl
;
break
;
case
5
:
std
::
cout
<<
"Friday"
<<
std
::
endl
;
break
;
case
6
:
std
::
cout
<<
"Saturday"
<<
std
::
endl
;
break
;
case
7
:
std
::
cout
<<
"Sunday"
<<
std
::
endl
;
break
;
default
:
std
::
cout
<<
"Invalid day"
<<
std
::
endl
;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Thursday
char type in switch statement:
Example 2
#include
<
iostream
using
namespace
std;
int
main
(
)
{
// local variable declaration
char
grade
= '
D
';
switch
(
grade
)
{
case
'
A
' :
cout
<<
"
Great!
"
<<
endl
;
break
;
case
'
B
' :
case
'
C
' :
cout
<<
"
Well done
"
<<
endl
;
break
;
case
'
D
' :
cout
<<
"
You passed
"
<<
endl
;
break
;
case
'
F
' :
cout
<<
"
Better try again
"
<<
endl
;
break
;
default
:
cout
<<
"
Invalid score
"
<<
endl
;
}
cout
<<
"
Your score is
"
<<
grade
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
您通过了
您的成绩是 D
Notes:
breakStatement: Eachcaseat the end of a statementbreakstatement is necessary to break out ofswitchstatement. If it is missingbreak, the program will continue to execute the nextcase, which is called "fall-through".defaultStatement:defaultstatement is optional, but is usually used when allcasedo not match, to handle all unlisted cases.- Expression types:
switchExpressions are usually of integer or enumeration type; floating-point and string types are not allowed.
Sometimes we may intentionally use fall-through; the following code will execute the same code block if number is 2 or 3.
Example
#include
int
main
(
)
{
int
number
=
2
;
switch
(
number
)
{
case
1
:
std
::
cout
<<
"Number is 1"
<<
std
::
endl
;
break
;
case
2
:
case
3
:
std
::
cout
<<
"Number is 2 or 3"
<<
std
::
endl
;
break
;
default
:
std
::
cout
<<
"Number is not 1, 2, or 3"
<<
std
::
endl
;
}
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Number is 2 or 3
C++ Decision Making