Example 1-6 is
another version of the FizzBuzz
game. This version uses a switch
statement instead of nested if/else
statements to determine what its output should be for each number.
Take a look at the example first, then read the explanation of
switch
.
Example 1-6. FizzBuzz2.java
package je3.basics; /** * This class is much like the FizzBuzz class, but uses a switch statement * instead of repeated if/else statements **/ public class FizzBuzz2 { public static void main(String[ ] args) { for(int i = 1; i <= 100; i++) { // count from 1 to 100 switch(i % 35) { // What's the remainder when divided by 35? case 0: // For multiples of 35... System.out.print("fizzbuzz "); // print "fizzbuzz". break; // Don't forget this statement! case 5: case 10: case 15: // If the remainder is any of these case 20: case 25: case 30: // then the number is a multiple of 5 System.out.print("fizz "); // so print "fizz". break; case 7: case 14: case 21: case 28: // For any multiple of 7... System.out.print("buzz "); // print "buzz". break; default: // For any other number... System.out.print(i + " "); // print the number. break; } } System.out.println( ); } }
The switch
statement
acts like a switch operator at a busy rail yard, switching a train (or
the execution of a program) to the appropriate track (or piece of
code) out of many potential tracks. A switch
statement is often an alternative to
repeated if/else
statements, but it
only works when the value being tested is an integer (i.e., long
, float
, double
, boolean
, and reference types such as
String
objects are not allowed) and
when the value is being tested against constant values. The basic
syntax of the switch
statement
is:
switch(expression
) {statements
}
The switch
statement is
followed by an expression
in parentheses
and a block of code in curly braces. After evaluating the
expression
, the switch
statement executes certain code
within the block, depending on the integral value of the expression.
How does the switch
statement know
where to start executing code for which values? This information is
indicated by case
: labels and with
the special default
: label. Each
case
: label is followed by an
integral value. If the expression
evaluates
to that value, the switch
statement
begins executing code immediately following that case
: label. If there is no case
: label that matches the value of the
expression, the switch
statement
starts executing code following the default
: label, if there is one. If there is
no default
: label, switch
does nothing.
The switch
statement is an
unusual one because each case doesn’t have its own unique block of
code. Instead, case
: and default
: labels simply mark various entry
points into a single large block of code. Typically, each label is
followed by several statements and then a break
statement, which causes the flow of
control to exit out of the block of the switch
statement. If you don’t use a
break
statement at the end of the
code for a label, the execution of that case “drops through” to the
next case. If you want to see this in action, remove the break
statements from Example 1-6 and see what happens
when you run the program. Forgetting break
statements within a switch
statement is a common source of bugs.
Get Java Examples in a Nutshell, 3rd Edition 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.