Switch Statement

  • A switch statement test a variable for equality against a list of values.
  • Each value is called a case , and the variable being switched on is checked for each case.
  • Syntax:-
switch(expression){
      case value1:
           //statement
      break;
     case value2:
          //statement
     break;
    .....
    .....
    default:
         //statement
}
  • A default case is used when no match is found( none of case is matched).
  • No break is needed in default case , as it always the last statement int the switch.

Example:-

int day = 2;
switch(day){
     case 1:
        System.out.println("Monday");
     break;
     case 2:
        System.out.println("Tuesday");
     break;
     case 3:
        System.out.println("Wednesday");
     break;
     default:
        System.out.println("No match");
}