Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.810 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. The six relational operators
  3. == is not =
  4. Precedence: arithmetic before relational
  5. Comparing doubles
  6. You cannot chain comparisons
  7. Try this

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 < 10 is true
  • n > 10 is false
  • n <= 5 is true (equal counts)
  • n >= 6 is false
  • n == 5 is true
  • n != 5 is false

== 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 / 4 is integer division giving 3, and 3 == 3.

Q2. Explain why if (x = 0) does not compile in Java, and what the author most likely intended. [2 points]

  • Cue. x = 0 is an assignment that produces an int, but if requires a boolean, so it fails to compile. The author meant if (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

Sources & how we know this