Boolean logic & if

AP Computer Science A · Unit 2: Control flow

Boolean logic & if notes

Fact-checked by a second, independent AI review.

Covers how to build and evaluate boolean expressions with relational and logical operators, and how to control program flow using if, if-else, and nested conditionals in Java for AP CS A.

Boolean type
true or false values used in conditions and control statements.
Relational operators
Use ==, !=, <, <=, >, >= to compare primitives in conditions.
Logical operators
&& (AND), || (OR), ! (NOT); NOT has highest precedence, then AND, then OR.
If statement
If (condition) { ... } runs the block when the condition is true.
If-else and nesting
Else provides an alternative; you can nest blocks and use else if for multiple conditions.
Short-circuit evaluation
Java may stop evaluating the rest of a boolean expression once the outcome is determined.

Remember it: Not, then And, then Or — remember the precedence: ! > && > ||.

Exam tip: Markers expect correct use of boolean expressions, proper braces, clear if/else/else-if chains, and correct equality checks; watch for short-circuit behavior and avoiding common off-by-one/logic errors.

Syllabus checklist

  • Boolean logic & if: core ideas
  • Terms and typical exam questions for "Control flow" in AP Computer Science A (Java
  • College Board)

Practice questions

  1. 1. In Java, which operator short-circuits when the first operand is false?

    Answer checked

    Show answer

    Answer: &&. && stops evaluating if the left side is false.

  2. 2. True or False: In Java, the && operator uses short-circuit evaluation.

    Answer checked

    Show answer

    Answer: True. Yes, && does short-circuit when the left side is false.

  3. 3. The boolean expression that is true only if both A and B are true is ____.

    Answer checked

    Show answer

    Answer: conjunction. Conjunction is a logical AND.

  4. 4. Given score = 85, which line prints 'B' in: if (score>=90) ... else if (score>=80) ... else ...

    Answer checked

    Show answer

    Answer: B. 85 is >=80 but not >=90, so the B branch runs.

  5. 5. What is the result of (3 + 5) ÷ 2 using int division in Java?

    Answer checked

    Show answer

    Answer: 4. (3+5)=8; 8/2=4 with int division.

Practise boolean logic & if as a game

Hearts, combos, streaks and an AI tutor that explains every answer. Start free.

Start free

More from Unit 2: Control flow