Tuesday, July 19, 2011

JAVA Programming : Switch Statement

JAVA Switch Statement


Switch Statement is also a control flow statement like If statement. But Switch statement is a bit different from If statement, because an If statement can only choose one course of action form multiple choices but switch statement can have multiple outcomes. We will explain it below. A switch statement works only for convertible int values (like byte, int, short, char) and enum constants.

Syntax of Switch Statement:


 switch(expression){
      case constantOne:
        //set of statement   
      case constantTwo:
        //set of statement
      case constantThree:
        //set of statement
      default:
        //set of statement
 }



Now lets see the functionality of Switch statement: In a switch statement the flow of execution starts from top to down i.e. the first case will be evaluated and the second case and so on and wherever it finds the matching case it will execute the associated code block and the remaining code block till the end of switch. Let’s understand with an example:


public class SwitchStatement {
  public static void main(String args[]){   
    int a=2;
    switch (a){
    case 1:
      System.out.println("one");
    case 2:
      System.out.println("two");
    case 3:
      System.out.println("three");
    case 4:
      System.out.println("four");
    }  
  }
}



The output of this program is :
     two
     three
     four

Here we can see that after finding the matching case, the associated code block and all the code blocks after that will get executed till the end of switch statement. This kind of situation is called fall through. To overcome this problem we can use break statement.

Break: Whenever the program finds a break statement, the execution of program jumps out of switch. Break statement is also used in loops like for and while. We discusses about it later.


public class SwitchStatement {
  public static void main(String args[]){   
    int a=2;
    switch (a){
    case 1:
      System.out.println("one");
      break;
    case 2:
      System.out.println("two");
      break;
    case 3:
      System.out.println("three");
      break;
    case 4:
      System.out.println("four");
      break;
    }  
  }
}



This is same example as above but with a slight change. We have used the break statement in all cases. So, the output of this program will be:

    two

This is because when it executes the case 2 block it will encounters a break statement and the execution of program will move out of switch immediately without executing remaining cases. This way it avoids the fall through situation. 

Default: Default works same as Else in If-Else statement. In switch statement if there is no matching case the default case will get executed. Here is an example: 


import java.util.*;
public class SwitchStatement {
  public static void main(String args[]){
    int option=0;
    String week="";
    Scanner scn = new Scanner(System.in);
 
    System.out.println("Enter a Weekday")
    option=scn.nextInt();
    
    switch (option){
    case 1:
      System.out.println("Sunday");
      break;
    case 2:
      System.out.println("Monday");
      break;  
    case 3:
      System.out.println("Tuesday");
      break;
    case 4:
      System.out.println("Wednesday");
      break;
    case 5:
      System.out.println("Thursday");
      break;
    case 6:
      System.out.println("Friday");
      break;
    case 7:
      System.out.println("Saturday");
      break;
    default:
      System.out.println("Wrong option");
      break;
    }  
  }
}



Output of Program: When there is a matching switch case.

When there is no matching case the default code block will execute.

2 comments:

  1. Howdy! Do you happen to own any journalism experience or it is just a pure natural gift of yours? Thank you so much in advance for your answer.

    ReplyDelete
  2. Great blog! The explanation of the Switch Statement in Java is clear and concise. The code examples provided help illustrate the concept effectively. The use of break statements to avoid fall-through situations is well explained, ensuring that readers understand the importance of using breaks within switch cases.
    Online Java Course in Gwalior is the best place to know more about java.

    ReplyDelete