Fall Through in Java
In Java, fall through is associated with the Java switch case. In this section, we will discuss the fall-through in the Java switch case with an example.
What is fall through?
It is a condition in which each case does not have any break statement. Note that in a switch statement, it is not necessary to have a break statement. Using the break statement is optional but using a break statement is a good habit. In this situation, the control flow will fall through until a break statement is found. It is the rarest situation.
The break statement denotes the end of the case. Omitting a break leads to a program execution continuing into the next case and onwards till a break statement is encountered or the end of the switch is reached.
In simple words, we can say that a switch statement will fall through if it executes all the statements after the first match. Note that no break statement is used after each case.
Let's understand it through a Java program.
Java Fall Through Examples
We are Indian. You are my crony friend. Our Indian flag has tri-color. It is the default statement.
In the above program, we observe that the specified string "we" matched with the second case and execute all the cases after the second case statement because the break statement is not used after each case. In programming terms, the situation is called a fall-through case of the switch statement.
Let's see another Java program for the same.
It is a vowel. The alphabet 'a' is a vowel.