Tutorials

Java Decision Making Statement


java switch-case


We had already discussed the below given scenario when if-else-if statement falls in a situation where if-else-if ladder becomes very large. For example,
 
if(condition){
  //code to be executed
}else if(){
 //code to be executed
}else if(){
….
…. //100 or 1000 more ladders of if-else-if in between
….
}else{
 //code to be executed
}

 
In this case, the program will keep on testing the condition until test condition pass or it will go on till the last else statement is executed. 
 
Just imagine, how much overhead and time consuming this process might become?
 
Switch case solves this problem easily. In case you have to code logic where if-else-if ladder seems to be too much to code then simply go for Switch case.
 
Let’s see how using switch-case will solve our problem,
 
Syntax:
 
switch(expression){
  case constant1: statement1
  break;
  case constant2: statement2
  break;
      .
      .
  case constant N: statement N
  break;
  default: statement
  break;
  }

Example:
 
package switchcase;

public class GradeDescription {

    public static void main(String[] args) {
      int grade = 2;

      switch(grade){
        case 1: System.out.println("First division");
        case 2: System.out.println("Second division");
        case 3: System.out.println("Third division");
        case 4: System.out.println("Fail");
        default: System.out.println("Didn't appear in exam");
      }
   }
}

OUTPUT:
Second division
Third division
Fail
Didn't appear in exam
 

How above code works?

Here we have entered manually the value of grade = 2
 
Switch case checks that grade is 2 so it transfers control directly to case 2. And it executes what is there in body of case 2. Here we have Second division which is printed first.
 
And then we see that all the case below case 2 is also getting executed. 
 
But in reality this should not happen (until your intentions are so). One value should only fall into one particular case and only body of that case should get executed. If we passed value as 2 then only case 2 should get executed. 
 
But here we are not getting this. 
 

Why other cases in above scenario is also getting executed?

We haven’t used break statement after case body ends. This is the reason that all the cases after the case 2 is getting executed.
 
Use break statement to achieve our desired result as shown in below code snippet.
 
package switchcase;

public class GradeDescription {

   public static void main(String[] args) {
     int grade = 2;

     switch(grade){
      case 1: System.out.println("First division");
      break;
      case 2: System.out.println("Second division");
      break;
      case 3: System.out.println("Third division");
      break;
      case 4: System.out.println("Fail");
      break;
      default: System.out.println("Didn't appear in exam");
    }
  }
}

OUTPUT
Second division
 
Congratulations!
We achieved our result.  You will learn in depth about break in the break statement section of this tutorial.
 


Please Share this page
Views : 67
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!