01 / 20

Java if Statements

Learn to make your programs decide: execute code only when a Boolean condition is true, branch with else, and chain logic with else if.

AP Computer Science A

What Is an if Statement?

An if statement is the most fundamental control structure in Java. It lets your program make decisions by testing a Boolean condition and executing a block of code only when that condition evaluates to true.

Boolean Conditions: The Gateway

Every if statement relies on a Boolean expression: an expression that evaluates to either true or false. Java provides several comparison operators to build these expressions:

\(==\)
Equal to
\(!=\)
Not equal to
\(\gt\)   \(\lt\)
Greater / Less than
\(\ge\)   \(\le\)
Greater/Less or equal

Why if Statements Matter

Without conditional logic, programs can only execute instructions sequentially: line after line, with no ability to adapt. The if statement unlocks three critical capabilities:

1
Input validation: Check whether user data is acceptable before processing it.
2
Branching logic: Choose different actions based on the current state of variables.
3
Error handling: Detect and respond to unexpected situations before they crash the program.

The Syntax of an if Statement

if (condition) { // code to execute when condition is true }
1
Keyword if: tells Java a conditional block follows.
2
Parentheses ( ): contain the Boolean expression to test.
3
Braces { }: wrap the body. If the condition is true, every statement inside runs. If false, the entire block is skipped.

How an if Flows

Start condition true / false? true Execute body false Skip body Continue

Adding an else Branch

When you need to do one thing if a condition is true and a different thing if it is false, add an else clause:

if (temperature > 100) { System.out.println("Boiling!"); } else { System.out.println("Not boiling."); }

Chaining with else if

When there are more than two possibilities, chain multiple conditions using else if. Java evaluates them top to bottom and executes the first block whose condition is true:

if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else { grade = "F"; }

if vs if-else vs else if

if alone
Tests one condition. Body runs or is skipped: 0 or 1 blocks execute.
if-else
Two-way branch. Exactly 1 of 2 blocks always executes.
else if chain
Multi-way branch. Exactly 1 of N blocks executes (if a final else is present).

How to Apply if Statements

1
Identify the decision: What question does the program need to answer?
2
Write the Boolean expression: Translate the question into a comparison using \(==\), \(!=\), \(\lt\), \(\gt\), \(\le\), or \(\ge\).
3
Determine the structure: Do you need just if, if-else, or an else if chain?
4
Write the body: Place the statements to execute inside curly braces { }.
5
Trace with test values: Mentally run through with a true case and a false case to verify correctness.

Example 1: Simple if

int age = 17; if (age >= 16) { System.out.println("You can drive."); } System.out.println("Program continues.");

Example 2: if-else in Action

int num = 7; if (num % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); }

Example 3: else if Chain

int temp = 35; if (temp >= 90) { System.out.println("Hot"); } else if (temp >= 60) { System.out.println("Warm"); } else if (temp >= 32) { System.out.println("Cold"); } else { System.out.println("Freezing"); }

Common Mistakes

✕ Using \(=\) instead of \(==\)
Writing if (x = 5) assigns 5 to x rather than comparing. Java will flag a type error because the result is an int, not a boolean.
✕ Forgetting braces
Without { }, only the very next statement is controlled by the if. The second line always runs, causing logic errors.
✕ Semicolon after if
Writing if (x > 0); creates an empty body. The block below executes unconditionally regardless of the condition.

Summary Comparison

if if-else else if chain
Branches 1 2 N
Blocks executed 0 or 1 Exactly 1 Exactly 1 (with else)
Mutual exclusion No Yes Yes
Requires else? No Yes Optional (recommended)
Use when One optional action Two alternatives Multiple categories

Question 1 of 5

What is printed by the following code?

int x = 10; if (x > 5) { System.out.print("A"); } if (x > 8) { System.out.print("B"); }

Question 2 of 5

What is printed by the following code?

int x = 10; if (x > 5) { System.out.print("A"); } else if (x > 8) { System.out.print("B"); }

Question 3 of 5

Which of the following will cause a compile-time error in Java?

Question 4 of 5

What does this code print?

int n = 3; if (n > 0) System.out.print("Pos"); System.out.print("Done");

Question 5 of 5

What is the value of grade after this code runs with \(score = 85\)?

String grade; if (score >= 70) { grade = "C"; } else if (score >= 80) { grade = "B"; } else if (score >= 90) { grade = "A"; } else { grade = "F"; }