0
0
Swiftprogramming~10 mins

Switch with value binding in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Switch with value binding
Start with a value
Enter switch statement
Match case pattern?
NoNext case
Yes
Bind value to variable
Execute case block
Exit switch
The switch checks the value against each case pattern. When a match is found, it binds the matched value to a variable and runs the case code.
Execution Sample
Swift
let point = (2, 0)
switch point {
case (let x, 0):
    print("On the x-axis at \(x)")
case (0, let y):
    print("On the y-axis at \(y)")
default:
    print("Somewhere else")
}
This code checks a point's coordinates and prints which axis it lies on, using value binding to capture the coordinate.
Execution Table
StepValueCase PatternMatch?Bound VariableActionOutput
1(2, 0)(let x, 0)Yesx = 2Execute first caseOn the x-axis at 2
2(2, 0)(0, let y)No-Skip case-
3(2, 0)defaultNo-Not reached-
4----Exit switch-
💡 First case matches because second element is 0, so switch exits after executing it.
Variable Tracker
VariableStartAfter Step 1Final
point(2, 0)(2, 0)(2, 0)
x-22
y---
Key Moments - 3 Insights
Why does the switch stop after the first case even though there are more cases?
Because the first case matches the value (2, 0), the switch executes that case and then exits immediately, as shown in execution_table step 1 and 4.
What does 'let x' do inside the case pattern?
'let x' binds the matched part of the tuple to variable x so it can be used inside the case block, as seen in execution_table step 1 where x = 2.
Why is the second case not executed?
The second case pattern (0, let y) does not match because the first element of the tuple is 2, not 0, so it is skipped (execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'x' after step 1?
A2
B0
C-
DUndefined
💡 Hint
Check the 'Bound Variable' column at step 1 in the execution_table.
At which step does the switch stop checking further cases?
AStep 4
BStep 3
CStep 1
DStep 2
💡 Hint
Look at the 'Match?' and 'Action' columns; the switch exits after the first match.
If the point was (0, 5), which case would match?
Acase (let x, 0)
Bcase (0, let y)
Cdefault
DNo case matches
💡 Hint
Refer to the case patterns and how they match tuple elements in the execution_table.
Concept Snapshot
Switch with value binding syntax:

switch value {
case let pattern:
    // use bound variables
default:
    // fallback
}

- Matches value to patterns
- Binds parts to variables
- Executes first matching case
- Exits after match
Full Transcript
This example shows how a Swift switch statement can match a tuple value and bind parts of it to variables using 'let'. The switch checks each case pattern in order. When it finds a match, it binds the matched parts to variables and runs the case code. Then it exits the switch. For example, with point (2, 0), the first case matches because the second element is 0. It binds x = 2 and prints 'On the x-axis at 2'. The other cases are skipped. This helps write clear code that extracts values directly in the switch cases.