Monday, November 18, 2024
Google search engine
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 Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments