Beginning Programming with Java For Dummies
Book image
Explore Book Buy On Amazon
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;

<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.

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

<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.

About This Article

This article is from the book:

About the book author:

Dr. Barry Burd holds an M.S. in Computer Science from Rutgers University and a Ph.D. in Mathematics from the University of Illinois. Barry is also the author of Beginning Programming with Java For Dummies, Java for Android For Dummies, and Flutter For Dummies.

This article can be found in the category: