Skip Top Navigation Bar

Switch Expressions - Determine a Value

Before you learn about switch expressions, you may want to review the tutorial on Switch Statements

Switch Expressions

How are switch expressions different from switch statements?

  • Switch expressions yield a value, typically assigned to a variable.
  • Switch expressions require a ; at the end.

Basic Structure of a Switch Expression

<type> returnVariable = switch (<variable>) { 
   case <value> -> //value to assign if variable == value ;
   //additional case statements
   default -> //value to assign if no case values match the value of variable ;
}; 

The above example re-written with lambda:

Resources

Calculator - Using Conditionals and Lambda Mini-Lab