By Barry A. Burd

Here’s a Java riddle: You have two baseball teams — the Hankees and the Socks. You want to display the teams’ scores on two separate lines, with the winner’s score listed first. (On the computer screen, the winner’s score is displayed above the loser’s score.) What happens when the scores are tied?

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;

import static java.lang.System.in;

import static java.lang.System.out;

 

classTwoTeams {

 

public static void main(String args[]) {

Scanner keyboard = new Scanner(in);

inthankees, socks;

 

out.print("Hankees and Socks scores? ");

hankees = keyboard.nextInt();

socks = keyboard.nextInt();

out.println();

 

if (hankees> socks) {

out.print("Hankees: ");

out.println(hankees);

out.print("Socks: ");

out.println(socks);

} else {

out.print("Socks: ");

out.println(socks);

out.print("Hankees: ");

out.println(hankees);

}

 

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.

using cascading if in java
Go, team, go!

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();

if (hankees > socks) {

out.println("Hankees win...");

out.print("Hankees: ");

out.println(hankees);

out.print("Socks: ");

out.println(socks);

} else if (socks > hankees) {

out.println("Socks win...");

out.print("Socks: ");

out.println(socks);

out.print("Hankees: ");

out.println(hankees);

} else {

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 (Condition1) {

SomeStatements

} else if (Condition2) {

OtherStatements

} else {

EvenMoreStatements

}

In general, you can use else if as many times as you want:

if (hankeesWin) {

out.println("Hankees win...");

out.print("Hankees: ");

out.println(hankees);

out.print("Socks: ");

out.println(socks);

} else if (socksWin) {

out.println("Socks win...");

out.print("Socks: ");

out.println(socks);

out.print("Hankees: ");

out.println(hankees);

} else if (isATie) {

out.println("It's a tie...");

out.print("Hankees: ");

out.println(hankees);

out.print("Socks: ");

out.println(socks);

} else if (gameCancelled) {

out.println("Sorry, sports fans.");

} else {

out.println("The game isn't over yet.");

}

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.