Chapter 52: Swift Switch

1. Why use switch instead of if…else if?

switch is usually cleaner and safer when you have many possible discrete values to check.

Typical situations where switch wins:

  • Checking one value against many exact possibilities (enum cases, string options, status codes…)
  • You want exhaustive checking (compiler warns if you forget a case)
  • You want cleaner vertical flow instead of long else if chains
  • You need multiple matching patterns (ranges, tuples, enums with associated values…)

switch is not just a replacement for if — it has different superpowers.

2. Basic switch syntax – the skeleton

Swift

Key rules you must remember:

  1. Every possible value must be covered (or you need default) → compiler error if missing cases (when switching on enum without default)
  2. No implicit fallthrough (unlike C/C++) → each case ends automatically
  3. You can have multiple values in one case (case 1, 3, 5:)
  4. You can use ranges, tuples, enums with associated values, etc.

3. Very first examples – feel the difference

Example 1 – Basic enum switch

Swift

Example 2 – No default needed when enum is exhaustive

If you switch on a non-exhaustive enum or other types, you need default:

Swift

4. Real-life examples – the kind of code you will actually write

Example 1 – HTTP status code handling (very common in networking)

Swift

Example 2 – App state / screen flow (very typical)

Swift

Example 3 – User role / permission system

Swift

Example 4 – Temperature + clothing advice (realistic nested switch)

Swift

5. Very Important Modern Features (2024–2026 style)

Feature 1 – No need for break (automatic break)

Unlike C/C++/Java, Swift does NOT fall through — each case ends automatically.

Feature 2 – Multiple matching values in one case

Swift

Feature 3 – Range matching

Swift

Feature 4 – switch on optionals (very clean)

Swift

Or even shorter (pattern matching):

Swift

6. Very Common Beginner Mistakes & Fixes

Mistake Wrong code Correct / Better way Why?
Forgetting default when needed switch statusCode { case 200: … } Add default: Compile error if not exhaustive (for some types)
Expecting fallthrough case 1: print(“One”) case 2: print(“Two”) Each case needs its own block No implicit fallthrough
Using if for many exact matches long else if chain Use switch Cleaner, safer (exhaustiveness)
Forgetting to handle all enum cases switch role { case .admin: … } Add all cases or default Compiler warns / errors
Nested switch hell switch inside switch inside switch Flatten with guard or combine conditions Hard to read & maintain

7. Quick Summary – When to prefer switch over if…else if

Situation Prefer switch? Prefer if…else? Reason / guideline
Checking many exact values (enum, status codes) Yes Exhaustive checking, cleaner
Matching ranges Yes Sometimes switch handles ranges very nicely
More than 4–5 conditions Yes Easier to read & maintain
Optional unwrapping + matching Yes case .some(let x): is very clean
Complex boolean combinations Yes `if a && (b
Early exit / validation Yes (guard) guard is designed for this

8. Small Practice – Try these

  1. Write a switch that gives grade based on percentage:
    • 90…100 → “A+”
    • 80..<90 → “A”
    • 70..<80 → “B”
    • 60..<70 → “C”
    • else → “Fail”
  2. Write a switch on enum UserRole (admin, moderator, member, guest)
    • admin → full access
    • moderator → content tools
    • member → normal view
    • guest → limited view + sign-in prompt
  3. Write a function that returns weather advice using switch on temperature ranges

Paste your code here if you want feedback or want to see even cleaner versions!

What would you like to explore next?

  • switch with associated values (very powerful with enums)
  • guard statement (the modern companion to if)
  • if let / optional binding in depth
  • Conditional logic in SwiftUI (showing/hiding views)
  • Or move to another topic (loops, functions, arrays, optionals…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *