Javatpoint Logo
Javatpoint Logo

Fallthrough in C++

In C++ language, fallthrough refers to the behaviour in a switch statement where the control flows from one case to another. It occurs when there is no break statement at the end of a case, allowing the control to continue to the next case.

In programming control, the flow structure plays a vital role in the execution path of the code. Among these structures, switch statements are used for selecting or executing specific code blocks based on the value of the expression. Sometimes, there will be a fallthrough, which leads to intended and unintended outcomes.

We can classify or divide the fallthrough into two. These are intentional fallthroughs and unintentional fallthroughs.

Unintentional Fallthrough:

Unintentional fallthrough is the more common issue while using the switch statements. It happens when a developer forgets to break a statement at the end of each case, leading to the control flow to the following cases. It often results in unexcepted results and logical errors.

Problems due to unintentional fallthrough:

It leads to subtle bugs and logic errors. Suppose the developer forgot to use the break statements after each case. It might go unnoticed, resulting in unexpected behavior. The result will be unexcepted.

Example:

Let us take a C++ program to illustrate the unintentional fallthrough:

Output:

Fallthrough in C++

Explanation:

In the program, there is a variable named "day" which represents the days in the week. The input is taken from the user and assigned to the "day" variable. It uses switch statements. The program employs unintentional fallthrough denoted by the [[fallthrough]] attribute after each case except the last. The unintentional fallthrough causes the program to print the day entered by the user and continue to print subsequent days without encountering a break statement.

If the user enters day 3, Tuesday should only be printed, but here, all the days from Tuesday to Saturday are printed because no break statement stops the control flow just after completing case 3. So, we call this fallthrough an unintentional fallthrough.

Intentional Fallthrough:

Intentional fallthrough means the developer did not want to break the control flow if the user chooses some particular case. The developer will get the desired results or outputs by using the intentional fallthrough. There will be no issues or logical errors in the program if fallthrough is intentional fallthrough

Example:

Let us take a C++ program to illustrate the intentional fallthrough:

Output:

Fallthrough in C++

Explanation:

In the program, a variable named "month" represents the month in a year. The value for the month is taken from the user. The month variable takes the input and uses a switch statement. The code efficiently categorizes the entered month into seasons. If some month is given, it tells the remaining months to complete that season. The intentional fallthrough is applied to connect the consecutive months within a season.

If the user gives 3 as input, it prints March, April and May months along with the season. Reflecting the logical continuity between these seasons for the given months







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA