Bird
Raised Fist0
Swiftprogramming~10 mins

Switch with compound cases in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Switch with compound cases
Start: value to check
↓
Switch statement begins
↓
Check first case
↓
Execute case
↓
End switch
↓
Default case
↓
Execute default
↓
End switch
The switch checks the value against each case, including compound cases separated by commas, and executes the matching case block.
Execution Sample
Swift
let letter = "a"
switch letter {
case "a", "e", "i", "o", "u":
    print("Vowel")
default:
    print("Consonant")
}
This code checks if the letter is a vowel using a switch with compound cases.
Execution Table
StepCondition CheckedResultBranch TakenOutput
1letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u"True (letter is "a")case "a", "e", "i", "o", "u"Vowel
2Switch ends after matching caseN/AExit switchN/A
💡 Matching case found at step 1, switch exits after executing that case.
Variable Tracker
VariableStartAfter Step 1Final
letter"a""a""a"
Key Moments - 2 Insights
Why does the switch not check other cases after finding a match?
Because in Swift, once a matching case is found, the switch exits immediately as shown in execution_table step 2.
How do compound cases work in the switch?
Compound cases use commas to list multiple values in one case, so if the value matches any of them, that case runs (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 1?
AError
BConsonant
CVowel
DNo output
💡 Hint
Check the 'Output' column in execution_table row 1.
At which step does the switch stop checking cases?
AStep 1
BStep 2
CAfter default case
DNever stops
💡 Hint
Look at the 'Branch Taken' and 'Exit switch' note in execution_table step 2.
If letter was "b", what case would run?
Adefault
Bcase "a", "e", "i", "o", "u"
CNo case runs
DError
💡 Hint
If no compound case matches, switch runs the default case.
Concept Snapshot
switch value {
  case val1, val2, val3:
    // runs if value matches any listed
  default:
    // runs if no case matches
}
- Compound cases use commas to check multiple values
- Switch exits after first match
- Default handles unmatched values
Full Transcript
This example shows how a Swift switch statement uses compound cases separated by commas to check if a value matches any of several options. The switch checks each case in order. When it finds a match, it runs that case's code and then exits the switch immediately. If no cases match, the default case runs. Here, the letter "a" matches the first compound case, so "Vowel" is printed and the switch ends.

Practice

(1/5)
1.

What does a compound case in a Swift switch statement allow you to do?

easy
A. Skip the default case safely
B. Use multiple switch statements at once
C. Combine if and switch in one line
D. Match multiple values in a single case using commas

Solution

  1. Step 1: Understand compound cases

    Compound cases let you list several values separated by commas in one case.
  2. Step 2: Recognize the benefit

    This makes the code cleaner by avoiding repeated code for similar cases.
  3. Final Answer:

    Match multiple values in a single case using commas -> Option D
  4. Quick Check:

    Compound case = multiple values in one case [OK]
Hint: Look for commas inside a case to spot compound cases [OK]
Common Mistakes:
  • Thinking compound cases combine multiple switches
  • Believing compound cases skip default
  • Confusing compound cases with if-else
2.

Which of the following is the correct syntax for a compound case in Swift?

switch value {
    case 1, 2, 3:
        print("One to three")
    default:
        print("Other")
}
easy
A. case 1, 2, 3:
B. case 1; 2; 3:
C. case (1, 2, 3):
D. case [1, 2, 3]:

Solution

  1. Step 1: Recall Swift compound case syntax

    Swift uses commas to separate multiple values in one case.
  2. Step 2: Check each option

    case 1, 2, 3: uses commas correctly; others use semicolons, parentheses, or brackets incorrectly.
  3. Final Answer:

    case 1, 2, 3: -> Option A
  4. Quick Check:

    Commas separate values in compound case [OK]
Hint: Use commas, not semicolons or brackets, for compound cases [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Putting values inside parentheses or brackets
  • Missing the colon after case
3.

What will be printed by this Swift code?

let letter = "b"
switch letter {
case "a", "e", "i", "o", "u":
    print("Vowel")
case "b", "c", "d":
    print("Consonant")
default:
    print("Other")
}
medium
A. Vowel
B. No output
C. Consonant
D. Other

Solution

  1. Step 1: Identify the value of letter

    The variable letter is "b".
  2. Step 2: Match letter in switch cases

    "b" matches the second case with "b", "c", "d" which prints "Consonant".
  3. Final Answer:

    Consonant -> Option C
  4. Quick Check:

    "b" in second case = Consonant [OK]
Hint: Check which case list contains the value [OK]
Common Mistakes:
  • Confusing vowels and consonants
  • Forgetting to include default case
  • Assuming no output if no exact match
4.

Find the error in this Swift switch statement:

let number = 3
switch number {
case 1, 2
    print("One or Two")
case 3:
    print("Three")
default:
    print("Other")
}
medium
A. Missing colon after case 1, 2
B. Missing default case
C. Cannot use compound cases with integers
D. Missing parentheses around 1, 2

Solution

  1. Step 1: Check syntax of each case

    Each case must end with a colon (:). The first case is missing it.
  2. Step 2: Verify other parts

    Default case is present; compound cases with integers are allowed; parentheses are not needed.
  3. Final Answer:

    Missing colon after case 1, 2 -> Option A
  4. Quick Check:

    Case lines must end with colon [OK]
Hint: Look for missing colons after case labels [OK]
Common Mistakes:
  • Forgetting colon after case
  • Thinking parentheses are required
  • Believing compound cases disallow integers
5.

Given this Swift code, what will be the output?

let grade = "B"
switch grade {
case "A", "B", "C":
    print("Pass")
case "D", "F":
    print("Fail")
case "I", "W":
    print("Incomplete or Withdrawn")
default:
    print("Unknown grade")
}
hard
A. Incomplete or Withdrawn
B. Pass
C. Fail
D. Unknown grade

Solution

  1. Step 1: Identify the value of grade

    The variable grade is "B".
  2. Step 2: Match grade in compound cases

    "B" matches the first case "A", "B", "C" which prints "Pass".
  3. Final Answer:

    Pass -> Option B
  4. Quick Check:

    "B" in first case = Pass [OK]
Hint: Match value to any in compound case list [OK]
Common Mistakes:
  • Assuming "B" is a fail grade
  • Ignoring default case
  • Confusing case groups
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard