AP Computer Science A · Unit 2 lecture · Part 2 of 12 · 8 min

2.3 if statementsChoosing what runs

Mr. Thompkins
An if statement is a fork in the road. Java looks at the test, takes one path, and keeps going. Your job when tracing is simple: work out the test, then follow exactly one path.

A one-way if runs its block only when the test is true. If the test is false, Java skips the block and goes on to the next statement. A two-way if-else has two blocks: the if block runs when the test is true, and the else block runs when it's false. Exactly one of the two runs, never both and never neither.

The block is the code between the braces { }. Java doesn't care about indentation, only about braces. If you leave the braces off, only the very next statement belongs to the if. The AP exam writes braces almost everywhere, and you should too.

age < 13 ?truefalseprice = 8.0;price = price - 2.0;if (age >= 65) ...exactly one branch runs, then the program goes on
An if-else as a flowchart. One test, two paths, and they meet again at the next statement.
1int age = 15;2double price = 12.0;3if (age < 13)14{5    price = 8.0;6}7else8{9    price = price - 2.0;210}11if (age >= 65)312{13    price = price / 2;14}15System.out.println(price);
  1. line 3 false for 15, so the else block runs
  2. line 9 price becomes 10.0
  3. line 11 A separate if: checked too, and false
output › 10.0
Two separate if statements, run under Java 17. The second one is checked no matter what the first one did.
?Wait, what does price = price - 2.0 do?
It's an assignment: Java evaluates the right side with the current value of price (12.0 - 2.0 = 10.0), then stores 10.0 back in price. You could also write price -= 2.0;, the compound assignment from Unit 1.
?Wait, what's a block again?
A block is a group of statements inside braces { }. It acts like one statement: an if runs its whole block or skips its whole block. Variables declared inside a block exist only until the closing brace.
Pause and think
int x = 8; int y = 0;
if (x > 10)
    x = 0;
    y = 1;

What are x and y afterward? (The indentation is misleading on purpose.)

I thought about it. Show me
x is 8 and y is 1. Without braces, only x = 0; belongs to the if, and it's skipped because 8 > 10 is false. y = 1; is just the next statement, so it always runs, whatever the indentation suggests.
Trace two separate ifs

What is printed?

int t = 12;
if (t % 2 == 0)
{
    t = t / 2;
}
if (t > 5)
{
    t -= 4;
}
else
{
    t += 10;
}
System.out.println(t);
  1. First if: 12 % 2 is 0, so the test is true and t becomes 6.
  2. Second if: it's a separate statement, so Java checks it too, using the new value of t. 6 > 5 is true.
    Why?
    The trap answer uses the old value, 12, or stops after the first if. Each if uses whatever the variables hold at that moment.
  3. The if block runs: t -= 4 makes t 2. The else block is skipped.

Answer: 2

Stuck? Get a hint
  1. Do the first if completely before you look at the second.
  2. After the first if, t is 6. Is 6 > 5?
  3. Only one of the second statement's two blocks runs.
Check yourself
What is printed?
int k = 10;
if (k >= 10)
{
    k += 5;
}
if (k < 10)
{
    k -= 5;
}
System.out.println(k);
  1. A15
  2. B5
  3. C20
  4. D10
Show the answer

A. k starts at 10. 10 >= 10 is true, so k += 5 makes it 15. The second if is a separate statement, and it uses the current value: 15 < 10 is false, so k stays 15. Choice D is the top trap: it runs both blocks (10 + 5 - 5), but by the time the second test is checked, k is already 15.

Exam tip from Mr. T

On every if question, first ask: is this one statement with else branches, or several separate ifs? Separate ifs are all checked, each with the current values. Draw a line between them on your scratch paper.