June 2018
Beginner
722 pages
18h 47m
English
The previous code example of the last section can be expressed as a switch statement, as follows:
switch(x){ case 5: //do something break; case 7: //do something else break; case 12: //do something different break; case 50: //do something yet more different break; default: //do something completely different}
The type of the expression that returns the value of the x variable can be either char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type. Notice the break keyword. It forces the exit from the switch...case statement. Without it, the following statement, do something, would be executed. We will talk about the break statement in the Branching statements section later.
Read now
Unlock full access