How do relational operators build boolean expressions, and what value does each one produce?
Topic 3.1 Boolean Expressions: evaluate expressions formed with the relational operators (<, >, <=, >=, ==, !=) that produce a boolean result of true or false.
A focused answer to AP CSA Topic 3.1, covering the six relational operators, how they compare numeric primitives to produce a boolean, the difference between == and =, comparing doubles, and how relational operators combine with arithmetic, with a fully traced worked evaluation.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 3.1) wants you to evaluate boolean expressions built from the relational operators. A relational operator compares two values and produces a result of type boolean - one of the two values true or false. These expressions are the conditions that drive every if statement and loop, so getting them exactly right is foundational for the whole of Units 3 and 4.
The six relational operators
For example, with int n = 5:
n < 10istruen > 10isfalsen <= 5istrue(equal counts)n >= 6isfalsen == 5istruen != 5isfalse
== is not =
In Java this confusion is usually caught by the compiler, because x = 5 produces an int, and an if requires a boolean. But inside boolean variables the bug can slip through, so always read == as "is equal to" and = as "becomes".
Precedence: arithmetic before relational
Relational operators have lower precedence than arithmetic operators. So in a + 1 > b * 2, Java first computes a + 1 and b * 2, then compares them. You rarely need parentheses around the arithmetic, but they make intent clear:
int a = 4;
int b = 3;
boolean r = a + 1 > b * 2; // (4 + 1) > (3 * 2) is 5 > 6 is false
Comparing doubles
Because double values are stored with limited precision, comparing them with == can give surprising results (for example, a computed 0.1 + 0.2 may not be exactly 0.3). On the AP exam, prefer <, >, <= and >= with double values, and treat == on double results with caution. Comparisons between an int and a double are allowed; the int is promoted to double first.
You cannot chain comparisons
A frequent beginner error is to write a range test as 0 < x < 100. Java evaluates 0 < x first, producing a boolean, and then tries boolean < 100, which does not compile. The correct form combines two separate comparisons with a logical operator: x > 0 && x < 100 (covered in Topic 3.5).
Try this
Q1. State the value of the expression 15 / 4 == 3. [1 point]
- Cue.
true-15 / 4is integer division giving3, and3 == 3.
Q2. Explain why if (x = 0) does not compile in Java, and what the author most likely intended. [2 points]
- Cue.
x = 0is an assignment that produces anint, butifrequires aboolean, so it fails to compile. The author meantif (x == 0), the equality comparison.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2021 (style)1 marksMultiple choice. Consider the following code segment.
```java
int a = 7;
int b = 3;
boolean result = (a % b == 1);
System.out.println(result);
```
What is printed?
(A) `true`
(B) `false`
(C) `1`
(D) `0`
(E) Nothing is printed; a compile-time error occurs.
Show worked answer →
The answer is (A).
% and == are both arithmetic-then-relational, but % (a multiplicative operator) binds tighter than ==, so a % b is evaluated first: 7 % 3 is 1. The expression becomes 1 == 1, which is true. That boolean is stored in result and printed as true. (C) and (D) wrongly expect an integer; a relational operator always yields a boolean, printed as the words true or false. (E) is wrong because the code compiles cleanly.
Markers reward knowing that arithmetic happens before the relational comparison, and that the result of == is a boolean.
AP 2019 (style)3 marksFree response (code writing). Two `int` variables `x` and `y` are given. Write a code segment that assigns to a `boolean` variable `between` the value `true` when `x` is strictly greater than 0 and `y` is strictly less than 100, and `false` otherwise. Use relational operators to build each condition, then store the combined result.
Show worked answer →
A 3-point question testing relational operators producing boolean values.
boolean firstOk = (x > 0);
boolean secondOk = (y < 100);
boolean between = firstOk && secondOk;
Point 1: x > 0 uses the strict greater-than relational operator and yields a boolean. Point 2: y < 100 uses strict less-than and yields a boolean. Point 3: combining the two boolean values (here with &&) and storing in between. A common error is writing 0 < x < 100, which does not compile in Java because 0 < x produces a boolean that cannot then be compared with < to an int.
Related dot points
- Topic 3.2 if Statements and Control Flow: use a one-way if statement so that a block of code runs only when its boolean condition is true, and trace the resulting flow of control.
A focused answer to AP CSA Topic 3.2, covering the syntax of a one-way if statement, the role of the boolean condition, why braces matter, the dangling-statement trap, and how control flows through and past the if, with a fully traced worked example.
- Topic 3.5 Compound Boolean Expressions: combine boolean expressions with the logical operators && (and), || (or) and ! (not), applying short-circuit evaluation.
A focused answer to AP CSA Topic 3.5, covering the logical operators && , || and !, their truth tables, operator precedence and short-circuit evaluation, why short-circuiting prevents errors, and how to trace a compound condition, with a fully worked example.
- Topic 3.6 Equivalent Boolean Expressions: apply De Morgan's laws and truth tables to produce equivalent boolean expressions and to simplify negations of compound conditions.
A focused answer to AP CSA Topic 3.6, covering De Morgan's laws for negating && and ||, using truth tables to prove equivalence, simplifying double negation, and rewriting conditions to remove a leading !, with a fully worked example.
- Topic 3.7 Comparing Objects: compare object references with == and !=, compare object contents with equals, and detect a null reference, understanding the difference between identity and equality.
A focused answer to AP CSA Topic 3.7, covering reference equality with == and !=, content equality with the equals method, comparing against null, why two equal-looking objects can be unequal by ==, and using equals/compareTo for Strings, with a fully worked example.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)