Do you give up? The answer is, there’s no right answer. What happens depends on the way you write the program. When the scores are equal, the condition hankees > socks
is false
. So the program’s flow of execution drops down to the else
clause. That clause displays the Socks score first and the Hankees score second.
importjava.util.Scanner;
<strong>import static java.lang.System.in;</strong>
<strong>import static java.lang.System.out;</strong>
classTwoTeams {
public static void main(String args[>) {
Scanner keyboard = new Scanner(<strong>in</strong>);
inthankees, socks;
out.print("Hankees and Socks scores? ");
hankees = keyboard.nextInt();
socks = keyboard.nextInt();
out.println();
if (hankees> socks) {
<strong>out.print("Hankees: ");</strong>
<strong>out.println(hankees);</strong>
<strong>out.print("Socks: ");</strong>
<strong>out.println(socks);</strong>
} else {
<strong>out.print("Socks: ");</strong>
<strong>out.println(socks);</strong>
<strong>out.print("Hankees: ");</strong>
<strong>out.println(hankees);</strong>
}
keyboard.close();
}
}
The program doesn’t have to work this way. If you take this code and change hankees > socks
to hankees >= socks
, then, in case of a tie, the Hankees score comes first.
Suppose that you want a bit more control. When the scores are equal, you want to see an It's a tie
message. To do this, think in terms of a three-pronged fork. You have a prong for a Hankees win, another prong for a Socks win, and a third prong for a tie. You can write this code in several different ways, but one way that makes lots of sense is the code below.
import java.util.Scanner;
import static java.lang.System.out;
class WinLoseOrTie {
public static void main(String args[>) {
Scanner keyboard = new Scanner(System.in);
int hankees, socks;
out.print("Hankees and Socks scores? ");
hankees = keyboard.nextInt();
socks = keyboard.nextInt();
out.println();
<strong> </strong>
<strong>if</strong> (hankees > socks) {
out.println("Hankees win...");
out.print("Hankees: ");
out.println(hankees);
out.print("Socks: ");
out.println(socks);
} <strong>else if</strong> (socks > hankees) {
out.println("Socks win...");
out.print("Socks: ");
out.println(socks);
out.print("Hankees: ");
out.println(hankees);
} <strong>else</strong> {
out.println("It's a tie...");
out.print("Hankees: ");
out.println(hankees);
out.print("Socks: ");
out.println(socks);
}
keyboard.close();
}
}
This code illustrates a way of thinking about a problem. You have one question with more than two answers. (The question is “Who wins?” and the answers are “Hankees,” “Socks,” or “Neither.”) The problem begs for an if statement, but an if
statement has only two branches — the true
branch and the false
branch. So you combine alternatives to form cascading if
statements.
Here, the format for the cascading if
statements is
if (<em>Condition1</em>) {
<em>SomeStatements</em>
} else if (<em>Condition2</em>) {
<em>OtherStatements</em>
} else {
<em>EvenMoreStatements</em>
}
In general, you can use else if
as many times as you want:
<strong>if</strong> (hankeesWin) <strong>{</strong>
out.println("Hankees win...");
out.print("Hankees: ");
out.println(hankees);
out.print("Socks: ");
out.println(socks);
<strong>} else if</strong> (socksWin) <strong>{</strong>
out.println("Socks win...");
out.print("Socks: ");
out.println(socks);
out.print("Hankees: ");
out.println(hankees);
<strong>} else if</strong> (isATie) <strong>{</strong>
out.println("It's a tie...");
out.print("Hankees: ");
out.println(hankees);
out.print("Socks: ");
out.println(socks);
<strong>} else if</strong> (gameCancelled) <strong>{</strong>
out.println("Sorry, sports fans.");
<strong>} else {</strong>
out.println("The game isn't over yet.");
<strong>}</strong>
Nothing is special about cascading if
statements. This isn’t a new programming language feature. Cascading if
statements take advantage of a loophole in Java — a loophole about omitting curly braces in certain circumstances. Other than that, cascading if
statements just give you a new way to think about decisions within your code.