Conditions and Operators: Conditions and Switch - Case: Fall Through#
Adapted from: “Learn Modern C++” by cpptutor: Learn Modern C++: Conditions and Operators
Program that Demonstrates Conditions and Fall Through in the Switch Case Statement#
// 03-fallthrough.cpp : demonstrate case clauses without break
#include <iostream>
using namespace std;
int main() {
cout << "Please enter an integer between zero and three:\n";
int n{};
cin >> n;
switch (n) {
case 0:
cout << "Number is less than 1\n";
[[fallthrough]];
case 1:
cout << "Number is less than 2\n";
[[fallthrough]];
case 2:
cout << "Number is less than 3\n";
break;
case 3:
cout << "Number is exactly 3\n";
break;
default:
cout << "Number out of range!\n";
break;
}
}
Explanation of the Above Code#
This C++ program demonstrates the use of the switch
statement with the [[fallthrough]]
attribute to explicitly allow fall-through behavior between cases. Here’s a breakdown of the code:
Code Explanation:#
Header Inclusion:
#include <iostream>
The
<iostream>
header is included to enable input (cin
) and output (cout
) operations.
Namespace:
using namespace std;
This allows the program to use standard library features (like
cin
andcout
) without prefixing them withstd::
.
Main Function:
int main() {
The
main
function is the entry point of the program.
User Prompt and Input:
cout << "Please enter an integer between zero and three:\n"; int n{}; cin >> n;
The program prompts the user to enter an integer between 0 and 3.
The input is stored in the variable
n
.
Switch Statement:
switch (n) {
The
switch
statement evaluates the value ofn
and executes the corresponding case block.
Case Blocks:
Case 0:
case 0: cout << "Number is less than 1\n"; [[fallthrough]];
Prints “Number is less than 1”.
The
[[fallthrough]]
attribute explicitly allows execution to continue to the next case (case 1).
Case 1:
case 1: cout << "Number is less than 2\n"; [[fallthrough]];
Prints “Number is less than 2”.
The
[[fallthrough]]
attribute allows execution to continue to the next case (case 2).
Case 2:
case 2: cout << "Number is less than 3\n"; break;
Prints “Number is less than 3”.
The
break
statement prevents further fall-through and exits theswitch
block.
Case 3:
case 3: cout << "Number is exactly 3\n"; break;
Prints “Number is exactly 3”.
The
break
statement exits theswitch
block.
Default Case:
default: cout << "Number out of range!\n"; break;
Handles any input that is not 0, 1, 2, or 3.
Prints “Number out of range!”.
Key Features:#
Fall-Through Behavior:
The
[[fallthrough]]
attribute is used to explicitly indicate intentional fall-through between cases. This improves code readability and avoids accidental fall-through, which can lead to bugs.
Default Case:
The
default
case ensures that invalid inputs are handled gracefully.
Example Input/Output:#
Input: 0
Please enter an integer between zero and three: 0 Number is less than 1 Number is less than 2 Number is less than 3
Input: 3
Please enter an integer between zero and three: 3 Number is exactly 3
Input: 5
Please enter an integer between zero and three: 5 Number out of range!
Compile and Run Code#
Use Python to Change to Working Directory#
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/03_Conditions_and_Operators"
os.chdir(code_dir)
Use Docker to Compile the Code in a C++23 Environment#
!docker run --rm -v $(pwd):/app cpp23-clang18:latest clang++-18 -std=c++23 -stdlib=libc++ /app/03-fallthrough.cpp -o /app/03-fallthrough
Use Docker to Run Executable in a C++23 Environment#
!echo "0" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-fallthrough
Please enter an integer between zero and three:
Number is less than 1
Number is less than 2
Number is less than 3
!echo "1" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-fallthrough
Please enter an integer between zero and three:
Number is less than 2
Number is less than 3
!echo "2" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-fallthrough
Please enter an integer between zero and three:
Number is less than 3
!echo "3" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-fallthrough
Please enter an integer between zero and three:
Number is exactly 3
!echo "4" | docker run -i --rm -v $(pwd):/app cpp23-clang18:latest ./03-fallthrough
Please enter an integer between zero and three:
Number out of range!