Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 5: Boolean Expressions and Conditionals

Programs make decisions, and decisions in Java are written with boolean expressions: expressions that evaluate to true or false. The comparison operators — ==, !=, <, >, <=, >= — compare numbers and produce booleans, while the logical operators combine them: && means and, || means or, and ! means not. A boolean expression such as age >= 18 && hasTicket is true only when both parts are true, and the operators have a precedence you must respect: ! binds tightest, then the comparisons, then &&, then ||, with parentheses always available to make your meaning explicit.

The if statement uses a boolean expression to choose between blocks of code. In its simplest form, if (condition) { ... } runs the block only when the condition is true. Adding else gives an alternative, and chaining else if lets you test several conditions in order, running the first block whose condition is true and skipping the rest. The chain matters: once a condition matches, the later ones are not even tested, so the order of the tests determines the result. Always use braces, even for a single statement, because a missing brace is the source of some of the most confusing bugs in beginner code.

Two boolean pitfalls appear on almost every exam. The first is writing = instead of == in a condition, which assigns rather than compares and is a compile-time error for booleans but a silent bug in some other contexts. The second is comparing objects with == when equals is needed, which you met in Unit 1. A third, subtler trap is short-circuit evaluation: in a && expression, if the left side is false, the right side is never evaluated, and in a || expression, if the left side is true, the right side is never evaluated. This is usually a feature — it lets you write guards such as x != 0 && 10 / x > 1 safely — but it means the right side cannot be relied on for side effects.

De Morgan's laws, which you will study in depth in the next lesson, let you rewrite negated conditions, and they are worth a first look here: !(a && b) is the same as !a || !b, and !(a || b) is the same as !a && !b. When a condition gets complicated, simplify it on paper before writing it in code, and test it with a small truth table: a condition that is wrong for one combination of inputs is wrong, however reasonable it looks. In free-response answers, trace conditionals by evaluating the boolean expression for the given values, step by step, and following the branch it selects.

Comparing double values with == is dangerous, because decimal numbers are stored approximately in the computer, and two values that look equal may differ in the last stored digit. A calculation such as 0.1 + 0.2 gives 0.30000000000000004 rather than exactly 0.3, so testing it with == against 0.3 would be false. The safe way to compare doubles is to check whether they are close enough, as in Math.abs(a - b) < 0.0001, which tests whether the difference is smaller than a tolerance you choose. The same caution applies to money: amounts of money are better stored as int cents than as double dollars, because int arithmetic is exact and double arithmetic is not.

A worked example

Worked example: the code below decides a ticket price from an age. Trace it for age 15: the first condition, age < 12, is false, so the else if is tested, age < 18 is true, and the price is set to 8, with the final else skipped entirely.

int age = 15;
int price;
if (age < 12)
{
    price = 5;
}
else if (age < 18)
{
    price = 8;
}
else
{
    price = 12;
}
System.out.println(price);  // prints 8

The chain works because the conditions are tested in order and only the first true branch runs. If the tests had been written in the opposite order, age < 18 first, then a 10-year-old would also match the first test and get the wrong price. When you write a chain, order the conditions from the most specific to the most general, or make the ranges explicit so they cannot overlap. And remember that an if statement without an else does nothing when its condition is false — the program simply moves on.

Key Terms

Boolean expression
An expression evaluating to true or false, built with comparison and logical operators.
if-else chain
A sequence of if and else if tests where the first true branch runs and the rest are skipped.
Short-circuit evaluation
In &amp;&amp; and ||, the right side is skipped when the left side already decides the result.
Logical operators
&& (and), || (or), and ! (not), which combine or negate boolean values.
Comparison operators
==, !=, <, >, <=, and >=, which compare values and produce booleans.

Discussion Questions

  1. Why should you always use braces with an if statement, even for one line?
  2. In x != 0 && 10 / x > 1, why does short-circuit evaluation matter?
  3. What goes wrong if the branches of an if-else chain are in the wrong order?

Practice Questions

  1. What is the value of (5 > 3) && (2 == 2)?
  2. What is the value of !(4 < 1) || (3 == 3)?
  3. Write a condition that is true when x is between 10 and 20 inclusive.
  4. if (x > 0) { y = 1; } else { y = 2; } What is y when x is −5?
  5. Why is if (a = true) a mistake in Java?
Show answers
  1. true, because both parts are true.
  2. true, because !(false) is true, so the || is true regardless of the second part.
  3. x >= 10 && x <= 20
  4. 2, because the condition is false and the else branch runs.
  5. It uses assignment (=) instead of comparison (==); the condition should test a value, not assign one.
Keep reading to complete this lesson