Making Choices
So far in this book, all the programs have run straight through from start to finish, without making any decisions along the way. In this chapter, you discover two Java statements that let you create some variety in your programs. The if statement lets you execute a statement or a block of statements only if some conditional test turns out to be true. And the switch statement lets you execute one of several blocks of statements depending on the value of an integer variable.
The if statement relies heavily on the use of boolean expressions, which are expressions that yield a simple true or false result. Because you can't do even the simplest if statement without a boolean expression, this chapter begins by showing you how to code simple boolean expressions that test the value of a variable. Later, after looking at the details of how the if statement works, I revisit boolean expressions to see how to combine them to make complicated logical decisions. Then, I get to the switch statement.
Warning | You're going to have to put your thinking cap on for much of this chapter, as most of it plays with logic puzzles. Find yourself a comfortable chair in a quiet part of the house, turn off the TV, and pour yourself a cup of coffee. |
Using Simple Boolean Expressions
All if statements, as well as several of the other control statements that I describe in Book II, Chapter 5 (while, do, and for), use boolean expressions to determine whether to execute or skip a statement (or a block of statements). A boolean expression is a Java expression that, when evaluated, returns a boolean value-either true or false.
As you discover later in this chapter, boolean expressions can be very complicated. However, most of the time, you use simple expressions that compare the value of a variable with the value of some other variable, a literal, or perhaps a simple arithmetic expression. This comparison uses one of the relational operators listed in Table 4-1. All these operators are binary operators, which means they work on two operands.
Operator | Description |
---|---|
== | Returns true if the expression on the left evaluates to the same value as the expression on the right. |
!= | Returns true if the expression on the left does not evaluate to the same value as the expression on the right. |
< | Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right. |
<= | Returns true if the expression on the left evaluates to a value that is less than or equal to the expression on the right. |
> | Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right. |
>= | Returns true if the expression on the left evaluates to a value that is greater than or equal to the expression on the right. |
A basic boolean expression has this form:
expression relational - operator expression
Java evaluates a boolean expression by first evaluating the expression on the left, then evaluating the expression on the right, and finally applying the relational operator to determine if the entire expression evaluates to true or false.
Here are some simple examples of relational expressions. For each example, assume that the following statements were used to declare and initialize the variables:
int i = 5;
int j = 10;
int k = 15;
double x = 5.0;
double y = 7.5;
double z = 12.3;
Here are the sample expressions, along with their results (based on the values supplied):
Value | Explanation | |
---|---|---|
i == 5 | true | The value of i is 5. |
i == 10 | false | The value of i is not 10. |
i == j | false | i is 5, and j is 10, so they are not equal. |
i == j − 5 | true | i is 5, and j − 5 is 5. |
i > 1 | true | i is 5, which is greater than 1. |
j == i * 2 | true | j is 10, and i is 5, so i * 2 is also 10. |
x = i | true | Casting allows the comparison, and 5.0 is equal to 5. |
k <> | false | Casting allows the comparison, and 15 is greater than 12.3. |
i * 2 <> | false | i * 2 is 10, which is not less than 7.5. |
Warning | Note that the relational operator that tests for equality is two equal signs in a row (==). A single equal sign is the assignment operator. When you're first learning Java, you may find yourself typing the assignment operator when you mean the equals operator, like this: if (i = 5) |
Oops. But Java won't let you get away with this, so you have to correct your mistake and recompile the program. At first, doing so seems like a nuisance. The more you work with Java, the more you realize that it really is a nuisance, but one you can get used to.
Warning | Another important warning: Do not test strings using any of the relational operators listed in Table 4-1, including the equals operator. You're probably going to feel tempted to test strings like this: inputString == "Yes" |
Note, however, that this is not the correct way to compare strings in Java. You find out the correct way in the section "Comparing Strings" later in this chapter.
No comments:
Post a Comment