Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 8
switch - Fall-through, [[fallthrough]] & Scope
`switch` dispatches on an integer or enum class value. Cases fall through by default - execution continues into the next case unless broken. C++17 added `[[fallthrough]]` attribute to document intentional fall-through and silence the warning. Variable declarations inside switch require extra care - they can be 'jumped over' leading to undefined behaviour unless wrapped in a block.
switch - All Patterns Including C++17 Attributes
#include <iostream>
#include <string>
enum class Day { Mon, Tue, Wed, Thu, Fri, Sat, Sun };
std::string day_type(Day d) {
switch (d) {
// ── Intentional fall-through (C++17 attribute) ────────
case Day::Sat:
case Day::Sun:
return "weekend"; // fall-through from Sat to Sun is intentional
// ── Intentional fall-through with [[fallthrough]] ─────
case Day::Mon:
std::cout << "Start of work week
";
[[fallthrough]]; // explicit: don't warn, I intend this
case Day::Tue:
case Day::Wed:
case Day::Thu:
case Day::Fri:
return "weekday";
default:
return "unknown";
}
}
void process_code(int code) {
switch (code) {
case 1:
std::cout << "Code 1
";
break;
case 2: case 3: // multiple cases for same body
std::cout << "Code 2 or 3
";
break;
case 4: { // block scope for local variable
int multiplied = code * 10; // needs braces!
std::cout << "Code 4, val=" << multiplied << "
";
break;
} // multiplied destroyed here
default:
std::cout << "Unknown code: " << code << "
";
}
}
int main() {
std::cout << day_type(Day::Sat) << "
"; // weekend
std::cout << day_type(Day::Mon) << "
"; // weekday (with Start of work week)
std::cout << day_type(Day::Wed) << "
"; // weekday
process_code(1);
process_code(3);
process_code(4);
process_code(99);
return 0;
}Common Mistakes
- Forgetting `break` causes silent fall-through bugs - if `case 2:` has no `break`, execution falls into `case 3:` code. Enable `-Wimplicit-fallthrough` (included in `-Wextra`) to warn about unlabeled fall-throughs.
- Declaring variables inside switch without a block - `case 1: int x = 5; break; case 2: /* x's initialisation was skipped! */` - this is undefined behaviour. Always wrap case bodies with local variables in `{ }`.
- `switch` only works with integral types and enums - you cannot `switch` on `std::string` or `double`. For strings, use if/else if chains or an `std::unordered_map<std::string, function>` dispatch table.
Tip
Tip
Practice switch Fallthrough fallthrough Scope in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
O(1) Jump Table Dispatch, Break Semantics, and C++17 Intentional Fallthrough
Practice Task
Note
Practice Task - (1) Write a working example of switch Fallthrough fallthrough Scope from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with switch Fallthrough fallthrough Scope is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.
Key Takeaways
- `switch` dispatches on an integer or enum class value.
- Forgetting `break` causes silent fall-through bugs - if `case 2:` has no `break`, execution falls into `case 3:` code. Enable `-Wimplicit-fallthrough` (included in `-Wextra`) to warn about unlabeled fall-throughs.
- Declaring variables inside switch without a block - `case 1: int x = 5; break; case 2: /* x's initialisation was skipped! */` - this is undefined behaviour. Always wrap case bodies with local variables in `{ }`.
- `switch` only works with integral types and enums - you cannot `switch` on `std::string` or `double`. For strings, use if/else if chains or an `std::unordered_map<std::string, function>` dispatch table.
Finished this lesson?