Tips for Forming Bigger and Better Conditions in Java - dummies

Tips for Forming Bigger and Better Conditions in Java

By Barry Burd

Consider this example if you need to write better conditions in Java. In Backgammon and other dice games, rolling 3 and 5 isn’t the same as rolling 4 and 4, even though the total for both rolls is 8. The next move varies, depending on whether you roll doubles.

To get the computer to roll two dice, you execute myRandom.nextInt(6) + 1 two times. Then you combine the two rolls into a larger, more complicated if statement.

So to simulate a Backgammon game (and many other, more practical situations) you need to combine conditions.

If die1 + die2 equals 8 and die1 equals die2, ...

You need things like and and or — things that can wire conditions together. Java has operators to represent these concepts.

Operator Symbol Meaning Example
&& and 4 < age && age < 8
|| or age < 4 || 8 < age
! not !eachKidGetsTen

image0.jpg

Combined conditions, like the ones in Table 10-1, can be mighty confusing. That’s why you need to tread carefully when you use such things. Here’s a short explanation of each example in the table:

  • 4 < age && age < 8

    The value of the age variable is greater than 4 and is less than 8. The numbers 5, 6, 7, 8, 9 … are all greater than 4. But among these numbers, only 5, 6, and 7 are less than 8. So only the numbers 5, 6, and 7 satisfy this combined condition.

  • age < 4 || 8 < age

    The value of the age variable is less than 4 or is greater than 8. To create the or condition, you use two pipe symbols. On many U.S. English keyboards, you can find the pipe symbol immediately above the Enter key (the same key as the backslash, but shifted).

    In this combined condition, the value of the age variable is either less than 4 or greater than 8. For example, if a number is less than 4, the number satisfies the condition. Numbers like 1, 2, and 3 are all less than 4, so these numbers satisfy the combined condition.

    Also, if a number is greater than 8, the number satisfies the combined condition. Numbers like 9, 10, and 11 are all greater than 8, so these numbers satisfy the condition.

  • !eachKidGetsTen

    You might be confused by the exclamation point. You may think that !eachKidGetsTen means, “Yes, each kid does get ten.” But that’s not what this expression means. This expression says, “The variable eachKidGetsTen does not have the value true.” In Java and other programming languages, an exclamation point stands for negative, for no way, for not.

    A boolean variable’s value is either true or false. Because ! means not, the expressions eachKidGetsTen and !eachKidGetsTen have opposite values. So when eachKidGetsTen is true, !eachKidGetsTen is false (and vice versa).

Java’s || operator is inclusive. This means that you get true whenever the thing on the left side is true, the thing on the right side is true, or both things are true. For example, the condition 2 < 10 || 20 < 30 is true.

In Java, you can’t combine comparisons the way you do in ordinary English. In English, you may say, “We’ll have between three and ten people at the dinner table.” But in Java, you get an error message if you write 3 <= people <= 10. To do this comparison, you need to something like 3 <= people && people <= 10.