Fallthrough in C++ Last Updated : 13 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Fall through is a type of error that occurs in various programming languages like C, C++, Java, Dart …etc. It occurs in switch-case statements where when we forget to add a break statement and in that case flow of control jumps to the next line. Due to this when any case is matched with the specified value then control falls through to subsequent cases until a break statement is found. In C++, the error is not found on fallthrough but in languages like Dart, an error occurs whenever a fallthrough occurs. It is not always necessary to avoid fallthrough, but it can be used as an advantage as well. Program 1: Below is the program to illustrate how fall-through occurs: C++ // C++ program to illustrate // Fallthrough in C++ #include <iostream> using namespace std; // Driver Code int main() { int n = 2; // Switch Cases switch (n) { case 1: { cout << "this is one \n"; } case 2: { cout << "this is two \n"; } case 3: { cout << "this is three \n"; } default: { cout << "this is default \n"; } } return 0; } Output: this is two this is three this is default Explanation: In the above code, there is no break statement so after matching with the 2nd case the control will fall through and the subsequent statements will also get printed. How to Avoid Fall Through? To avoid Fall through, the idea is to use a break statement after each and every case so that after matching it goes out of the switch statement and control goes to the statement next to the switch statement. Program 2: Below is the program to illustrate how to avoid the fall through: C++ // C++ program to illustrate how to // avoid fall through #include <iostream> using namespace std; // Driver Code int main() { int n = 2; // Switch Cases switch (n) { case 1: { cout << "this is one \n"; break; } case 2: { cout << "this is two \n"; // After this break statement // the control goes out of // the switch statement break; } case 3: { cout << "this is three \n"; break; } default: { cout << "this is default \n"; break; } } return 0; } Output: this is two Comment More infoAdvertise with us Next Article erase_if() Function in C++ C coder_nero Follow Improve Article Tags : Misc C++ Programs C++ CPP-Basics cpp-switch CPP-Control-Flow +2 More Practice Tags : CPPCPP-Control-FlowMisc Similar Reads How to Catch All Exceptions in C++? In C++, exceptions are objects that indicate you have an error in your program. They are handled by the try-catch block in C++. In this article, we will learn how to catch all the exceptions in C++. Catching All Exceptions in C++To catch all kinds of exceptions in our catch block in C++, we can defi 2 min read erase_if() Function in C++ The std::erase_if() is a utility introduced in C++20 that is used to remove elements from containers based on a specified condition. This function erases all elements that satisfy a given predicate from standard containers like std::vector, std::deque, std::list, std::forward_list, std::string, and 3 min read exchange() Function in C++ 14 The exchange() function is a built-in function in C++ 14 defined in the <utility> header. The exchange() function copies new value to old value and it will return the old value. Syntax: exchange(old, new) Parameters: The function needs two parameters. The parameter needs to be set.The paramete 2 min read Exit a loop in C++ Exit a Loop in C++: If the condition of an iteration statement (for, while, or do-while statement) is omitted, that loop will not terminate unless the user explicitly exits it by a break, continue, goto, or some less obvious way such as a call of exit() in C++. Some common ways to exit a loop are as 4 min read Structure of C++ Program The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows: Documentation Section:This section comes first and is used to document the logic of the program that the programmer going to code.It can be also used to write for purpo 5 min read How to Throw and Catch Exceptions in C++? In C++, exception handling is a mechanism that allows us to handle runtime errors and exceptions are unusual conditions that occur at runtime. In this article, we will learn how to throw and catch exceptions in C++. Throw and Catch Exceptions in C++In C++ exceptions can be "thrown" when an error occ 2 min read Like