Fall Through Condition in Java

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 : 
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");           }         }     }}
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");                     }     }}}
Consonant
Java-Control-Flow
Picked
Technical Scripter 2020
Java
Technical Scripter
Java-Control-Flow
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java