Switch Statement in JAVA

Switch is conditional control statement in java. Switch statement check expression where to switch and execute loop of cases.
Switch Statement is like else-if statement of small version. In else-if condition we have to write more code and conditions, but in switch case we have to just pass expression in switch(Expression)

Example of switch case statement

public class SwitchStatement {
  public static void main(String[] args)
  {
      int a=3;

      switch (a) {
          case 1:
          {
             System.out.println("case 1");
             break; // break the loop
          }
          case 2:
          {
              System.out.println("case 2");
              break;  // break the loop
          }
          case 3:
          {
              System.out.println("case 3");
              break;  // break the loop
          }
          case 4:
          {
              System.out.println("case 4");
              break;  // break the loop
          }
          default:
          {
              System.out.println("Default Case");
              break;  // break the loop
          }
    }
  }
}

Output

case 3

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post