Skip to content
Related Articles

Related Articles

Improve Article

Fall Through Condition in Java

  • Last Updated : 11 Feb, 2021

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. Basically, the expression can be a byte, short, char, and int primitive data types. Beginning with JDK7, it also works with enumerated types ( Enums in java), the String class, and Wrapper classes.

Flow Diagram of Switch-case : 

Flow Diagram of Switch-Case statement

Fall through condition: This condition occurs in the switch control statement when there is no break keyword mention for the particular case in the switch statement and cause execution of the cases till no break statement occurs or exit from the switch statement. This condition has its own advantage and disadvantage and it totally depends upon the type of operation we want in our program. 

Fall through condition in below program:



Java




// Java program to showcase the fall through condition
  
import java.util.*;
import java.io.*;
class GFG{ 
public static void main(String[] args) {
        int gfg = 1
    
        switch ( gfg ){ 
          case 1:{ 
            System.out.println("GeeksforGeeks number 1"); 
          
          // Since break statement is missing
          // it will lead to fall through condition
          case 2:{ 
            System.out.println("GeeksforGeeks number 2"); 
          
          case 3:{ 
            System.out.println("GeeksforGeeks number 3"); 
          
          default :{ 
            System.out.println("This is default case"); 
          
        
    }
}
Output
GeeksforGeeks number 1
GeeksforGeeks number 2
GeeksforGeeks number 3
This is default case

Disadvantage: In the above program, we forgot to mention the break statement in the switch statement that leads to executing all the cases even they didn’t match with the matched value. This situation creates a major problem in the programs. So we have to use the break keyword for every case in the switch statement in order to overcome this situation this is the disadvantage of the fall through condition. 

Advantage of fall through condition:

We know very well that the switch statement works for a single variable or expression and in many cases when there are same output many values and here fall through condition plays an important role in this case and makes the program efficient by reducing comparisons

Example: Java total number of days in a month, Java Program to check whether the character is a vowel or not, etc.

Java




// Java Program to check whether the character is a vowel or not
  
import java.util.*;
import java.io.*;
class GFG{ 
public static void main(String[] args) {
          
        char ch='f'
    
        switch ( ch ){ 
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':System.out.println("Vowel");
        break;
        default :{ 
            System.out.println("Consonant"); 
            
        
    }
}}
Output
Consonant

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :