C# switch Statement
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;
case constant-expression :
statement(s);
break;
/* You can have any number of case statements */
default : /* 可选的 */
statement(s);
break;
}
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 switch and must be a constant.
- 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 the case statement is empty, it does not need to contain break, control flow will continue to the following cases until a break is encountered.
- C# does not allow execution to continue from one case section to the next. If a case statement has already executed, it must include break or another jump statement.
- A switch statement can have an optional default statement at the end of the switch. The default statement is used to execute a task when none of the above cases are true. default also needs to contain break statement, which is a good practice.
- C# does not support explicitly falling through from one case label to another. To make C# support explicit fall-through from one case label to another, you can use goto a switch-case or goto default.
Flowchart
Example
The following example is used to determine the current day of the week:
Example
using
System
;
namespace
MyApplication
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
int
day
=
4
;
switch
(
day
)
{
case
1
:
Console
.
WriteLine
(
"Monday"
)
;
break
;
case
2
:
Console
.
WriteLine
(
"Tuesday"
)
;
break
;
case
3
:
Console
.
WriteLine
(
"Wednesday"
)
;
break
;
case
4
:
Console
.
WriteLine
(
"Thursday"
)
;
break
;
case
5
:
Console
.
WriteLine
(
"Friday"
)
;
break
;
case
6
:
Console
.
WriteLine
(
"Saturday"
)
;
break
;
case
7
:
Console
.
WriteLine
(
"Sunday"
)
;
break
;
}
}
}
}
The execution result varies depending on the day; the result on the day I ran it was:
Thursday
The following example determines a student's score and includes default Statement:
Example
using
System
;
namespace
DecisionMaking
{
class
Program
{
static
void
Main
(
string
[
]
args
)
{
/* Local variable definition */
char
grade
=
'B'
;
switch
(
grade
)
{
case
'A'
:
Console
.
WriteLine
(
"Excellent!"
)
;
break
;
case
'B'
:
case
'C'
:
Console
.
WriteLine
(
"Well done"
)
;
break
;
case
'D'
:
Console
.
WriteLine
(
"You passed"
)
;
break
;
case
'F'
:
Console
.
WriteLine
(
"Better try again"
)
;
break
;
default
:
Console
.
WriteLine
(
"Invalid score"
)
;
break
;
}
Console
.
WriteLine
(
"Your score is {0}"
, grade
)
;
Console
.
ReadLine
(
)
;
}
}
}
When the code above is compiled and executed, it produces the following result:
做得好
您的成绩是 B
C# Decision Making