Java Switch Statement

 

The switch statement is another conditional structure. It is good alternative of if else if ladder statement. It can be used easily when there are many choices available and only one should be executed.

SYNTAX:
The following is the syntax of switch statement:

Switch (expression)

{

case val1:

statements1;

break;

case val2:

statements2;

break;

:

:

case valn:

statement;

break;

default:

statements;

}

 

Flowchart of switch statement:

Consider the following diagram in which the structure of the switch statement is defined:

flowchart-of-switch-statement

Working switch statement:

Switch statement compares the result of a single expression with multiple cases. Expression can be any valid that results in integer or character value. The expression is evaluated at the top of switch statement and its result is compared with different cases. Each case label represents one choice. If the result matches with any case, the corresponding block of statements is executed. Any number of cases can be used in one switch statement.

The default label appears at the end of all case labels. It is executed only once when the result of expression does not match with any case label.

The break statement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and the control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.

NOTE: Remember that we cannot check a range of values using the switch statement like we did in the “if else if” ladder statement.

 

Example of switch statement in Java:

Consider the following code in which the switch statement is used to display the name of the fruits according to the number entered for example 1 for Apple, 2 for Mango and so on.

CODE:

public class ExampleSwitch {

public static void main (string [] args) {

{

int n = 3;

Switch (n)

{

case 1:

system. out. println (“Apple”);

break;

case 2:

system. out. println (“Mango”);

break;

case 3:

system. out. println (“Banana”);

break;

case 4:

system. out. println (“Peach”);

break;

case 5:

system. out. println (“Avocado”);

break;

case 6:

system. out. println (“Apricot”);

break;

case 7:

system. out. println (“Melon”);

break;

default:

system. out. println (“out of range”);

}

OUTPUT:

Banana

In the above example an integer type variable is declared named “n”. The variable n is initialized with value 3. The control enters the switch statement, the switch statement takes the value of n and compares it with all the cases when the value of n is matched with any of the case the statements in the body of that case will be executed and then the break statement is also executed. The message is printed and the control moves out of switch statement.

 

Java switch statement is fall through:

By Java switch statement is fall through we mean that if the break statement is not used after the statements of each case then all the cases will be executed even if the match is found.

Consider the following example in which we have used the same program as above but did not use the break statement at the end of each case:

CODE:

public class ExampleSwitch {

public static void main (string [] args) {

{

int n = 3;

Switch (n)

{

case 1:

system. out. println (“Apple”);

case 2:

system. out. println (“Mango”);

case 3:

system. out. println (“Banana”);

case 4:

system. out. println (“Peach”);

case 5:

system. out. println (“Avocado”);

case 6:

system. out. println (“Apricot”);

case 7:

system. out. println (“Melon”);

default:

system. out. println (“Out of range”);

}

OUTPUT:

Banana

Peach

Avocado

Apricot

Melon

Out of range

In the above example the first two cases are not executed because the condition is not true. Then the case three is matched and it is executed. Note that there was no break statement; hence all the other cases will be executed.