JAVA- Switch statement

The problem in if-statement is the complexity to read and follow. Even it may be confuse the programmer who designed it. The switch statement tests the value of the variable with a list of case values and when a match is found, and then the block of that particular case will be executed.

Syntax:

switch(expression)

{

case: 1

block-1

break;

case: 2

block-2

break;

default:

default-block

break;

}

As is the above syntax we will give the expression in the form of variable. And according to that given value it will executes the case. If any of the case is not matched then the default case will be executed. And we have used the break statement which will help to exit from the switch statement. The break will skip the rest of the cases for comparisons, and this saves execution time.

Leave a Reply

Your email address will not be published. Required fields are marked *