Nested if Statement

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   go to next page

For each of the following possible values of (count+1), which branch of the first if will be executed?

Answer:

  • 2      true branch
  • 3      false branch
  • 4      false branch
  • 5      false branch

Nested if Statement

When (count+1) is equal to 3, the false branch is executed.

if ( count+1  == 2  )
  suffix = "nd"
else
  if ( count+1 == 3  )
    suffix = "rd";
  else
    suffix = "th";

System.out.println( "Enter the " + 
    (count+1) + suffix + " integer (enter 0 to quit):" );

The false branch consists of an if statement. When this nested if statement is executed, its true branch will execute, selecting the String "rd" for suffix. The next statement to execute is the println.

QUESTION 11:

What suffix is chosen if (count+1) is equal to 5?