Decisions begin with the if statement.
Every program that responds to input or data must choose between paths — and the if statement is where that choice begins.
An if statement lets a program run a block of code only when a boolean condition is true. If the condition is false, the block is skipped entirely.
This is the simplest form of selection in Java. It is the foundation for every later concept in this unit — if-else, nested conditionals, and even loop termination logic.
On the AP exam, single if statements appear in code tracing questions, in short FRQ logic checks, and as building blocks inside larger methods.
A boolean condition controls one optional block.
The condition is evaluated once; the body runs zero or one time.
Syntax
if (condition) {
// statements executed only if condition is true
}
The condition must be a boolean expression. It can use relational operators such as ==, !=, <, >, <=, >=, or logical operators like &&, ||, and !.
Example
int score = 85;
if (score >= 60) {
System.out.println("Pass");
}
Because 85 >= 60 is true, "Pass" is printed. If score were 40, nothing would print — there is no alternative branch.
Short-circuit evaluation
Java evaluates && and || left to right and stops as soon as the result is known. This matters when one side could throw an error.
if (s != null && s.length() > 0) {
// safe: the right side is only checked if s is not null
}
Terms students must recognize instantly.
Every AP question on selection uses this vocabulary.
- Boolean expression — any expression that evaluates to
trueorfalse. - Condition — the boolean expression placed inside the
ifparentheses. - Relational operator — compares two values:
==,!=,<,>,<=,>=. - Logical operator — combines booleans:
&&(and),||(or),!(not). - Block — the statements grouped inside
{ }that run when the condition is true. - Short-circuit evaluation — Java stops evaluating a logical expression once the result is determined.
How the if statement is tested.
Expect tracing, boolean logic, and careful boundary-value reasoning.
MCQ questions usually test whether you can trace execution when the condition uses compound logic or boundary values.
- Watch for
<vs<=on boundary values — a common trap. - Expect short-circuit questions where the right operand would cause an error if evaluated.
- Know that
Stringequality uses.equals(), not==.
FRQ questions reward clean conditions. If your logic uses if (x == true), simplify to if (x) — examiners value idiomatic Java.
Trace an if with compound conditions.
Reason through the boolean step by step, not by guessing.
Problem. What does the following code print when x = 5 and y = 10?
if (x > 0 && y / x > 1) {
System.out.println("A");
}
if (x == 0 || y / x > 2) {
System.out.println("B");
}
Step 1. Evaluate the first condition: x > 0 is true, so Java continues. y / x is 10 / 5 = 2, and 2 > 1 is true. The whole condition is true, so "A" prints.
Step 2. Evaluate the second condition: x == 0 is false. With ||, Java still checks the right side. y / x is 2, and 2 > 2 is false. The whole condition is false, so "B" does not print.
Answer. The program prints only A.
Why this matters. Notice how short-circuit evaluation protected the first if from a divide-by-zero risk had x been 0.
Errors that quietly cost marks.
Most if mistakes are syntax-correct but logically wrong.
- Using
=instead of==.=is assignment;==is comparison. In Java,if (x = 5)is a compile error forint, but forbooleanvariables it silently changes meaning. - Comparing Strings with
==. Uses1.equals(s2).==compares references, not contents. - Misplaced semicolon.
if (x > 0);ends the statement immediately, making the next block run unconditionally. - Forgetting braces. Without
{ }, only the next single statement is controlled by theif. - Boundary errors. Confusing
<with<=changes whether the endpoint is included.
Quick reference for selection logic.
The operators and patterns you must recognize on sight.
if Statement Reference
AP Quick Reference| Concept | Meaning | AP Exam Tip |
|---|---|---|
== vs = |
Comparison vs assignment | Inside an if, you almost always want ==. |
&& |
Logical AND, both sides must be true | Short-circuits when the left side is false. |
|| |
Logical OR, at least one side true | Short-circuits when the left side is true. |
! |
Logical NOT, flips the boolean | Apply De Morgan's laws to simplify negations. |
.equals() |
Compares String contents | Never use == for String comparison. |
| Boundary values | The exact value at the edge of a condition | Always test < vs <= at the boundary. |
How to lock in the if statement.
Move from recognition to fluency in three layers.
Use the three practice tools below this lesson in order:
- MCQ Practice — train your eye to trace boolean logic and spot boundary traps. Read every option before answering and watch for short-circuit clues.
- Java Lab — write small methods that take input and return a decision. Aim for one clean condition per problem rather than nested fixes.
- FRQ Practice — write decision logic in full method form. Justify why each condition uses
<or<=in plain language before you code.
If you can defend your boundary choices out loud, you're ready for the AP-style version.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.