If statements can be nested inside other if statements. Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements. The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
if (boolean expression)
{ // This nested if is executed if outer if is true
if (boolean expression)
{
statement;
}
}
Subsection2.4.1Multiway selection (else if)
A single if/else statement allows us to select between 2 branches of code. With nested if/else statements, we can pick between 3 or more branches of code. A multi-way selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multi-way selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
if (x < 0)
{
System.out.println("x is negative");
}
else if (x == 0)
{
System.out.println("x is zero");
}
else
{
System.out.println("x is positive");
}
if (x < 0)
{
System.out.println("x is negative");
}
else if (x == 0)
{
System.out.println("x is zero");
}
else
{
System.out.println("x is positive");
}
The else-if connection is necessary if you want to hook up conditionals together. In the following code, there are 4 separate if statements instead of the if-else-if pattern. Will this code print out the correct grade? First, trace through the code to see why it prints out the incorrect grade. Use the Code Lens button. Then, fix the code by adding in 3 elseβs to connect the if statements and see if it works.
Finish the following code so that it prints βPlug in your phone!β if the battery is below 50, βUnplug your phone!β if it is at or above 100, and βAll okay!β otherwise. Change the battery value to test all 3 conditions.
Sometimes with nested ifs we find a dangling else that could potentially belong to either if statement. The rule is that the else clause will always be a part of the closest unmatched if statement in the same block of code, regardless of indentation.
// Nested if with dangling else
if (boolean expression)
if (boolean expression)
Do statement;
else // belongs to closest if
Do other statement;
Activity2.4.7.
Try the following code with a dangling else. Notice that the indentation does not matter to the compiler (but you should make it your habit to use good indentation just as a best practice). How could you get the else to belong to the first if statement?
. It was a text-based interactive fiction game where you had to make your way through an elaborate cave. The program only understood one word or phrase commands like north, south, enter, take, etc. You can try playing adventureβ3β
In a game like Adventure, else if statements can be used to respond to commands from the user like n, s, e, w. Try the program below (or in an interactive input IDE like JuiceMindβ5β
). This current adventure game asks the user whether they want to move n, s, e, or w, but right now only the north direction is coded. It leads to a new method called forest().
In the main method, add in else if statements to go in the directions of βsβ for south, βeβ for east, βwβ for west, and an else statement that says βYou canβt go in that directionβ. Be creative and come up with different locations in each direction. Have each direction call a static method that you will write. The forest() and sea() methods are shown as examples for two of the diretions. You will need to change the input below the code to s or e or w and then run to test these branches. How many test-cases are needed to test all branches of your code? You can also connect locations to one another by calling their methods. If you have time, you can expand this game further with more nested if/else statements and come up with a different adventure location.
This is a text adventure game that lets the user move in 4 different directions. Right now, it only lets the user move north. Add in else if statements to go in the directions of "s" for south, "e" for east, "w" for west, and an else statement that says "You canβt go in that direction". Be creative and come up with different locations in static methods below main for each direction. There are 5 TODO steps below.
(AP 2.4.A.2) The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
(AP 2.4.A.3) A multi-way selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multi-way selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
Assume an int variable x has been properly declared and initialized. Which of the following code segments will print out βHighβ if x is 66 and above, βMediumβ is x is between 33-65, and βLowβ if x is below 33.