Can a single if-else statement choose one of three options?
No. An if-else statement makes a binary decision, a choice between two options.
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: