How does Swift's switch differ from C's switch?
understanding Swift's safer control flow.
switches must be exhaustive, there is no implicit fallthrough between cases, and cases can match ranges, tuples, and bind values.
WHAT THIS TESTS This checks whether you understand that Swift deliberately reworked switch to eliminate classic C bugs and to act as a pattern-matching tool, not just an integer dispatch. It is a beginner question but reveals whether you think in Swift idioms or are translating C habits.
A GOOD ANSWER COVERS First, exhaustiveness: a Swift switch must handle every possible value, so you either cover all enum cases or provide a default; the compiler rejects an incomplete switch. Second, no implicit fallthrough: once a case matches, only its statements run and control exits the switch, so no trailing break is required, which removes the infamous missing-break bug. Third, richer matching: cases can match ranges like 1...9, tuples, bind associated values, and add where clauses for extra conditions. Mention that fallthrough is still available as an explicit keyword when intentionally wanted.
COMMON WRONG ANSWERS Saying you must write break after each case to prevent fallthrough. Believing switch only works on integers. Forgetting that empty case bodies are not allowed and that every case needs at least one statement.
LIKELY FOLLOW-UPS How do you match multiple values in one case? How does value binding with associated enum values work? When would you use the fallthrough keyword? What does a where clause add?
ONE CONCRETE EXAMPLE Given let point = (1, 1), a switch can match case (0, 0) for the origin, case (_, 0) for the x-axis, case (let x, let y) where x == y for the diagonal, and a final default. Each branch runs independently with no break, and the compiler guarantees one branch always executes because the default makes it exhaustive.
Read the original → docs.swift.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.