The switch Statement & Fallthrough Control
🟢 Beginner
📖 Definition
A switch statement compares an integral/enum expression against case values. break statements prevent fallthrough to subsequent case labels. C++17 provides [[fallthrough]] attribute for intentional fallthroughs.
💡 Code Example
#include <iostream>
int main() {
int option = 2;
switch (option) {
case 1:
std::cout << "Option 1 Selected\n";
break;
case 2:
std::cout << "Option 2 Selected\n";
break;
default:
std::cout << "Invalid Option\n";
break;
}
return 0;
}