From the free course AP Computer Science A on Lykke

2.5 Compound Boolean Expressions

Key concepts: Compound Boolean expressions · Logical AND operator (&&) · Logical OR operator (||) · else if structures · Boolean expression evaluation · Conditional statements · Algorithm implementation · Determining program output · Guess-checker algorithms · Counting correct digits in correct locations

A compound Boolean expression combines two or more Boolean conditions into one expression that evaluates to either true or false. In Java, logical operators let one conditional statement express several acceptable or required situations at once.

2.5 Compound Boolean Expressions

A compound Boolean expression combines two or more Boolean conditions into one expression that evaluates to either true or false. In Java, logical operators let one conditional statement express several acceptable or required situations at once.

Learning Objective 2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.

The three logical operators are ! (not), && (and), and || (or). Each produces a Boolean result.

Operator Meaning Expression is true when
!a NOT a is false
a && b AND both a and b are true
`a b`

The operators have a defined order of precedence: ! is evaluated first, then &&, then ||. Parentheses should be used when they make the intended grouping clearer, especially when && and || appear together.

Requiring several conditions with &&

The logical AND operator, &&, requires both conditions to be true. For example, the expression x >= 9 && x <= 17 is true only when x is at least $9$ and at most $17$.

if (x >= 9 && x <= 17)
{
    System.out.println("The value is in the permitted range.");
}
else
{
    System.out.println("The value is outside the permitted range.");
}

Suppose x is 12. The first condition, x >= 9, is true. The second, x <= 17, is also true, so the complete expression is true and the program prints The value is in the permitted range. If x is 20, the first condition is true but the second is false; because AND requires both conditions, the complete expression is false.

Misconception check: x >= 9 && x <= 17 does not mean “either comparison may be true.” It describes an inclusive range, so values must satisfy both boundaries.

Accepting alternatives with ||

The logical OR operator, ||, makes an expression true when the left condition, the right condition, or both conditions are true. It is useful when an algorithm allows more than one way to proceed.

Consider a program that permits access either when the number of dogs has reached its maximum or when a value lies in the range from $9$ through $17$:

if (dogs == maxDogs || (x >= 9 && x <= 17))
{
    System.out.println("Condition accepted.");
}
else
{
    System.out.println("Condition rejected.");
}

The parentheses make the intended structure visible: first evaluate the range condition with &&, then combine it with dogs == maxDogs using ||. If dogs == maxDogs is true, the entire expression is true regardless of the range. If the dog limit has not been reached, the range condition must be true.

Compound expressions and else if

An else if structure can represent alternative conditional logic. When several branches lead to the same result, the following structure expresses the same logical options as the compound expression above:

if (dogs == maxDogs)
{
    System.out.println("Condition accepted.");
}
else if (x >= 9 && x <= 17)
{
    System.out.println("Condition accepted.");
}
else
{
    System.out.println("Condition rejected.");
}

The compound version combines the alternatives into one Boolean test:

if (dogs == maxDogs || (x >= 9 && x <= 17))
{
    System.out.println("Condition accepted.");
}

These versions produce the same output because both accepted cases print the same message. They are not automatically interchangeable when the branches perform different actions: an else if structure can select a distinct action for each true condition, while a single compound condition usually treats the alternatives as one category.

Misconception check: || does not mean “exactly one.” It is inclusive OR: the expression is also true when both conditions are true.

Tracing evaluation and execution order

Skill 3.A: Determine the result or output based on statement execution order in an algorithm. To trace a compound condition, evaluate each smaller condition, apply operator precedence, choose the branch, and then continue with the statements inside that branch.

For dogs == maxDogs || (x >= 9 && x <= 17), suppose dogs is 4, maxDogs is 6, and x is 15:

  1. dogs == maxDogs is false.
  2. x >= 9 is true.
  3. x <= 17 is true.
  4. true && true is true.
  5. false || true is true.
  6. The first branch executes, printing Condition accepted.

Skill 2.A: Write program code to implement an algorithm. A reliable construction process is to translate the algorithm’s words directly: “both” suggests &&; “either” or “one of these options” suggests ||; “not” suggests !. Then test boundary values such as $9$, $17$, $8$, and $18$ rather than testing only an ordinary middle value.

Retrieval check

What does the following program print when dogs is 3, maxDogs is 3, and x is 2?

if (dogs == maxDogs || (x >= 9 && x <= 17))
{
    System.out.println("A");
}
else
{
    System.out.println("B");
}

Answer: It prints A. The left side of || is already true because dogs == maxDogs; therefore the complete compound expression is true, even though x is outside the range.

2.5 Compound Boolean Expressions - AP Computer Science A - image 1
2.5 Compound Boolean Expressions - AP Computer Science A - image 1
2.5 Compound Boolean Expressions - AP Computer Science A - diagram 1
2.5 Compound Boolean Expressions - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis