cpp / intermediate
Snippet
Multi-Case Logic with Fallthrough
The [[fallthrough]] attribute is an intermediate syntax feature that tells the compiler a missing break is intentional, avoiding warnings while sharing logic between switch cases.
snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>void processCommand(int cmd) {switch (cmd) {case 1:case 2:std::cout << "Handling basic command..." << std::endl;[[fallthrough]]; // Explicitly indicating intentional fallthroughcase 3:std::cout << "Performing advanced processing..." << std::endl;break;default:std::cout << "Unknown command." << std::endl;}}int main() {processCommand(1);return 0;}
Breakdown
1
[[fallthrough]];
Standard attribute to suppress compiler warnings about missing break statements.
2
case 1: case 2:
Stacking cases allows multiple inputs to trigger the same initial block of code.