Friday, September 5, 2025
HomeLanguagesJavaFall Through Condition in Java

Fall Through Condition in Java

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("Lazyroar number 1"); 
          } 
          // Since break statement is missing
          // it will lead to fall through condition
          case 2:{ 
            System.out.println("Lazyroar number 2"); 
          } 
          case 3:{ 
            System.out.println("Lazyroar number 3"); 
          } 
          default :{ 
            System.out.println("This is default case"); 
          } 
        } 
    }
}


Output

Lazyroar number 1
Lazyroar number 2
Lazyroar 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
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS