Can a single if-else statement choose one of three options?

A good answer might be:

No---an if-else statement makes a binary decision, a choice between two options.

Nested If for a Three-way Choice

To make a three-way choice, a nested  if  is used. This is where an if-else statement is part of a the true-branch (or false-branch) of another if-else statement. So the nested if will execute only when the outer if has already made a choice between two branches. Here is a program fragment that does that to make a choice of one of the three Strings.
String suffix;

 . . . . .    // count is the number of integers added so far.
 . . . . .    // count+1 will be the next integer to read
 

if ( count+1  ______________  )
    suffix = "nd";
else
    if ( count+1  ______________  )          // false-branch of first if
        suffix = "rd";                       // false-branch of first if
    else                                     // false-branch of first if
        suffix = "th";                       // false-branch of first if

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

Complete the if statements so that:

  • when (count+1) is 2 the suffix "nd" is chosen;
  • when (count+1) is 3 the suffix "rd" is chosen;
  • and when (count+1) is 4 or higher the suffix "th" is chosen.

QUESTION 8:

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

Click Here after you have answered the question