0
0
Swiftprogramming~10 mins

Switch with where clauses in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Switch with where clauses
Start with a value
Enter switch statement
Check case 1
Check where clause?
Default case or end
Evaluate where condition
Execute case
End switch
The switch checks each case and if it has a where clause, it evaluates that condition before executing the case.
Execution Sample
Swift
let number = 15
switch number {
case let x where x % 3 == 0:
    print("Divisible by 3")
default:
    print("Other")
}
This code checks if number is divisible by 3 using a where clause in the switch case.
Execution Table
StepCase CheckedWhere Clause ConditionCondition ResultBranch TakenOutput
1case let x where x % 3 == 015 % 3 == 0trueYesDivisible by 3
2defaultN/AN/ANo
💡 First case matches with where clause true, so switch ends after executing that case.
Variable Tracker
VariableStartAfter Step 1Final
number151515
xN/A1515
Key Moments - 2 Insights
Why does the switch check the where clause condition after matching the case pattern?
Because the where clause adds an extra condition to decide if the matched case should run. See execution_table step 1 where the case matches number but only runs because where condition is true.
What happens if the where clause condition is false?
The switch skips that case and checks the next one. In the execution_table, if the condition was false, it would move to the default case.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'x' when the where clause is checked?
A0
B3
C15
DN/A
💡 Hint
Check variable_tracker row for 'x' after Step 1.
At which step does the switch decide to execute the case with the where clause?
AStep 1
BStep 2
CNo step, it skips
DBefore Step 1
💡 Hint
Look at execution_table row where Branch Taken is 'Yes'.
If the number was 14, how would the execution_table change?
AStep 1 where clause condition would be true, case runs
BStep 1 where clause condition would be false, default case runs
CNo cases would run
DError occurs
💡 Hint
Think about 14 % 3 == 0 and how where clause affects case execution.
Concept Snapshot
switch value {
  case let x where condition:
    // runs if pattern matches and condition is true
  default:
    // runs if no other case matches
}

Use where to add extra checks to cases.
Switch stops at first matching case with true where clause.
Full Transcript
This visual trace shows how a Swift switch statement works when using where clauses. The switch starts by checking each case pattern. If a case has a where clause, it evaluates that condition next. Only if the where clause is true does the switch execute that case. Otherwise, it moves to the next case. In the example, the number 15 matches the case pattern and passes the where clause check (divisible by 3), so it prints 'Divisible by 3' and ends. Variables like 'x' get assigned during pattern matching and are used in the where clause. If the where clause was false, the switch would continue to the default case. This helps add extra conditions to switch cases beyond simple pattern matching.