Learn Java the Hard Way (Second Edition)

Exercise 14: Compound Boolean Expressions

Sometimes we want to use logic more complicated than just “less than” or “equal to”. Imagine a grandmother who will only approve you dating her grandchild if you are older than 25 and younger than 40 and either rich or really good looking. If that grandmother was a programmer and could convince applicants to answer honestly, her program might look a bit like this:

 1 import java.util.Scanner;
 3 public class ShallowGrandmother {
 4     public static void main( String[] args ) {
 5         Scanner keyboard = new Scanner(System.in);
 6         int age;
 7         double income, cute;
 8         boolean allowed;
10         System.out.print( "Enter your age: " );
11         age = keyboard.nextInt();
13         System.out.print( "Enter your yearly income: " );
14         income = keyboard.nextDouble();
16         System.out.print( "How cute are you, on a scale from 0.0 to 10.0? " );
17         cute = keyboard.nextDouble();
19         allowed = ( age > 25 && age < 40 && ( income > 50000 || cute >= 8.5 ) );
21         System.out.println( "Allowed to date my grandchild? " + allowed );
22     }
23 }

What You Should See

So we can see that for complicated Boolean expressions you can use parentheses to group things, and you use the symbols && to mean “AND” and the symbols || to mean “OR”.

I know what you are thinking: using & (an “ampersand” or “and sign”) to mean “AND” makes a little sense, but why two of them? And whose idea was it to use || (“pipe pipe”) to mean “OR”?!?

Fortunately for you, you don’t need to know any of that. You just need to remember what to type and get it right.

This next little bit is going to be a little bit weird, because I’m going to show you the “truth tables” for AND and OR, and you’ll have to think of “AND” as an operation that is being performed on two values instead of a conjunction.

Here is the truth table for AND:

Inputs   Output
A B A && B
true true true
true false false
false true false
false false false

You read the tables this way: let’s pretend that our shallow grandmother has decided that she will only go on a cruise if it is cheap AND the alcohol is included in the price. So we will pretend that statement A is “the cruise is cheap” and statement B is “the alcohol is included”. Each row in the table is a possible cruise line.

Row 1 is a cruise where both statements are true. Will grandmother be excited about cruise #1? Yes! “Cruise is cheap” is true and “alcohol is included” is true, so “grandmother will go” (A && B) is also true.

Cruise #2 is cheap, but the alcohol is not included (statement B is false). So grandmother isn’t interested: (A && B) is false when A is true and B is false.

Clear? Now here is the truth table for OR:

Inputs   Output
A B A || B
true true true
true false true
false true true
false false false

Let’s say that grandmother will buy a certain used car if it is really cool-looking OR if it gets great gas mileage. Statement A is “car is cool looking”, B is “good miles per gallon” and the result, A OR B, determines if it is a car grandmother would want.

Car #1 is awesome-looking and it also goes a long way on a tank of gas. Is grandmother interested? Heck, yes! We can say that the value true ORed with the value true gives a result of true.

In fact, the only car grandmother won’t like is when both are false. An expression involving OR is only false when BOTH of its components are false.

(There is no video for this Study Drill yet.)

“Learn Java the Hard Way” is ©2013–2016 Graham Mitchell