go to previous page   go to home page   go to next page hear noise

Fill in the two blanks so that the nested if's make the correct choice.

Answer:

See below.


How the Three-way Choice Works

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):" );



Nested If

The first if makes a choice between its true branch and its false branch. Its false branch is complicated, but that doesn't matter. Each branch of an if statement can be as complicated as needed. The fragment shows the first if's true branch in blue and its false branch in red.

For example, if (count+1) is equal to 2, the true branch is picked. The variable suffix gets "nd", and the false branch is completely skipped.



QUESTION 10:

The expression (count+1) is always greater than 1. For each of the following possible values of (count+1) which string will be assigned to suffix?

    value of count+1which suffix?
if ( count+1  == 2  )
  suffix = "nd";
else
  if ( count+1 == 3  )
    suffix = "rd";
  else
    suffix = "th";
2
3
4
5