AP® Computer Science A · Unit 2 · Topic 2.5
Combining Conditions with AND, OR, and NOT
AND (&&), OR (||), and NOT (!) combine boolean expressions into one true-or-false answer, letting a program test several conditions at once inside a single if statement.
A single comparison like x > 0 only answers one question. Real decisions usually depend on more than one fact at a time: is x positive AND less than 10? Is a user logged in OR an admin? Combining conditions lets a program fold several true/false questions into the one answer an if statement needs.
Each operator folds truth values down differently. && (AND) is true only when both sides are true. || (OR) is true when at least one side is true. ! (NOT) flips a single boolean, turning true into false and false into true. Java evaluates the pieces first, then applies the operator to the resulting booleans, never to the original numbers.
This topic sits right after simple if/else comparisons and right before short-circuit evaluation, where Java skips evaluating the right side of && or || once the answer is already decided. It also sets up loop conditions in Unit 2, since while and for loops test compound conditions the same way every pass. Getting comfortable folding multiple booleans into one here makes trace tables for loops much easier to read later.
Key terms
- Boolean expression
- An expression that evaluates to exactly one of two values, true or false.
- && (AND)
- Operator that produces true only when both of its boolean operands are true.
- || (OR)
- Operator that produces true when at least one of its boolean operands is true.
- ! (NOT)
- Operator that flips a single boolean value from true to false or false to true.
- Truth table
- A table listing every combination of input truth values alongside the resulting output for an expression.
Watch it explained
- How it worksHow do AND, OR, and NOT each fold truth values down into a single answer?
- Worked exampleFind the value of x > 0 && x < 10 for x = 12 and then for x = 5.
- Worked exampleFind when !(a || b) comes out true, using a truth table with all four rows.
- Common mistakeWhy isn't 0 < x < 10 a legal way to test a range in Java?
Check yourself
End of topic.