Website For Developers

Switch Statement in Java


There are two types of decision-making in the Java statements.
1. if statement
2. switch statement


Switch Statement


Syntax

switch (Condition){
case value :
break;
case value :
break;
default :
}

Example of switch statement in Java

public class SwitchExample {

   public static void main(String[] args) {
     int age = 20;
     switch(age)
     {
       case 10 :
         System.out.println( "Hello Kid!" );
         break;
       case 20 :
         System.out.println( "Hello Young Boy!" );
         break;
       case 30 :
         System.out.println( "Hello Man!" );
         break;
       default :
         System.out.println( "Invalid age" );
     }
   }
}

Output

Hello Young Boy!