Java switch-case statement

A switch statement allows a variable to be tested for equality against multiple values. It provides the case for the different blocks of code to be executed. Switch expression and case value must be of the same type. There must be at least one case or multiple cases with unique case values. In the end, it can have a default case which is optional that is executed if no cases are matched. It can also have an optional break that is useful to exit the switch otherwise it continues to the next case.

Syntax of switch statements:

Example of nested-if statement:

Output: