← Back to Swift Course | Chapter 3: Control Flow | Lesson 3 of 8

Switch Statement Basics

A switch statement checks a value against a list of possible matches and runs the code for whichever one fits.
Syntax
swift
switch value {
case pattern1:
    // code
case pattern2, pattern3:
    // code
default:
    // code
}

Basic Switch Syntax

A switch statement evaluates a value once and matches it against a series of case patterns, running only the matching block.

Example: Basic Switch Syntax

markup
// Declare a constant `day`, set to `3`
let day = 3
// Switch on `day` and branch by case
switch day {
// Case: `1`
case 1:
    // Print "Monday" to the console
    print("Monday")
// Case: `2`
case 2:
    // Print "Tuesday" to the console
    print("Tuesday")
// Case: `3`
case 3:
    // Print "Wednesday" to the console
    print("Wednesday")
default:
    // Print "Some other day" to the console
    print("Some other day")
}

Matching Multiple Values in One Case

A single case can match several values at once by separating them with commas.

Example: Matching Multiple Values in One Case

markup
// Declare a constant `letter` (a Character), set to `"a"`
let letter: Character = "a"
// Switch on `letter` and branch by case
switch letter {
// Case: `"a", "e", "i", "o", "u"`
case "a", "e", "i", "o", "u":
    // Print "\(letter) is a vowel" to the console
    print("\(letter) is a vowel")
default:
    // Print "\(letter) is a consonant" to the console
    print("\(letter) is a consonant")
}

No Implicit Fallthrough

Unlike C or Java, Swift's switch cases do not fall through to the next case by default -- only the matching case's code runs.

Note: If you truly want fallthrough behavior, add the fallthrough keyword explicitly at the end of a case.

Example: No Implicit Fallthrough

markup
// Declare a constant `number`, set to `2`
let number = 2
// Switch on `number` and branch by case
switch number {
// Case: `1`
case 1:
    // Print "one" to the console
    print("one")
// Case: `2`
case 2:
    // Print "two" to the console
    print("two")
// Case: `3`
case 3:
    // Print "three" to the console
    print("three")
default:
    // Print "other" to the console
    print("other")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that Swift's switch must be exhaustive -- every possible value must be handled, often requiring a default case.
  2. Expecting implicit fallthrough between cases like in C; Swift cases do NOT fall through automatically unless you write fallthrough explicitly.
  3. Using a switch for a simple two-way boolean check where an if-else would be simpler and clearer.
Chapter Summary
  • switch compares a value against multiple case patterns and runs the matching one.
  • Swift switches must be exhaustive; a default case covers any remaining values.
  • Unlike C, cases do not fall through to the next case automatically.
  • Multiple values can be matched in one case using a comma-separated list.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.