Lesson 2.2: Boolean Expressions

Unit 2 · Lesson 2.2 · Code Mechanics

Lesson 2.2: Boolean Expressions

🕑 20–25 min · 8 Practice Questions · 4 Mastery Questions · Output Predictor Game

Key Vocabulary

Term Definition
boolean expression Any expression that evaluates to exactly true or false. The fundamental input to every if statement and loop. (EK 2.2.A.3)
relational operator An operator that compares two values and produces a boolean result. Java has six: ==, !=, <, >, <=, >=.
== (equality) Tests whether two values are equal. With primitives, compares values directly. With objects, compares memory addresses, not content. (EK 2.2.A.1)
!= (inequality) Tests whether two values are different. Evaluates to true when operands are not equal, false when they are equal. (EK 2.2.A.1)
primitive type A basic data type stored directly as a value: int, double, boolean, char. Relational operators compare primitive values directly.
reference type A type whose variable stores a memory address pointing to an object, not the object's data directly. Examples: String, ArrayList. Using == on reference types compares addresses.
operator precedence The order in which Java evaluates operators in an expression. Arithmetic operators (*, /, %, then +, -) are evaluated before relational operators (<, >, <=, >=, ==, !=).

What Is a Boolean Expression?

A boolean expression is any expression that evaluates to exactly one of two values: true or false. This is the most important concept in all of Unit 2 because every selection statement and every loop depends on a boolean expression to determine what runs next. Without boolean expressions, programs cannot make decisions or repeat work.

The boolean data type in Java has exactly two possible values: true and false. These are keywords, not strings — they have no quotes. A boolean variable stores one of these values the same way an int stores a number.

CED Connection (EK 2.2.A.3)

"An expression involving relational operators evaluates to a Boolean value." This is the core of 2.2: learn the six relational operators, understand what each compares, and be able to evaluate any combination of them to true or false.

The Six Relational Operators

Java provides exactly six relational operators. Every AP CSA exam that tests Topic 2.2 uses one or more of these. You need to evaluate them instantly for any numeric values.

Operator Meaning & Notes
== Equal to. true when both sides have the same value.
⚠ NOT the same as assignment =. Using = inside an if is a compile error in Java.
!= Not equal to. true when both sides have different values. Logical opposite of ==.
< Less than. true when left side is strictly smaller. Does NOT include equal. (EK 2.2.A.2)
> Greater than. true when left side is strictly larger. Does NOT include equal. (EK 2.2.A.2)
<= Less than or equal to. true when left is smaller OR equal. Includes the boundary value.
>= Greater than or equal to. true when left is larger OR equal. Includes the boundary value.
⚠ Most common boundary trap: score >= 60 includes 60; score > 60 does not.

Equality and Inequality: == and != (EK 2.2.A.1)

The == operator answers the question: "do these two things have the same value?" The != operator answers: "are these two things different?" For primitive types, both operators compare the actual stored values directly and produce an unambiguous result.

Worked Example — == and != with Primitives

int a = 5;
int b = 5;
int c = 8;

System.out.println(a == b);   // true  — same value
System.out.println(a == c);   // false — different values
System.out.println(a != c);   // true  — they differ
System.out.println(a != b);   // false — they are the same

boolean sameAge = (a == b);   // boolean variable stores the result
System.out.println(sameAge);  // true

Key point: a relational expression produces a boolean value that can be stored in a variable, passed to a method, or used directly in an if condition.

The critical distinction: primitives vs. reference types (EK 2.2.A.1)

EK 2.2.A.1 explicitly covers both cases. With primitive types (int, double, boolean, char), == compares the actual stored values. With reference types (objects like String, ArrayList), == compares memory addresses — whether two variables point to the same object in memory, not whether their contents are equal.

Worked Example — == on Strings (Reference Trap)

String s1 = new String("hello");
String s2 = new String("hello");
String s3 = s1;

System.out.println(s1 == s2);       // false — different objects in memory
System.out.println(s1 == s3);       // true  — same object (same address)
System.out.println(s1.equals(s2));  // true  — same content

s1 and s2 contain identical text but are separate objects. == sees two different addresses and returns false. The correct way to compare String content is .equals(), covered in detail in Lesson 2.6.

AP Trap: = vs. == (Most Common Java Beginner Error)

= is assignment — it stores a value into a variable. == is comparison — it produces a boolean. Writing if (a = 5) in Java is a compile error because a = 5 returns an int, not a boolean. This is intentionally different from C/C++ where it silently compiles and causes bugs. The AP exam exploits this distinction in spot-the-error questions.

Numeric Comparisons: <, >, <=, >= (EK 2.2.A.2)

These four operators establish ordering relationships between numeric values. They apply to int and double variables, literal numbers, and any arithmetic expression that produces a number. The result is always a boolean.

Worked Example — All Six Operators with score = 85

int score = 85;

System.out.println(score == 85);  // true
System.out.println(score != 85);  // false
System.out.println(score < 90);   // true
System.out.println(score > 90);   // false
System.out.println(score <= 85);  // true  — equal to boundary
System.out.println(score >= 90);  // false — not equal or above 90
System.out.println(score >= 85);  // true  — exactly at boundary

The boundary cases (<= and >= when values are exactly equal) are the most frequently tested. Always check: does the problem say "at least", "no more than", "strictly less", or "exceeds"? Each phrase maps to a specific operator.

English-to-operator translation

AP exam questions describe conditions in plain English. You must instantly translate to the correct Java operator. The most frequently confused pairs are > vs. >= and < vs. <=:

English phrase Java operator
"is equal to" / "same as" ==
"is not equal to" / "differs from" !=
"is less than" / "is below" / "is under" <
"is greater than" / "is above" / "exceeds" >
"at most" / "no more than" / "does not exceed" <=
"at least" / "no less than" / "minimum of" >=

Operator Precedence with Relational Operators

Relational operators have lower precedence than arithmetic operators. This means Java always finishes all arithmetic first, then evaluates the comparison. You do not need parentheses to make a + b > c * 2 work correctly — arithmetic happens first automatically. However, using parentheses for clarity is considered good style.

Worked Example — Precedence in Practice

int a = 3, b = 4;

// Arithmetic runs BEFORE relational:
System.out.println(a + b > 6);    // 3+4=7, then 7>6 = true
System.out.println(a * b == 12);   // 3*4=12, then 12==12 = true
System.out.println(b - a != 0);    // 4-3=1, then 1!=0 = true
System.out.println(a * 2 <= b);   // 3*2=6, then 6<=4 = false

// Storing the result:
boolean result = a + b >= 7;      // 7 >= 7 is true
System.out.println(result);        // true

Storing and Using Boolean Results

A relational expression produces a boolean value that can be used in three ways: directly inside an if or loop, stored in a boolean variable for reuse, or passed as a method argument. Storing boolean results in named variables often makes code more readable and is a pattern you will use throughout FRQ solutions.

Three Ways to Use a Boolean Expression

int score = 75;

// 1. Directly in an if:
if (score >= 60) {
    System.out.println("Passed");
}

// 2. Stored in a variable:
boolean passed = score >= 60;
boolean excellent = score >= 90;
if (passed) {
    System.out.println("Passed");
}

// 3. In a while loop condition:
int countdown = 5;
while (countdown > 0) {
    System.out.println(countdown);
    countdown--;
}

AP Trap: Boundary Values (< vs. <=)

The single most common error on AP MCQ trace questions involving comparisons. score >= 60 is true when score is exactly 60. score > 60 is false when score is exactly 60. Always test boundary values explicitly. If a question gives score = 60 and asks which condition is true, check both options carefully.

AP Trap: Using == to Compare Strings

== on String objects tests memory address equality, not content. Two String objects with identical text may return false for ==. This is tested directly on the AP exam. Always use .equals() to compare String content. This is covered fully in Lesson 2.6.

AP Trap: Chained Comparisons Don't Work in Java

In math, you write 0 < x < 10 to mean "x is between 0 and 10." This does not work in Java. 0 < x < 10 would try to evaluate 0 < x first (producing a boolean), then compare that boolean to 10 — a type error. The correct Java version requires &&: x > 0 && x < 10. This is covered in Topic 2.5.

Real-World Connection

Boolean expressions are the decision engine behind everything a computer does. A credit card processor checks balance >= chargeAmount before approving a transaction. A game engine checks playerHealth > 0 to determine if the game continues. A sensor system checks temperature >= threshold to trigger an alert. Every conditional in software — from a mobile app to a space shuttle controller — is ultimately a boolean expression evaluated to true or false. Topic 2.2 gives you the foundation for writing every condition you will ever use in AP CSA.

Summary

  • Boolean expressions evaluate to true or false. They are the input to every if statement and loop.
  • Six relational operators: ==, !=, <, >, <=, >=. All produce boolean results. (EK 2.2.A.2, 2.2.A.3)
  • Primitives: == and != compare actual values. (EK 2.2.A.1)
  • Reference types (objects): == compares memory addresses, not content. Use .equals() for content. (EK 2.2.A.1)
  • Precedence: arithmetic operators evaluate before relational operators. a + b > c computes a + b first.
  • Boundary trap: < excludes the boundary; <= includes it. Test boundary values explicitly.
  • Chained comparisons like 0 < x < 10 do not work in Java. Use && instead.
Tier 2 · AP Practice

Practice Questions

MCQ 1
What is the value of the expression 7 != 7?
A 7
B false
C true
D A compile error
B. != evaluates to true only when the values are different. Since 7 equals 7, 7 != 7 evaluates to false.
MCQ 2
Given int a = 10; int b = 10;, which expression evaluates to true?
A a > b
B a < b
C a != b
D a >= b
D. Since a == b == 10, a >= b is true (10 ≥ 10). A is false (10 > 10), B is false, C is false (they are equal).
MCQ 3
What is printed?

int temp = 98;
boolean isFever = temp > 100;
System.out.println(isFever);
A false
B true
C 98
D 100
A. 98 > 100 is false. The boolean variable isFever stores false, which is what gets printed.
MCQ 4
Which expression does NOT evaluate to a boolean value?
Predict which one is the odd one out before reading the options.
A x == 0
B x != y
C x + y
D x >= y
C. x + y is an arithmetic expression that evaluates to a numeric value (int or double), not a boolean. All expressions using relational operators (==, !=, <, >, <=, >=) evaluate to boolean.
MCQ 5
What is printed?

int p = 4;
int q = 8;
System.out.println(p * 2 == q);
A false
B true
C 8
D Compile error: cannot use == with expressions
B. p * 2 = 4 * 2 = 8. Then 8 == 8 = true. Arithmetic is evaluated before relational operators.
MCQ 6
Consider this code. What is the output?

int n = 15;
System.out.println(n % 2 == 0);
System.out.println(n > 10);
A false
true
B true
false
C true
true
D false
false
A. 15 % 2 = 1, and 1 == 0 is false. Then 15 > 10 is true.
MCQ 7
Which of the following correctly declares and initializes a boolean variable that is true when score is at least 60?
A boolean pass = score;
B int pass = score >= 60;
C boolean pass = (score >= 60) = true;
D boolean pass = score >= 60;
D. A relational expression like score >= 60 evaluates to a boolean, which can be assigned directly to a boolean variable. A assigns an int to boolean (type error). B assigns boolean to int (type error). C uses = on an expression (compile error).
MCQ 8
Which pair of expressions always evaluates to the same value?

I. a == b and b == a
II. a < b and b > a
III. a <= b and a < b
A I only
B II only
C I and II only
D I, II, and III
C. I: a == b and b == a are always equivalent (equality is symmetric). II: a < b and b > a are always equivalent. III: a <= b is true when a == b, but a < b is false when a == b — not equivalent.
PRACTICE WITH A GAME — CHOOSE ONE:
Truth Table — Boolean Expressions
Click each Result cell to toggle T/F. Fill the entire column then submit.
Table 1 of 3Score: 0
🎉 Done!
You got 0 of 3 correct.
Output Predictor — Boolean Expressions
Type the exact output (true or false). Capitalization matters.
Question 1 of 6Score: 0

🎉 Done!
You got 0 of 6 correct.
Tier 3 · AP Mastery

Mastery: Boolean Expression Analysis

What is printed?

int x = 10;
int y = 20;
boolean result = x + 5 > y - 3;
System.out.println(result);
Evaluate both sides before choosing.
A true
B 15
C 17
D false
D. Left side: 10 + 5 = 15. Right side: 20 - 3 = 17. 15 > 17 is false.
A student writes: boolean inRange = (1 <= x) == (x <= 10); to check if x is between 1 and 10. When x = 5, this evaluates to true. When x = 0, what happens?
Trace each sub-expression separately when x = 0.
A inRange is true, correctly identifying 0 as out of range
B inRange is true even though 0 is out of range — a logic error
C inRange is false, correctly identifying 0 as out of range
D A compile error occurs because you cannot use == on boolean expressions
B. When x = 0: 1 <= 0 is false, 0 <= 10 is true. So false == true is false... wait — actually let’s re-trace. When x = 15: 1 <= 15 is true, 15 <= 10 is false, true == false = false. When x = 0: false == true = false. The trap is x = -5: false == false = true — incorrectly says -5 is in range. The expression is a logic error for negative values below 1.
Which code segment correctly checks whether grade is exactly 100?
Predict the answer before reading options.
A if (grade = 100)
B if (grade >= 100 <= 100)
C if (grade == 100)
D if (grade =! 100)
C. A uses = (assignment), which is a compile error in an if condition. B chains relational operators incorrectly (Java does not support that syntax). D uses =! which is not a valid operator (you want !=). Only C uses == correctly.
What is printed?

int a = 6;
int b = 3;
System.out.println(a / b == 2);
System.out.println(a % b == 0);
Evaluate integer division and remainder separately.
A true
true
B true
false
C false
true
D false
false
A. 6 / 3 = 2 (integer division), so 2 == 2 = true. 6 % 3 = 0, so 0 == 0 = true. Both lines print true.
Extension

Boolean Variables as Flags

Experienced programmers often use flag variables — boolean variables that track state throughout a program. For example: boolean found = false; at the start of a search, then set to true when the item is located. How does storing a boolean expression in a named variable improve code readability compared to putting the expression directly in an if statement?

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]