6m left·0%
Reading Time: 6 min
Last Updated: February 23, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: February 23, 2026
Main Ideas: 5

Topic 2.3 Notes – if Statements

Verified for 2027 AP® Computer Science A Exam
Read aloud
If statements let your program make decisions. Instead of running every line in order, Java can check a condition and choose which code to execute. This is how programs respond to data, user input, and changing values during execution.

1. How If Statements Control Program Flow

By default, Java runs code sequentially from top to bottom. An if statement interrupts that flow.

It checks a Boolean expression that evaluates to true or false. Based on that result, Java either executes a block of code or skips it.

Here’s the idea visually:

Study guide illustration

Flowchart of an if-else selection statement

What’s happening under the hood:

  1. Evaluate the Boolean condition.
  2. If it’s true, run the associated block.
  3. If it’s false, skip that block (or run the else block, if there is one).
  4. Continue with the next statement after the block.

This is called a selection statement because the program selects which path to follow.

2. Types of Selection Statements

There are two forms you need to know for AP CSA.

a. One-way selection

Used when something should happen only if a condition is true.

int temperature = 85;

if (temperature > 80) {
    System.out.println("It's hot outside.");
}
System.out.println("Check complete.");

If temperature > 80 is:

  • true → the message prints
  • false → nothing happens inside the block

Important: the program always continues after the block.

This shows up a lot in tracing problems where a variable only changes under certain conditions. If the condition is false, that change never happens.

b. Two-way selection

Used when you want one action if true and a different action if false.

int score = 72;

if (score >= 70) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}

Exactly one branch runs.

Here’s the structure visually:

Study guide illustration

If-else flowchart structure

The program reaches a condition, follows either the True path or the False path, and then both paths rejoin before continuing.

Key distinction:

  • One-way → maybe something runs
  • Two-way → something always runs

On quizzes, students often assume both blocks might run. That never happens in an if-else.

3. How Conditions Are Evaluated

The condition must be a Boolean expression.

Common forms:

  • Numeric comparisons: >, <, >=, <=, ==, !=
  • Boolean variables: if (isOpen)
  • String comparison:
    if (name.equals("Alex"))
    
    Never use == for Strings.
  • Compound conditions:
    • && (and)
    • || (or)
    • ! (not)

Nested if statements are allowed:

if (score >= 60) {
    if (score >= 90) {
        System.out.println("Excellent");
    }
}

The inner if only runs if the outer condition is true. When tracing, evaluate one condition at a time. Don’t skip steps.

4. Common Mistakes That Lose Points

Missing curly braces

Without braces, only the next line belongs to the if.

if (score > 90)
    System.out.println("A");
    System.out.println("Great job"); // always runs

On FRQs, always use braces. It avoids logic errors and graders like clarity.

Using = instead of ==

if (x = 5)   // compile error
  • = assigns
  • == compares

This is a frequent multiple-choice trap.

Null issues with objects

Calling a method on null causes a crash.

Safer pattern:

if (name != null && name.equals("Mia")) {
    System.out.println("Hello Mia");
}

Because of short-circuit evaluation, name.equals(...) only runs if name != null is true.

Separate if statements vs if-else

These are different:

if (score >= 80)
    System.out.println("B or higher");

if (score >= 90)
    System.out.println("A");

If score is 95, both lines print.

That’s correct behavior, but students often expect only one to run. Use if-else if you want mutual exclusivity.

5. How This Shows Up on Tests

In multiple-choice, you’ll trace nested or compound conditions and determine:

  • What prints
  • Final variable values
  • Which blocks execute

Slow down and evaluate each condition in order. Most wrong answers come from assuming a block runs without checking its condition carefully.

On FRQs, you’ll write methods that:

  • Validate input
  • Return different values based on conditions
  • Update variables depending on criteria

Clear logic and correct Boolean expressions matter more than fancy structure.

Key Takeaways

An if statement changes normal top-to-bottom execution by making a decision based on a Boolean expression.
A one-way if might execute a block, but an if-else always executes exactly one branch.
Conditions must evaluate to true or false; use .equals() for Strings, not ==.
Without braces, only one statement belongs to the if, even if indentation suggests otherwise.
In nested conditionals, the inner if only runs if the outer condition is true.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining