Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# switch Statement

C# switch Statement

C# Decision C# Decision Making

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:

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 C# Decision Making