0
0
Kotlinprogramming~10 mins

When expression as powerful switch in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - When expression as powerful switch
Start
Evaluate expression
Match case 1?
NoMatch case 2?
Execute case 1
Return result
End
The when expression evaluates a value and matches it against multiple cases, executing the first matching case or else the default else case, then returns the result.
Execution Sample
Kotlin
val x = 2
val result = when (x) {
  1 -> "One"
  2 -> "Two"
  3 -> "Three"
  else -> "Unknown"
}
This code checks the value of x and returns a string matching the number or "Unknown" if no match.
Execution Table
StepExpression ValueCase CheckedMatch?ActionResult
121NoCheck next case
222YesReturn "Two""Two"
33SkippedSkipped"Two"
4elseSkippedSkipped"Two"
💡 Match found at case 2, returned "Two", execution stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
x2222
resultundefinedundefined"Two""Two"
Key Moments - 3 Insights
Why does the when expression stop checking cases after finding a match?
Because when acts like a switch, it executes only the first matching case and then returns immediately, as shown in execution_table row 2.
What happens if none of the cases match and there is no else branch?
The when expression will throw an exception if no else branch is provided and no case matches. The else branch acts as a default to avoid this.
Can when expression return a value?
Yes, when is an expression that returns the value of the matched case, as shown by the 'result' variable in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
A"Two"
B"One"
C"Three"
D"Unknown"
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the when expression stop checking further cases?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Match?' column and see where 'Yes' appears first in execution_table.
If x was 5, which case would be executed?
ACase 1
BCase 2
CCase 3
DElse case
💡 Hint
Refer to the concept_flow and execution_table logic for unmatched values.
Concept Snapshot
when (value) {
  case1 -> result1
  case2 -> result2
  ...
  else -> defaultResult
}

- Evaluates value once
- Matches cases top to bottom
- Executes first matching case
- Returns the matched case's result
- else is mandatory if cases don't cover all values
Full Transcript
The when expression in Kotlin works like a powerful switch statement. It evaluates a value once and checks it against multiple cases in order. When it finds the first matching case, it executes that case and returns its result immediately, skipping the rest. If no cases match, the else branch runs as a default. This makes when concise and expressive for branching logic. In the example, x is 2, so when matches case 2 and returns "Two". Variables track the value of x and the result returned. The execution table shows step-by-step how each case is checked and when the match happens. This helps beginners see how when stops checking after the first match and how the result is assigned. Remember to always include else if your cases don't cover all possibilities to avoid errors.