If and else decisions
What you will learn
An if statement chooses an action according to a condition. A ticket machine can accept a request when seats are available and display an alternative message otherwise. Java evaluates a boolean expression and follows the branch that matches its value.
public class Main { public static void main(String[] args) { int seats = 3; if (seats > 0) { System.out.println("Available"); } else { System.out.println("Sold out"); } }}Expected output:
AvailableThree is greater than zero, so only the first branch runs.
How it works
Write if (condition) { statements } and optionally else { statements }. The condition must be boolean or Boolean; a Boolean is unboxed and null throws NullPointerException. Java does not treat nonzero integers as true. An else-if chain tests conditions in order and selects the first true branch. A final else handles remaining values. Separate if statements are independent. Braces group statements and delimit the scope of local variables. Without braces an else attaches to the nearest eligible unmatched if; use braces to make the intended grouping visible.
public class Main { public static void main(String[] args) { int grams = 1000; if (grams <= 500) { System.out.println("Small"); } else if (grams <= 1000) { System.out.println("Medium"); } else { System.out.println("Large"); } }}Expected output:
MediumThe first test fails; the inclusive second test succeeds at exactly 1000.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { int score = 95; if (score >= 50) { System.out.println("Pass"); } if (score >= 90) { System.out.println("Distinction"); } if (score >= 90) { System.out.println("Gold"); } else if (score >= 50) { System.out.println("Silver"); } }}Expected output:
PassDistinctionGoldBoth achievements apply, but the medal categories are exclusive.
Common mistakes
Testing score >= 50 before score >= 90 in the same chain hides the more specific category. An accidental semicolon immediately after if creates an empty controlled statement. A local variable declared inside a branch is unavailable outside its block.
Try it yourself
Classify an integer battery percentage 0–100: below 20 prints Low, 20–79 prints Normal, 80 or more prints High. Test 80, 19, 20 and 79. Print one label per input.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
If and else decisions — knowledge check
Apply if and else decisions to examples and boundary conditions.
Which condition accepts exactly zero items?
Two independent if conditions are true. Assuming normal completion, which bodies run?
Which test comes first for exclusive awards at scores 90 and 50?
Why is if (5) invalid?
Where can a local variable declared inside an if block be used?