Week 17 · 17.2 · Tue, Dec 1 · We Do + Try Its
If It Is True: The if Statement in Java
This section shows the four kinds of steps on today's bench: following which lines run, reading the condition, tracing a box that changes inside an if, and spotting two mistakes the compiler cannot catch.
The if Line Decides
An if statement has a condition in parentheses and a block in braces. The if line always runs, because it is where the program decides. When the condition is true, the block runs. When it is false, the program skips the block and goes on after the closing brace.
How to follow an if:
- Run the lines above it, and note what each box holds.
- Work out the condition with those values: true or false.
- If it is true, run the block. If it is false, jump past the closing brace.
Example 1. Follow the program when score is 82.
1int score = 82; 2if (score >= 70) 3{ 4 System.out.println("pass"); 5} 6System.out.println("done");
Show the solution
pass done
82 >= 70 is true, so the block runs. Lines 1, 2, 4 and 6 run, and the program prints pass and then done.
Example 2. The same program when score is 55. Which lines run?
1int score = 55; 2if (score >= 70) 3{ 4 System.out.println("pass"); 5} 6System.out.println("done");
Show the solution
lines that run: 1, 2, 6
55 >= 70 is false, so the program jumps past the block. Line 2 still runs, because that is where the program decides. Only done is printed.
Work each one in your notebook first, then check it.
Try It 1. int k = 7; then if (k > 5) prints yes inside its block, and the last line prints end. What is printed?
Try It 2. The same program with int k = 3; What is printed?
Reading the Condition
A condition can use any of the comparisons from yesterday, such as >= or ==. It can also be a boolean box, which already holds true or false.
Example 3. Is the condition true when age is exactly 13?
1int age = 13; 2if (age >= 13) 3{ 4 System.out.println("teen ticket"); 5}
Show the solution
teen ticket
>= means greater than or equal to, so 13 >= 13 is true, and the block runs.
Example 4. Use a boolean box as the condition.
1boolean raining = true; 2if (raining) 3{ 4 System.out.println("umbrella"); 5} 6System.out.println("go");
Show the solution
umbrella go
raining holds true, so the condition is true. The line after the block runs either way.
Work each one in your notebook first, then check it.
Try It 3. int h = 18; then if (h >= 18) prints vote inside its block. Does vote print?
Try It 4. boolean open = false; then if (open) prints enter inside its block, and the last line prints wait. What is printed?
A Box Inside an if
A block can change a box. A trace table gets a row only for the lines that store a value or print. The if line decides and gets no row, and a skipped line gets no row either.
Example 5. Trace lives and the output.
1int lives = 3; 2if (lives < 5) 3{ 4 lives = lives + 1; 5} 6System.out.println(lives);
Show the solution
line lives Output 1 3 4 4 6 4 4
3 < 5 is true, so line 4 runs and lives becomes 4. Line 6 prints 4.
Example 6. Two ifs, one after the other. Each one decides on its own.
1int n = 4; 2if (n > 2) 3{ 4 System.out.println("more than 2"); 5} 6if (n > 5) 7{ 8 System.out.println("more than 5"); 9}
Show the solution
more than 2
4 > 2 is true, so the first block runs. 4 > 5 is false, so the second block is skipped.
Work each one in your notebook first, then check it.
Try It 5. int s = 10; then if (s > 5) holds s = s - 3; in its block, and the last line prints s. What is printed?
Try It 6. The same program with int s = 2; What is printed?
Two Mistakes the Compiler Cannot Catch
Both of these programs compile and run, but they give the wrong results.
Example 7. A semicolon right after the parentheses. x is 5. What prints?
1int x = 5; 2if (x > 100); 3{ 4 System.out.println("big"); 5}
Show the solution
big
The semicolon ends the if statement on line 2, so the braces below it are no longer its block. They run every time, and big is printed even though 5 > 100 is false.
Example 8. Braces left out. The writer meant both subtractions for a child only. age is 30.
1int age = 30; 2int p = 10; 3if (age < 12) 4 p = p - 4; 5 p = p - 2; 6System.out.println(p);
Show the solution
8
Without braces, only line 4 belongs to the if. Line 5 runs for everyone, even though it is indented, so an adult pays 8 instead of 10. Braces around both lines fix it.
Sources: Oracle, The Java Tutorials: The if-then and if-then-else Statements; course repository, Bench Basics B24 job data (2026), run with the course JDK.
Work each one in your notebook first, then check it.
Try It 7. int y = 1; then the line if (y > 9); ends with a semicolon, and the braces below it print nine. What is printed?
Try It 8. int q = 5; then if (q > 10) with no braces, followed by the indented lines q = 0; and q = q + 1; and then a line prints q. What is printed?