How to use switch statement in java ?

How to use switch statement in java ?

Previous Home Next

 

This example shows how the case statements are executed.

Switch statement has cases .if the case is match with the parameter pass to the switch block.
then it prints it rest will terminate.
 
  int day = 3;

switch (day)

we take day as variable and pass it to the switch block.
we assign value 3 to the variable day
output would be Wednesday as it is the third day of the week.

 


public class SwitchExample {


public static void main(String[] args) {
int day = 3;

switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
case 4: System.out.println("Thursday"); break;
case 5: System.out.println("Friday"); break;
case 6: System.out.println("Saturday"); break;
case 7: System.out.println("Sunday"); break;
default: System.out.println("Invalid Day.");break;
}
}
}




Wednesday
Previous Home Next