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

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:

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:

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