Switch Case Statement

  • The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
  • Switch case statement is used when we have number of options (or choices) and we may need to perform a different task for each choice.
Syntax
switch (variable or an integer expression)
{
     case constant:
     //Java code
     ;
     case constant:
     //Java code
     ;
     default:
     //Java code
     ;
}
Flow Diagram For Switch Cases:
Some Important rules for switch statements :
  • Duplicate case values are not allowed.
  • The value for a case must be the same data type as the variable in the switch.
  • The value for a case must be a constant or a literal.Variables are not allowed.
  • The break statement is used inside the switch to terminate a statement sequence.
  • The break statement is optional. If omitted, execution will continue on into the next case.
  • The default statement is optional, and it must appear at the end of the switch.
Example:
public class SwitchCaseExample1 {

   public static void main(String args[]){
     int num=2;
     switch(num+2)
     {
        case 1:
      System.out.println("Case1: Value is: "+num);
        case 2:
      System.out.println("Case2: Value is: "+num);
        case 3:
      System.out.println("Case3: Value is: "+num);
        default:
      System.out.println("Default: Value is: "+num);
      }
   }
}

Output:
Default: Value is: 2

Break Statement used in Switch Case:

Break statements are used when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the execution flow would directly come out of the switch, ignoring rest of the cases

Example:
public class SwitchCaseExample2 {

   public static void main(String args[]){
      int i=2;
      switch(i)
      {
     case 1:
       System.out.println("Case1 ");
       break;
     case 2:
       System.out.println("Case2 ");
       break;
     case 3:
       System.out.println("Case3 ");
       break;
     case 4:
           System.out.println("Case4 ");
           break;
     default:
       System.out.println("Default ");
      }
   }
}
Output:
Case2

Related Post

Create UITableView (Table View) By the use of stor... How to use UITableView in Above IOS 8 and Swift (UITableView) In this tutorial we are study how to use the TableView in Swift. One of the most common...
Array An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. The first el...
Introduction to JVM Introduction to JVM JVM stands for Java Virtual Machine. It works as an engine that derives the Java Code. The JVM does not understand Java ty...
Create Gradient layer by the use of CAGradientLaye... Create Gradient layer by the use of CAGradientLayer Mostly developers are use the gradient (Combination of colors) colors images for make app more ef...
Compile C/C++ program (Dev-C/C++) what is Dev-C/C++? Dev-C/C++, developed by Bloodshed Software, is a fully featured graphical IDE(Integrated Development Environment),which ...
Strings String can be represented as a single-dimensional character type array. Strings in C are group of characters, digits, and symbols enclosed in quotatio...
Author

Write A Comment