Only One Statement per Branch

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

No. The programmer probably wants the three statements after the else to be part of a false block, but has not used braces to show this.

Only One Statement per Branch

The false block was not put inside braces:

if ( num < 0 )
    System.out.println("The number " + num + " is negative.");   
else
    System.out.println("The number " + num + " is zero or positive.");  
    System.out.print  ("Positive numbers are greater ");  
    System.out.println("than zero. ");    

System.out.println("Good-bye for now");  

Our human-friendly indenting shows what we want, but the compiler ignores indenting. The compiler groups statements according to the braces. What it sees is the same as this:

if ( num < 0 )
    System.out.println("The number " + num + " is negative.");         // true-branch
else
    System.out.println("The number " + num + " is zero or positive");  // false-branch
System.out.print  ("Positive numbers are greater ");           // always executed  
System.out.println("or equal to zero. ");                      // always executed
System.out.println("Good-bye for now");                        // always executed

QUESTION 10:

How would you fix the problem?