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 :
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:
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.