Conditions and if/else
1.3.1 — Conditions and if/else
An if statement runs code only when a condition is true. This lesson starts with one rule: a person must be at least 16 years old.
int age = 16; creates a whole-number variable. System.out.println(...) prints a value. Each code cell supplies its own inputs; no earlier notebook is needed.
Compare two numbers
A comparison produces true or false, a value of type boolean.
| Operator | Meaning | Example |
|---|---|---|
| < | Less than | 15 < 16 is true |
| <= | Less than or equal to | 16 <= 16 is true |
| > | Greater than | 16 > 16 is false |
| >= | Greater than or equal to | 16 >= 16 is true |
| == | Equal to | 16 == 16 is true |
| != | Not equal to | 15 != 16 is true |
Run the next cell. >= includes the boundary value 16.
Choose between two branches
Put the condition inside parentheses after if. Braces { ... } group the statements in each branch.
When the condition is true, Java runs the if block. Otherwise it runs the else block. Only one of these two blocks runs.
Run the next cell, then change joinAge to 15 and rerun it.
Assignment and comparison
= stores a value. == compares two values.
int count = 3; // store 3
count == 3 // compare: trueAn if condition must be boolean. if (count = 3) fails to compile because that assignment produces an int, not a boolean. Use if (count == 3) to test equality.
Combine conditions
&&means and: both conditions must be true.||means or: at least one condition must be true.!means not: it reverses a boolean value.
The next rule requires both an age of at least 16 and an available place. places > 0 means there is space. Run the cell.
Stop checking when the answer is known
&& checks its right side only if the left side is true. || checks its right side only if the left side is false. This is short-circuiting.
In the next cell, divisor != 0 is false. Java skips 12 / divisor > 2, avoiding division by zero. The order matters: check the divisor before dividing.
Check rules in order
An else if adds another condition. Java checks from top to bottom and runs the first matching branch. The final else covers the remaining cases.
This rule checks age first, then available places. If both requirements fail, it prints Too young.
Repair two overlapping checks
Requirement: print exactly one message. Check age first, then available places.
The next cell uses two separate if statements, so an age of 15 and zero places prints both Too young and Full. Change only the second if to else if, then run it again. It should print only Too young.
| repairAge | repairPlaces | Correct message |
|---|---|---|
| 15 | 0 | Too young |
| 15 | 1 | Too young |
| 16 | 0 | Full |
| 16 | 1 | Can join |
After your repair, change just these two input values to check the rows. An else-if chain runs only the first matching branch.
Practice: check the minimum age
In the next cell, replace false with a comparison that is true when practiceAge is at least 16. Do not change the messages.
Run it with 16: it should print Can join.
Hint
Use practiceAge >= 16 inside the parentheses after if.
Worked solution
int practiceAge = 16;
if (practiceAge >= 16) {
System.out.println("Can join");
} else {
System.out.println("Too young");
}Check the boundaries
In your completed practice cell, change only practiceAge and run each case:
| practiceAge | Expected output |
|---|---|
| 15 | Too young |
| 16 | Can join |
| 17 | Can join |
For the earlier two-input example, change checkedAge and availablePlaces to match each row:
| checkedAge | availablePlaces | Expected output |
|---|---|---|
| 15 | 0 | Too young |
| 16 | 0 | Full |
| 16 | 1 | Can join |
| 17 | 1 | Can join |
Enable JavaScript to run the Java notebook.