Java Tutorial
Java If Else
if runs a block when a condition is true. else and else if cover the other paths.
if and else
if tests a boolean. When it is true, the block runs. else runs when it is not.
Example
public class Main {
public static void main(String[] args) {
int n = 7;
if (n % 2 == 0) System.out.println("even");
else System.out.println("odd");
}
}else if
Chain extra tests with else if. The first true branch wins. Put the default last.
Example
public class Main {
public static void main(String[] args) {
int score = 82;
if (score >= 90) System.out.println("A");
else if (score >= 80) System.out.println("B");
else System.out.println("C");
}
}Braces keep a block together
A one-line body can skip the braces, but a block with braces is safer. The moment a branch needs a second statement, the braces are what bind the two lines to the if — without them, only the first line belongs to the condition and the second always runs.
Example
public class Main {
public static void main(String[] args) {
int temp = 32;
if (temp <= 0) {
System.out.println("Freezing");
System.out.println("Wear a coat");
} else {
System.out.println("Above freezing");
}
}
}Combine conditions
Join tests with && (both must be true) and || (either is enough). This is how you ask real questions like "old enough and a member".
Example
public class Main {
public static void main(String[] args) {
int age = 20;
boolean member = true;
if (age >= 18 && member) {
System.out.println("Welcome in");
} else {
System.out.println("Not yet");
}
}
}Use == to compare, not =. A single = is assignment; writingif (x = 5) is a compile error in Java (it is not a boolean) — the language catches this classic slip for you.
Try It Yourself
Exercise: Read an int hour (0–23) and print "Good morning" before 12, "Good afternoon" before 18, otherwise "Good evening".
Show solution
if (hour < 12) System.out.println("Good morning");
else if (hour < 18) System.out.println("Good afternoon");
else System.out.println("Good evening");Order matters: because the first true branch wins, checking hour < 12 first means the later tests only see the remaining hours.
Key Takeaways
ifruns a block when a boolean is true;elsecovers the rest.- Chain with
else if; the first true branch wins, so order your tests carefully. - Combine conditions with
&&and||. - Compare with
==, never a single=.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Maths
Pass mark
A threshold is an if. Scores at or above 70 pass. The else covers the rest without a second formula.
Example
public class Main {
public static void main(String[] args) {
int score = 71;
if (score >= 70) System.out.println("pass");
else System.out.println("retry");
}
}