Switch Case in Java

It is very difficult and lengthy to write programs using if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths they are used as the replacement of if-then and if-then-else statements. Switch statements with String cases are supported in Java SE 7 but the earlier version of Java did not support String in switch/case. Switch statements works with type byte, short, char, or int.

Switch Case in Java

It is very difficult and lengthy to write programs using if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths they are used as the replacement of if-then and if-then-else statements. Switch statements with String cases are supported in Java SE 7 but the earlier version of Java did not support String in switch/case. Switch statements works with type byte, short, char, or int.

Switch Case in Java


It is very difficult and lengthy to write programs using if-then and if-then-else statements. As switch statement allows in-numerous possible execution paths they are used as the replacement of if-then and if-then-else statements.

Switch statements with String cases are supported in Java SE 7 but the earlier version of Java did not support String in switch/case.

Switch statements works with type byte, short, char, or int.

The execution of the switch statement takes place by comparing the value of the expression with each of the constants. The comparison of the values of the expression with each of the constants occurs after the case statements. Otherwise, the statements after the default statement will be executed.

A switch block is the body of a switch statement that evaluates its expression and executes all statements having similar case label

A statement in the switch block can be labeled with one or more case or default labels.

A break statement is used under the switch block to terminate a statement. This being an optional statement is used to jump at the end of the switch statement or if not used the execution of the statements associated with the next case will take place.

Example of Switch Statement in Java:
import java.io.*;

public class SwitchExample{
  public static void main(String[] args) throws Exception{
  int x, y;
  BufferedReader object = new BufferedReader
     (new InputStreamReader(System.in));
  System.out.println("Enter two numbers for operation:");
  try{
  x = Integer.parseInt(object.readLine());
  y = Integer.parseInt(object.readLine());
  System.out.println("1. Add");
  System.out.println("2. Subtract");
  System.out.println("3. Multiply");
  System.out.println("4. Divide");
  System.out.println("enter your choice:");
  int a= Integer.parseInt(object.readLine());
  switch (a){  
  case 1:
 System.out.println("Enter the number one=" + (x+y));
 break;
  case 2:
  System.out.println("Enter the number two=" + (x-y));
  break;
  case 3:
  System.out.println("Enetr the number three="+ (x*y));
  break;
  case 4:
  System.out.println("Enter the number four="+ (x/y));
  break;
  default:
  System.out.println("Invalid Entry!");
  }
  }
  catch(NumberFormatException ne){
  System.out.println(ne.getMessage() + " is not a numeric value.");
  System.exit(0);
  }

  }
}