Switch Statements
By the end of this lesson, you'll be able to:
- Write a switch statement as an alternative to a long if/else if chain
- Use when blocks and a default block correctly
- Decide when switch is clearer than if/else if
Prerequisites: "Logical Operators in Practice"
The basic shape
String region = 'EMEA';
switch on region {
when 'AMER' {
System.debug('Americas team');
}
when 'EMEA' {
System.debug('Europe, Middle East, Africa team');
}
when 'APAC' {
System.debug('Asia-Pacific team');
}
when else {
System.debug('Unknown region');
}
}
String region = 'EMEA';— the value being checked.- (blank line)
switch on region {—switch on regionnames the value being checked once; everywhenbelow tests it against one possible match. 4-6.when 'AMER' { ... }—regionis not'AMER', so this block is skipped. 7-9.when 'EMEA' { ... }— matches, sinceregionis'EMEA'— logs "Europe, Middle East, Africa team". 10-12.when 'APAC' { ... }— never even checked, since line 7 already matched. 13-15.when else { ... }— the catch-all, playing the same role a trailingelseplays in an if-chain — also never reached here.}— closes the wholeswitchblock.
When switch beats if/else if
Compare the switch above to the equivalent if-chain:
if (region == 'AMER') {
System.debug('Americas team');
} else if (region == 'EMEA') {
System.debug('Europe, Middle East, Africa team');
} else if (region == 'APAC') {
System.debug('Asia-Pacific team');
} else {
System.debug('Unknown region');
}
if (region == 'AMER') {—false.System.debug('Americas team');— skipped.} else if (region == 'EMEA') {—true— the match.System.debug('Europe, Middle East, Africa team');— runs.} else if (region == 'APAC') {— never reached.System.debug('Asia-Pacific team');— skipped.} else {— never reached.System.debug('Unknown region');— skipped.}— closes the chain.
Both work identically. switch reads more clearly once you're checking the same single value against many possibilities — the repeated region == in the if-chain adds visual noise that switch on region avoids entirely.
Trade-offs and when NOT to use this
switch only compares one value for equality against fixed matches — it can't express range checks like "greater than 50" the way if/else if can. Reach for switch specifically when you're matching one variable against several discrete, exact values; use if/else if for ranges or compound conditions.
Exercise
Declare String orderStatus = 'SHIPPED'. Write a switch statement debugging a different message for 'PENDING', 'SHIPPED', 'DELIVERED', and a when else for anything else.
Show hint
switch on orderStatus { when 'PENDING' { ... } when 'SHIPPED' { ... } ... }
Switch Statements Quiz
Leaderboard
- 1. Monde Masiza 1/1
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
A switch statement checks one value against several possible matches — often clearer than a long if/else if chain when every branch is testing the exact same variable for equality.