The switch Statement

In this lesson, we will introduce the Java switch statement that provides a clear way to define a multiway decision.

What is a switch statement?

We can use a switch statement when a decision depends on the value of an expression. This value’s data type must be either

  • char
  • An integer type such as int
  • An enumeration, or
  • A string

Example 1

For example, if the int variable dayNumber has a value ranging from 1 to 7 to indicate one of the days Sunday through Saturday, we need not use an if-else statement to produce the day of the week as a string. Instead, we can use the following switch statement:

Get hands-on with 1400+ tech skills courses.