This book is now obsolete Please use CSAwesome instead.

5.2. Three or More Options

You can even pick between 3 or more possibilites. Just add else if for each possibility after the first if and before the last possibility, the else.

../_images/Condition-three.png

Figure 1: The order that statements execute in a conditional with 3 options: if, else if, and else

Run the code below and try changing the value of x to get each of the three possible lines in the conditional to print.

Note

Another way to handle 3 or more conditional cases is to use the switch and break keywords, but these will not be on the exam. For a tutorial on using switch see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html.

Check your understanding

You have attempted of activities on this page