Switch Statement In Java

Switch Statement ?

It is mainly used in multi-option programs. Switch case statement is used when we have multiple conditions and we need to perform different action based on the condition. When we have multiple conditions and we need to execute a block of statements when a particular condition is satisfied.


It is mainly used in multi-option programs.

The switch statement evaluates an expression, then attempts to match the result of the expression to one of several possible cases.

Each case contains a value and a list of statements.

The flow of control transfer to statement associated with the first case value that matches.

the case value can be of any data type.

Often a break statement is used as the last statement in each case's statement list.

A break statement causes control to transfer to the end of the switch statement.

If a break statement is not used, the flow of control will continue executing the next case also.

A switch statement can have an optional default case.


Example 1:



Output :

 x has a value of 0!

Example 2:



Output :

you have selected : 12