APrep Open the app

AP Computer Science A · Unit 2: Selection & Iteration

2.1 Boolean Expressions

Boolean Expressions Basics

A boolean expression evaluates to exactly one of two values: true or false. They're built using relational operators: `<`, `>`, `<=`, `>=`, `==` (equals), and `!=` (not equals). Comparing values with `==` and `!=` works for primitives like `int` and `double`, but comparing objects like `String` needs `.equals()` instead, since `==` checks references.

Logical Operators

Logical operators combine boolean expressions: `&&` (AND, true only if both are true), `||` (OR, true if at least one is true), and `!` (NOT, flips the value). These are used in compound boolean expressions to represent multiple conditions, such as `(x > 0) && (x < 10)`.

Truth Tables & Short-Circuit Evaluation

Truth tables show all possible outcomes of a boolean expression given its inputs. Java uses short-circuit evaluation: with `&&`, if the first operand is false, the second isn't evaluated; with `||`, if the first is true, the second isn't evaluated. This matters when the second operand has side effects, like a method call or array access that could throw an error.

Key terms

Boolean expression
An expression that evaluates to either true or false.
Relational operators
Operators like <, >, <=, >=, ==, and != used to compare values.
Logical operators
&& (AND), || (OR), and ! (NOT) — used to combine or invert boolean expressions.
Short-circuit evaluation
Java stops evaluating && or || as soon as the result is determined, skipping the remaining operand.
String comparison with ==
Compares object references, not content; use .equals() to compare String content.

Try an AP-style question

Code

int x = 5;
boolean b = x > 3 && x < 5;

What is b?

  1. 5
  2. true
  3. false
  4. An error
Show the answer

C. x < 5 is false.

Practice 8 AP-style questions with instant explanations, flashcards and spaced review.

Study 2.1 free in APrep