---
schema_version: "1"
id: 61135aaae0ba45b09324454856bdc02126c3a17b159289478c301ba13e31c119
title: Switch Statements
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/langCon_apex_switch.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 1e84489410a41edc08638ff2028bc17a577114434e880b3633e47bec1e4e6d35
description: Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly.
---
# Switch Statements

Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly.

The 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 {
}

The switch statement evaluates the expression and executes the code block for the matching when value. If no value matches, the when else code block is executed. If there isn’t a when else block, no action is taken.

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

There is no fall-through. After the code block is executed, the switch statement exits.

Apex switch statement expressions can be one of the following types.

- Integer
- Long
- sObject
- String
- Enum

## 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.

Each when value must be unique. For example, you can use the literal x only in one when block clause. A when block is matched one time at most.

## When Else Block

If no when values match the expression, the when else block is executed.

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

Salesforce recommends including a when else block, especially with enum types, although it isn’t required. When you build a switch statement using enum values provided by a managed package, your code might not behave as expected if a new version of the package contains additional enum values. You can prevent this problem by including a when else block to handle unanticipated values.

If you include a when else block, it must be the last block in the switch statement.

## Examples with Literals

You can use literal when values for switching on Integer, Long, and String types. String clauses are case-sensitive. For example, “orange” is a different value than “ORANGE.”

**Single Value Example**

The following example uses integer literals for when values.

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**

Because all types in Apex are nullable, a when value can be null.

switch on i {
   when 2 {
       System.debug('when block 2');
   }
   when null {
       System.debug('bad integer');
   }
   when else {
       System.debug('default ' + i);
   }
}

**Multiple Values Examples**

The Apex switch statement doesn’t fall-through, but a when clause can include multiple literal values to match against. You can also nest Apex switch statements to provide multiple execution paths within a when clause.

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**

Instead of switching on a variable expression, the 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');
   }
}

## Example with sObjects

Switching on an sObject value allows you to implicitly perform instanceof checks and casting. For example, consider the following code that uses if-else statements.

if (sobject instanceof Account) {
    Account a = (Account) sobject;
    System.debug('account ' + a);
} else if (sobject instanceof Contact) {
    Contact c = (Contact) sobject;
    System.debug('contact ' + c);
} else {
    System.debug('default');
}

You can replace and simplify this code with the following switch statement.

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');
   }
}

![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)

#### Note

You can use only one sObject type per when block.

## Example with Enums

A switch statement that uses enum when values doesn’t require a when else block, but it is recommended. You can use multiple enum values per when block clause.

switch on season {
   when WINTER {
       System.debug('boots');
   }
   when SPRING, SUMMER {
       System.debug('sandals');
   }
   when else {
       System.debug('none of the above');
   }
}