Font Awesome Icons

Tutorial

Switch statement:

Java switch statement compares the value and executes one of the case blocks based on the condition. It is same as if…else if ladder. Below are some points to remember while working with switch statements:
case value must be of the same type as expression used in switch statement.
case value must be a constant or literal.
case values should be unique. If it is duplicate, then program will give compile time error.
The break statement is used inside the switch to terminate a statement sequence.
The break statement is optional. If omitted, execution will continue on into the next case.

class switch1{

 public static void main(String args[]){

   int i=2;

   switch(i){

     case 0:

     System.out.println(“i is 0”);

     break;

 

     case 1:

     System.out.println(“i is 1”);

     break;

 

     case 2:

     System.out.println(“i is 2”);

     break;

 

     case 3:

     System.out.println(“i is 3”);

     break;

 

     case 4:

     System.out.println(“i is 4”);

     break;

 

     default:

     System.out.println(“i is not in the list”);

     

 }

}

}