Based on the dangling-else discussion in Exercise 4.27, modify the following code to produce the output shown.
Question:
Based on the dangling-else discussion in Exercise 4.27, modify the following code to produce the output shown. Use proper indentation techniques. You must not make any additional changes other than inserting braces and changing the code’s inden- tation. We’ve eliminated the indentation from the following code to make the problem more chal- lenging. [Note: It’s possible that no modification is necessary.]
if (y == 8)
if (x == 5)
System.out.println("@@@@@");
else
System.out.println("#####");
System.out.println("$$$$$");
System.out.println("&&&&&");
a) Assuming that x = 5 and y = 8, the following output is produced:
@@@@@
$$$$$
&&&&&
b) Assuming that x = 5 and y = 8, the following output is produced:
@@@@@
c) Assuming that x = 5 and y = 8, the following output is produced:
@@@@@
&&&&&
d) Assuming that x = 5 and y = 7, the following output is produced. [Note: The last three output statements after the else are all part of a block.]
#####
$$$$$
&&&&&
Exercise 4.27
The Java compiler always associates an else with the immediate- ly preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement
if (x > 5)
if (y > 5)
System.out.println("x and y are > 5");
else
System.out.println("x is <= 5");
appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the string "x and y are > 5". Otherwise, it appears that if x is not greater than 5, the else part of the if…else outputs the string "x is <= 5". Beware! This nested if…else statement does not execute as it appears. The compiler actually interprets the statement as
if (x > 5)
if (y > 5)
System.out.println("x and y are > 5");
else
System.out.println("x is <= 5");
in which the body of the first if is a nested if…else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string—"x and y are > 5"—is displayed. However, if the second con- dition is false, the string "x is <= 5" is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statement’s condition is false, the inner if…else is skipped and noth- ing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if…else statement to execute as it was originally intended.
Step by Step Answer:
Java How To Program Early Objects
ISBN: 9780134743356
11th Edition
Authors: Paul Deitel, Harvey Deitel