Good news Dev folks! Apex switch statement is coming in Winter 18 release.

No more hastle of nested or a ladder of If-else blocks it can be simplified with help of new Switch statements.

Salesforce has delivered one more from IdeaExchange Add "Switch" or "Case" Statement to Apex


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 {		  // when else block, optional
        // code block 4

What are key difference between using Switch statement vs If-else?

  • Switch is more compact and readable
  • Switch is very easy to use when there are multiple choices which helps avoids nested if-else blocks
  • Unlike If-else, Switch statement jumps to directly to value rather executing every statement in If-else until condition met true.
  • If-else can have range of values as expression but Switch doesnt do that

NOTE: This feature was in Developer preview

For more info you can refer salesforce release doc here