Problem

(Dangling-else Problem) Determine the output for each of the given sets of code when x is...

(Dangling-else Problem) Determine the output for each of the given sets of code when x is 9 and y is 11 and when x is 11 and y is 9. The compiler ignores the indentation in a Java program. Also, the Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({}). On first glance, you may not be sure which if a particular else matches—this situation is referred to as the "dangling-else problem." We've eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply the indentation conventions you've learned.]

a) if (x<10)

if (y > 10)

System.out.println("*****");

else

System.out.println("#####");

System.out.println("$$$$$");

b) if (x<10)

{

if (y > 10)

System.out.println("*****");

}

else

{

System.out.println("#####");

System.out.println("$$$$$");

}

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 4
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT