Sub topic 1
Build Conditions That Evaluate to true or false
A condition is a test with a yes-or-no answer. In Java, those answers are written as true and false. For example, “Is the score at least 70?” is a condition. If the score is 85, the answer is true. If the score is 60, the answer is false.
The Java type boolean holds only true or false. You can store the result of a test in a boolean variable or use the test directly when your program needs to make a choice. In this chapter, you’ll print the results so you can see how Java works them out.
Compare Two Values
An expression is a piece of code that produces a value. A comparison expression tests two values and produces a boolean result. An operator is a symbol that tells Java what to do. For example, the comparison operator >= tells Java to check whether the value on the left is greater than or equal to the value on the right.
| Operator | Test | Example |
|---|---|---|
| `==` | Equal to | `5 == 5` is `true` |
| `!=` | Not equal to | `5 != 5` is `false` |
| `<` | Less than | `5 < 8` is `true` |
| `<=` | Less than or equal to | `5 <= 5` is `true` |
| `>` | Greater than | `5 > 8` is `false` |
| `>=` | Greater than or equal to | `5 >= 5` is `true` |
Pay attention to the exact limit. If a passing score is 70, use score >= 70. The test score > 70 leaves out the score of 70 itself.
Combine Tests with AND, OR, and NOT
A logical operator joins or changes boolean values. Java has three logical operators you’ll use often:
&&means AND. Both tests must betrue.||means OR. At least one test must betrue. It’s fine if both aretrue.!means NOT. It flipstruetofalseorfalsetotrue.
Suppose a visitor needs a ticket and must either be 18 or older or have an adult with them. Save this program as EntryCheck.java and run it.
public class EntryCheck {
public static void main(String[] args) {
int age = 16;
boolean hasTicket = true;
boolean hasAdultWithThem = true;
boolean isAdult = age >= 18;
boolean canEnter = hasTicket && (isAdult || hasAdultWithThem);
boolean needsTicket = !hasTicket;
System.out.println(canEnter);
System.out.println(needsTicket);
}
}The first line printed is true. The visitor is not 18, so isAdult is false. But hasAdultWithThem is true, so the OR test passes. The visitor also has a ticket, so the AND test passes. The second line is false because ! flips the true value of hasTicket.
Group Related Tests with Parentheses
Parentheses group the two ways to meet the age rule. This keeps the ticket rule separate: every visitor needs a ticket. If you leave out the parentheses, Java groups AND before OR. Then hasTicket && isAdult || hasAdultWithThem would let a visitor with an adult enter even without a ticket.
To check whether a number is in a range, write two full tests. For example, use age >= 0 && age < 18, not 0 <= age < 18. Java does not read a chain of comparisons the way you might in math.
Know When Java Skips a Test
Short-circuit evaluation means Java skips the test on the right when the test on the left already gives the answer.
- With
&&, afalseleft side is enough. Both sides cannot betrue, so Java skips the right side. - With
||, atrueleft side is enough. At least one side is alreadytrue, so Java skips the right side.
This matters when the second test is only safe after the first one passes. Say you have 12 treats and want to check if each person can get at least three. If there are zero people, you must not divide by that count. Java stops with an error if you divide a whole number by zero.
public class TreatCheck {
public static void main(String[] args) {
int treats = 12;
int people = 0;
boolean enoughPerPerson = people > 0 && treats / people >= 3;
boolean needsAttention = people == 0 || treats / people < 3;
System.out.println(enoughPerPerson);
System.out.println(needsAttention);
}
}Save this as TreatCheck.java and run it. The first test prints false because people > 0 is false. The second prints true because people == 0 is true. In both tests, Java skips the division. If you put the division first, the check for zero would come too late.
Practice
Exercise
In EntryCheck, set age to 18 and hasTicket to false. Leave hasAdultWithThem as true. What will the program print?
Show answer
It prints false, then true. The visitor meets the age rule but has no ticket, so canEnter is false and needsTicket is true.
Exercise
In TreatCheck, change people to 4. Does Java skip either division? What will the program print?
Show answer
Neither division is skipped. The left side of AND is true, and the left side of OR is false, so Java needs both right-side tests. The program prints true, then false, because 12 divided by 4 is 3.