2.3 Boolean Expressions

Boolean Expressions and Comparison in Java

Unit 2: Variables, Decisions, and Input · Lesson 2.3 · about 30 minutes · no coding experience needed

Before a program can make a decision it needs a question with a yes or no answer. That is all a boolean expression is, and there is exactly one character that turns writing them into a nightmare if you get it wrong.

What you will be able to do

  • Write a comparison that produces true or false
  • Explain the difference between = and ==
  • Combine two tests with and, or and not

Words you will need

Condition
In plain words A question with a yes or no answer.
More precisely An expression whose value is a boolean.
Comparison operator
In plain words A symbol that asks a question about two values.
More precisely == != < > <= >=
Logical operator
In plain words Glue for joining two questions.
More precisely && is and, || is or, ! is not.

Build it step by step

Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.

  1. Step 1

    On your screen An editor showing comparison expressions with their true or false results annotated.

    int score = 10;
    score > 5     // true
    score < 5     // false
    score == 10   // true
    score != 10   // false

    Each of these is a question, and the answer is always exactly true or false.

    `>` and `<` you already know. `==` asks "are these the same?". `!=` asks "are these different?".

    Note there is no `=` in that list at all.

  2. Step 2

    On your screen A side-by-side comparison of a single equals and a double equals with their meanings labeled.

    score = 10;   // PUTS 10 into score
    score == 10;  // ASKS whether score is 10

    These look almost identical and do completely different things.

    One `=` is an instruction: change the box. Two `==` is a question: is the box holding this?

    Say it out loud when you read code. "Becomes" for one, "is equal to" for two. Using `=` where you meant `==` is the most common logic error in this entire course, and in a condition Java usually rejects it with `incompatible types`, which is its way of saying "you gave me an instruction where I wanted a question".

  3. Step 3

    On your screen Two conditions combined with the and operator, showing the result is true only when both parts are true.

    boolean hasKey = true;
    int score = 10;
    
    hasKey && score > 5   // true, both are true
    hasKey && score > 50  // false, the second part fails

    `&&` is AND. Both sides must be true or the whole thing is false. One false anywhere poisons it.

    `||` is OR. It only needs one side to be true.

    The two vertical bars are on the backslash key on most keyboards.

  4. Step 4

    On your screen The not operator inverting a boolean value.

    boolean gameOver = false;
    !gameOver             // true
    !gameOver && hasKey   // true

    `!` is NOT. It flips true to false and false to true.

    Read `!gameOver` as "not game over". Once you read it that way, `!gameOver && hasKey` is just "the game is not over and I have the key", which is a sentence rather than a puzzle.

Mistakes almost everyone makes here

These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.

Common wrong idea `=` and `==` are interchangeable.

What is actually true One assigns, two compares. They are unrelated operations.

Why it matters This is the most common logic error in the course, and Java's error message for it does not mention the equals sign at all.

Common wrong idea `&&` means "and then", so order does not matter.

What is actually true Both sides must be true. If the left is false, Java does not even look at the right.

Why it matters That short-circuiting is what makes a safety check like `hasKey && useKey()` work.

Common wrong idea `score > 5` needs to be stored somewhere to be useful.

What is actually true It IS a value: true or false. It can be used directly in an if.

Why it matters Students who think conditions must be stored write far more code than they need.

Common wrong idea You compare text with == the same as numbers.

What is actually true For text, == asks a different question than you probably mean.

Why it matters This course avoids String comparison for that reason. Flagged here so it is not a surprise later.

Check your understanding

Answer these before moving on. They are graded instantly and you can retry.

  1. 1. Which operator ASKS whether two values are the same?

  2. 2. int lives = 3; What is the value of `lives != 3`?

  3. 3. hasKey is true and score is 2. What is `hasKey && score > 5`?

  4. 4. hasKey is true and score is 2. What is `hasKey || score > 5`?

  5. 5. gameOver is false. What is `!gameOver`?

Fill in the code

A door should open only when the player has the key AND the alarm is not ringing. Fill in the missing operators.

boolean canOpen = hasKey  alarmRinging;
Stuck? Open a hint for each blank
  • Blank 1: Both parts have to be true.
  • Blank 2: One character that flips a true into a false.

Lesson quiz

This one counts toward your progress. Take it when the section above makes sense.

  1. 1. Which line ASKS a question rather than giving an instruction?

  2. 2. a is 5 and b is 10. What is `a < b && b < 20`?

  3. 3. When is `x || y` false?

  4. 4. A student writes `if (score = 10)` and Java refuses it. Why?

The short version

  • A condition is a question whose answer is true or false.
  • `=` becomes. `==` is equal to. They are not interchangeable.
  • && needs both sides. || needs one. ! flips.
  • A comparison IS a value and can be used directly.

If you get stuck

These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.

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]