From lesson 2-3, 10 % 3 in Java is 1.
% gives the remainder after integer division, and 10 divided by 3 is 3 with 1 left over.
That operator turns up constantly inside conditions. Testing whether a number is even is n % 2 == 0, and testing divisibility by anything else follows the same shape.
if and else
Branching is where programs earn their keep. Whether a password is correct, whether a cart qualifies for free shipping, whether an input is even a number: every rule a business has becomes an if somewhere.
The Java version is the same idea as Python in different clothes. The condition goes in parentheses, the body goes in braces, and there is no colon. Python's elif becomes else if.
if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else { System.out.println("C or below"); }
Comparisons are the usual >, >=, <, <=, ==, !=. Remember lesson 2-2: == is only for primitives, and for Strings you write name.equals("Ada").
The logic operators change names from Python:
| Python | Java |
|---|---|
and | && |
or | || |
not | ! |
Branches are tested in order and the first match wins, so score >= 80 is only reached when score >= 90 was already false. That is why the bands do not need an upper bound written out.
A grade classifier
One chain of branches, plus a compound condition stored in a boolean.
public class Main { public static void main(String[] args) { int score = 87; if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else { System.out.println("C or below"); } boolean passing = score >= 60 && score <= 100; System.out.println("passing: " + passing); } }
Output
B
passing: trueWith score at 87 the first test fails and the second succeeds, so exactly one line prints. Setting the score to 93 fires the first branch and 55 falls through to the else.
The passing line shows that a condition is just a value. score >= 60 && score <= 100 produces a boolean that can be stored, printed, or passed around, which is often clearer than burying the same expression inside an if.
Combining conditions
&& and || short-circuit exactly like Python's and/or: Java stops evaluating as soon as the answer is known. That lets you write guards safely:
if (name != null && name.equals("Ada")) { ... }
If name is null (Java's None), the second half never runs, so no crash. Flip a boolean with !: !open means "not open".
One style rule worth adopting now: always write the braces, even for one-line bodies. Every style guide at every major company requires it.
Order inside a && is part of the logic
When name is null, the line if (name.equals("Ada") && name != null) crashes.
&& evaluates left to right and short-circuits, meaning it stops as soon as the answer is settled. Here the crash is on the left side, because calling .equals on a null reference throws a NullPointerException before the null check is ever reached.
The guard has to come first:
if (name != null && name.equals("Ada")) { System.out.println("hello Ada"); }
Now name != null is false for a null reference, so && never evaluates the right side and nothing throws. Short-circuiting is what makes the pattern safe rather than merely tidy, and it is why operand order in a condition is logic rather than style.
The leap year rule
A year is a leap year when it is divisible by 4 and not by 100, or when it is divisible by 400.
public class Main { public static void main(String[] args) { int year = 2024; boolean leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; if (leap) { System.out.println(year + " is a leap year"); } else { System.out.println(year + " is not a leap year"); } } }
Output
2024 is a leap yearDivisibility is a % test, so divisible by 4 becomes year % 4 == 0. The two-part rule is grouped in parentheses and then joined to the 400 test with ||, because && binds tighter than || and the parentheses make the intent obvious to a reader either way.
Storing the answer in a boolean named leap keeps the if short enough to read at a glance. The rule genuinely works out this way: 1900 was not a leap year and 2000 was, which is exactly what the % 400 clause exists to handle.