Skip to main content
← AP CSA Study Guide

Code Scholars Tutoring · AP Computer Science A

2.1: Boolean Operators

A comparison produces a boolean. Negating a conjunction changes && to || and negates both operands; negating a disjunction does the reverse.

College Board Unit 2

How it works

Boolean expressions combine comparisons with && (and), || (or), and ! (not). Precedence runs ! first, then &&, then ||, so a || b && c evaluates as a || (b && c). De Morgan laws give the equivalences you need for negation: !(a && b) is !a || !b, and !(a || b) is !a && !b. Recognizing that two differently written conditions are logically identical is a tested skill in itself.

Build a truth table for an unfamiliar condition rather than reasoning about it in prose.

Common error

Negating a compound condition by negating each part without swapping and with or.

Worked example

Java

int age = 16;
boolean member = false;
System.out.println(age >= 18 || member);
System.out.println(!(age >= 18 || member));

Output

false
true

Both conditions in the disjunction are false, so its negation is true.

Exercises

2.1.1 · Trace

Determine the exact output. Explain the Java rule or state changes that produce it.

Java

boolean a = true;
boolean b = false;
System.out.println(!a || b && a);
Show solution

Output

false

Evaluate !, then &&, then ||. Each side of the final disjunction is false.

2.1.2 · Write code

For int x = 10, print whether x lies outside the inclusive interval 3 through 8. Use De Morgan's law to write the condition without !.

Show solution

One valid solution is shown below. Other implementations satisfying the contract are also valid.

Java

int x = 10;
boolean outside = x < 3 || x > 8;
System.out.println(outside);
Practice this topic: MCQ →

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic