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

Java switch case Statement

Java switch case Statement

The switch case statement determines whether a variable is equal to one of a series of values, each of which is called a branch.

Syntax

The syntax of the switch case statement is as follows:

switch

(

expression

)

{

case

value

:

//

Statement

break

;

//

Optional

case

value

:

//

Statement

break

;

//

Optional

//

You can have any number of case statements

default

:

//

Optional

//

Statement

}

The switch case statement has the following rules:

When switch case executes, it first performs matching; on a successful match it returns the value of the current case, then depending on whether there is a break, decides whether to continue outputting or to exit the judgment.

Example

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

//

char grade = args[0].charAt(0);

char

grade

=

'

C

'

;

switch

(

grade

)

{

case

'

A

'

:

System

.

out

.

println

(

"

Excellent

"

)

;

break

;

case

'

B

'

:

case

'

C

'

:

System

.

out

.

println

(

"

Good

"

)

;

break

;

case

'

D

'

:

System

.

out

.

println

(

"

Pass

"

)

;

break

;

case

'

F

'

:

System

.

out

.

println

(

"

You need to work harder

"

)

;

break

;

default

:

System

.

out

.

println

(

"

Unknown grade

"

)

;

}

System

.

out

.

println

(

"

Your grade is

"

grade

)

;

}

}

The compilation and execution results of the above code are as follows:


良好
你的等级是 C

If a case block has no break statement, the JVM does not output the return value of every case in sequence; instead it continues matching, and if no match succeeds it returns the default case.

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

i

=

5

;

switch

(

i

)

{

case

0

:

System

.

out

.

println

(

"

0

"

)

;

case

1

:

System

.

out

.

println

(

"

1

"

)

;

case

2

:

System

.

out

.

println

(

"

2

"

)

;

default

:

System

.

out

.

println

(

"

default

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


default

If a case block has no break statement, after a successful match, starting from the current case, the values of all subsequent cases will be output.

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

i

=

1

;

switch

(

i

)

{

case

0

:

System

.

out

.

println

(

"

0

"

)

;

case

1

:

System

.

out

.

println

(

"

1

"

)

;

case

2

:

System

.

out

.

println

(

"

2

"

)

;

default

:

System

.

out

.

println

(

"

default

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


1
2
default

If the currently matched case block has no break statement, starting from the current case, the values of all subsequent cases will be output; if a subsequent case block has a break statement, the judgment is exited.

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

i

=

1

;

switch

(

i

)

{

case

0

:

System

.

out

.

println

(

"

0

"

)

;

case

1

:

System

.

out

.

println

(

"

1

"

)

;

case

2

:

System

.

out

.

println

(

"

2

"

)

;

case

3

:

System

.

out

.

println

(

"

3

"

)

;

break

;

default

:

System

.

out

.

println

(

"

default

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


1
2
3