×

Fall through in Java

In this article, you will be acknowledged about the fall through condition in Java along with an example program.

Fall through

It is a situation where there isn't a break statement in any of the cases. Although a break statement is not required in a switch statement. Implementing a break statement is optional, but it could be makes a great sense. Unless a break statement is found, the control flow in this case will be disturbed. It happens quite rarely.

The break statement indicates that the case is over. A program will continue to run until a break statement is discovered or the completion of the transition is reached if a break statement is not included.

If a switch statement evaluates every statement that follows the first test case, we can conclude that it ended in failure. Notably, there is no break statement after each case.

Let us understand this condition with the syntax and an example program.

Syntax

switch(expression)   
{  
case 1:  
//program statements  
case 2:  
//program statements  
case 3:  
//program statements  
case n:  
//program statements  
default:  
// program statements  
}

Example(1):

File name: Fallthrou1.java

// Java program that depicts the flow and implementation of fall through condition
import java.util.*;
import java.io.*;
class Fallthrou1{
public static void main(String[] args) {
int lte = 1;

switch ( lte ){
case 1:{
System.out.println("I am an intern");
}
//Because there is no break statement, a fall through situation will occur.
case 2:{
System.out.println("I am a student");
}
case 3:{
System.out.println("I am a java aspirant");
}
default :{
System.out.println("I am a human being");
}
}
}
}

Output

I am intern
I am a student
I am a java aspirant
I am a human being

Example(2);

File name: Fallthrou2.java

// Java program that would represent the flow of fall through condition in java
import java.util.*;
import java.io.*;


public class Fallthrou2  
{  
public static void main(String arg[])  
{  
String sky="She ";  
switch(sky)  
{  
case "she":  
System.out.println("She is a good woman.");  
case "them":  
System.out.println("She is a wise woman.");  
case "me":  
System.out.println("She is a good friend of mine.");  
case "ours":  
System.out.println("She helped me through hard times.");  
default:  
System.out.println("We are best friends.");  
}      
}  
}  

Output

She is a good woman.
     She is a wise woman.
     She is a good friend of mine.
     She helped me through hard times.
     We are best friends.

Real-time applications

The fall through condition in java can be implemented to solve a few real- time situations like

  • If a character is a vowel or a consonant.
  • To know the number of days in a specific month

File name: VorC.java

// Java Program to verify if an alphabet is a vowel or a consonant


import java.util.*;
import java.io.*;
class VorC{
public static void main(String[] args) {

char alpha='k';

switch ( alpha ){
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
                                      System.out.println("It is a Vowel");
break;
default :{
System.out.println(" It is a Consonant");

}
}
}}

Output

It is a Consonant

File name: Daysinmon.java

// Java program to know the number of days in a specific month


import java.util.Scanner;


public class Daysinmon {
  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);


    int mon = 0;
    int d;


   System.out.println("Enter the number of month in a year : ");
    mon = s.nextInt();


    switch (mon) {
    case 4:
    case 6:
    case 9:
    case 11:
      d = 30;
      break;


    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      d = 31;
      break;


    case 2:
      d = 28;
      break;


    default:
      d = 0;
      break;
    }


    if (d != 0)
{


     System.out.println("The total number of days in a given month is :"+d);
}
else
{
  System.out.println("Please enter a valid input");
} 
}
}

Output

Enter the number of month in a year :
8
The total number of days in a given month is :31

Advantages

We are well aware that the switch statement only functions for a single data point or statement, and that there are frequently multiple outputs with the same value. In these situations, the fall through condition is significant and helps the system run more quickly by avoiding comparing.

Disadvantages

We neglect to specify explicit break statement inside the switch statement, which causes all cases, even those that don't match the searched value to be executed. The program suffer greatly as a result of this scenario. Therefore, to resolve this issue, we must include the break keyword inside the switch statement for each case. The fall through circumstance has this problem.


Related Topics

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Convert List to String in Java

We occasionally need to create a string from a list of characters. Since a string is only a collection of characters, a character array can be converted into a string....

6 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Java Math atan() Method

The atan() method of Math class computes the trigonometric Arc Tangent (inverse of tangent ) of an angle. The value returned is between -pi/2 to pi/2. Syntax: public static double atan(double a) Parameters: The...

2 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

How to Change the Day in the Date using Java?

To operate with the date and time in Java, we need the Calendar abstract class. It provides a number of helpful interfaces that enable us to convert dates between a...

4 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.