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

Drag and Drop Collection View cell in IOS 11 &... Drag and Drop Collection View cell in IOS 11 & swift 4 In this tutorial we are explain how to drag and drop collection view cell in two collectio...
Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...
Emailing Phrases Most useful emailing phrases There are some standard phrases that are used in business or formal letters. These are essential to give a professional ...
Control Flow Statements | Swift tutorial Control Flow : Swift tutorial Swift Provides diffrent type of controll flow statements like: Loops Statements : Loops are used for perform the...
UIPopOverPresentationController In Swift UIPopOverPresentationController UIPopOverViewController in swift is writen in swift 3.1 and Use the xcode Version 8.3.2. For use this import the Pop...
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