5.2. The switch statement

[3.4] Use a switch statement

In this section, you’ll learn how to use the switch statement and see how it compares to nested if-else constructs. You’ll learn the right ingredients for defining values that are passed to the switch labels and the correct use of the break statement in these labels.

5.2.1. Create and use a switch statement

You can use a switch statement to compare the value of a variable with multiple values. For each of these values, you can define a set of statements to execute.

The following example uses a switch statement to compare the value of the variable marks with the literal values 10, 20, and 30, defined using the case keyword:

A switch statement can define multiple case

Get OCA Java SE 8 Programmer I Certification Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.