Notes
Apex includes a switch statement designed to check whether an expression matches any of several values and branch accordingly.
The "when" value can represent a single value, multiple values, or sObject types.
The switch statement evaluates the expression and executes the code block associated with the matching "when" value. If no value matches, the code block under "when else" is executed. In the absence of a "when else" block, no action is taken.
Here's an example:
String dayOfWeek = 'Monday';
switch on dayOfWeek {
when 'Monday' {
System.debug('It\'s Monday!');
}
when 'Tuesday' {
System.debug('It\'s Tuesday!');
}
when 'Wednesday' {
System.debug('It\'s Wednesday!');
}
when 'Thursday' {
System.debug('It\'s Thursday!');
}
when 'Friday' {
System.debug('It\'s Friday!');
}
when 'Saturday' {
System.debug('It\'s Saturday!');
}
when 'Sunday' {
System.debug('It\'s Sunday!');
}
when else {
System.debug('Invalid day of the week!');
}
}
Output: It's Monday!
Video
Video does not exists.