5.2 if Statements
Key terms: block
5.2.1 Syntax and Semantics
Java provides three statements for conditional execution: if, if-else, and switch. The
general syntax for an if statement is:
if (E) {
// conditionally executed code
}
Here E denotes a Boolean expression. The statements enclosed in curly braces form a block, a sequence of one or more statements treated as a single unit. If a block contains only a single statement, the braces may be omitted, though including them is generally considered good practice.
Listing 5.2.1 uses if statements to simulate the first roll of a popular dice game. The outcome
depends on the total rolled, so different messages are displayed under different conditions.
Listing 5.2.1 - ComeOutRoll.java
package chap05.sect2;
import java.util.concurrent.ThreadLocalRandom;
/**
* Simulates the first roll in the game of Craps. The player rolls two dice and the outcome depends
* on the sum: natural (7 or 11) - player wins; craps (2, 3, or 12) - player loses; otherwise, the
* game continues according to a different rule.
*
* @author Drue Coles
*/
public class ComeOutRoll {
public static void main(String[] args) {
ThreadLocalRandom rand = ThreadLocalRandom.current();
int die1 = rand.nextInt(1, 7);
int die2 = rand.nextInt(1, 7);
final int comeOutRoll = die1 + die2;
System.out.printf("You rolled %d + %d = %d. %n", die1, die2, comeOutRoll);
final String winMessage = "Natural. You win.";
final String loseMessage = "Craps. You lose.";
String result = "The game continues."; // provisional
// each condition is tested independently
if (comeOutRoll == 7) {
result = winMessage;
}
if (comeOutRoll == 11) {
result = winMessage;
}
if (comeOutRoll == 2) {
result = loseMessage;
}
if (comeOutRoll == 3) {
result = loseMessage;
}
if (comeOutRoll == 12) {
result = loseMessage;
}
System.out.println(result);
}
}
Output 5.2.1a
You rolled 4 + 3 = 7.
You win.
Output 5.2.1b
You rolled 2 + 1 = 3.
You lose.
Output 5.2.1c
You rolled 3 + 2 = 5.
The game continues.
Statements within a block are always indented to emphasize that they are elements of a higher-level structure. This convention is observed in most programming languages. The Java compiler ignores whitespace so consistent indentation is not enforced at the language level, but the use of indentation to reflect the logical structure of code is generally regarded as one of the most important ingredients of good coding style.
Do not confuse the equals operator (==) with the assignment operator (=). The former is used to
compare two values whereas the latter assigns a value to a variable. To illustrate, consider the
following code:
x = x + 1;
It would not make sense to read this as x equals x + 1: two numbers that differ by 1, like 6
and 7, are not equal. But that is not what the code says. It says, x gets x + 1. When this
statement is executed, the expression on the right-hand side is evaluated and the result is assigned
to x — that is, the value of x is incremented by 1.