Hello Everyone, In this tutorial, we are going to learn about switch statement in the apex. In this section, I am providing detailed functional behavior of switch statement with syntax and appropriate code example and which type of switch statement expression can be?

After completing this tutorial, you’ll able to:
  • Define the switch statement.
  • Define the type of switch statement expression.
  • Write a proper switch statement code block.
So let's begin,

Switch Statement

In Salesforce apex provides a switch statement that checks whether an expression matches one of several values and branches accordingly. In apex switch statement evaluates the expression and executes the code block for the matching when value. If no value matches in when block then when else code block is executed. If you do not define any when else block, no action is taken.

Syntax is:
 switch on expression {
    when value1 {  // when block 1
        // code block 1
    } 
    when value2 {  // when block 2
        // code block 2
    }
    when value3 {  // when block 3
        // code block 3
    }
    when else {    // default block, optional
        // code block 4
    }
 }
The when value can be a single value, multiple values, or sObject types.

For example:
 when value1 {
 }
 when value2, value3 {
 }
 when TypeName VariableName {
 }
When Blocks

Each when block has a value that the expression is matched against. These values can take one of the following forms.

when literal {} (a when block can have multiple, comma-separated literal clauses)
when SObjectType identifier {}
when enum_value {} 

The value null is a legal value for all types and each when value must be unique.

When Else Blocks

If no when values match in switch statement expression, then when else block is executed. When else is the last block in the switch statement.

Expression Types

Apex switch statement expressions can be one of the following types.
  1. Integer
  2. Long
  3. String
  4. sObject
  5. Enum
Examples with Literals

Here I am providing switch statement expression examples with literals.

Single Value Example
 switch on i {
   when 2 {
       System.debug('when block 2');
   }
   when -3 {
       System.debug('when block -3');
   }
   when else {
       System.debug('default');
   }
 }
Null Value Example
 switch on i {
   when 2 {
       System.debug('when block 2');
   }
   when null {
       System.debug('bad integer');
   }
   when else {
       System.debug('default ' + i);
   }
 }
Multiple Values Example
 switch on i {
   when 2, 3, 4 {
       System.debug('when block 2 and 3 and 4');
   }
   when 5, 6 {
       System.debug('when block 5 and 6');
   }
   when 7 {
       System.debug('when block 7');
   }
   when else {
       System.debug('default');
   }
 }
Method Example
Following example switches on the result of a method call.
 switch on someInteger(i) {
   when 2 {
       System.debug('when block 2');
   }
   when 3 {
       System.debug('when block 3');
   }
   when else {
       System.debug('default');
   }
 }
sObjects Example
 switch on sobject {
   when Account a {
       System.debug('account ' + a);
   }
   when Contact c {
       System.debug('contact ' + c);
   }
   when null {
       System.debug('null');
   }
   when else {
       System.debug('default');
   }
 }
Enums Example
 switch on season {
   when WINTER {
       System.debug('boots');
   }
   when SPRING, SUMMER {
       System.debug('sandals');
   }
   when else {
       System.debug('none of the above');
   }
 }

See also:
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.