Guard clauses and boundaries
What you will learn
A guard clause checks a condition that makes normal processing inappropriate and exits early. It keeps invalid data away from the calculation. Boundary tests use values immediately below, at and above a threshold to verify the rule.
public class Main { public static void main(String[] args) { int quantity = -2; if (quantity <= 0) { System.out.println("Invalid quantity"); return; } System.out.println(quantity * 4); }}Expected output:
Invalid quantityThe guard exits main before multiplication.
How it works
Specify the valid range and whether endpoints are included. Reject invalid values first. In the conventional void main method, return; ends that method without a result; in these standalone examples it ends the program. Other methods may return a value or throw an exception, covered later. A guard may protect division or a method call. Keep validation separate from the business decision. Test the smallest valid value, the largest valid value and invalid neighbors. For ranges, use && for both bounds, not a chained comparison like 0 <= n <= 100.
public class Main { public static void main(String[] args) { int orderValue = 50; if (orderValue < 0 || orderValue > 10000) { System.out.println("Invalid amount"); return; } int fee = 5; if (orderValue >= 50) { fee = 0; } System.out.println(fee); }}Expected output:
0The allowed whole-credit amount is 0–10000. Exactly 50 qualifies for free shipping.
Check a boundary or unusual input
public class Main { public static void main(String[] args) { String label = null; if (label == null || label.length() == 0) { System.out.println("Missing label"); return; } System.out.println(label); }}Expected output:
Missing labelThe left side of || is true. Java skips length(), avoiding a call on null. length() counts UTF-16 code units; here only emptiness matters.
Common mistakes
Printing an error without stopping can still allow an invalid calculation. Use return here or an exclusive else. For inclusive limits 1–10, rejection is n < 1 || n > 10, not n <= 1 || n >= 10.
Try it yourself
A workshop accepts 1–12 seats inclusive. Print Invalid for other integers and stop; otherwise print the booking cost at 7 credits per seat. Test 12, 0, 1 and 13 as independent runs.
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
Guard clauses and boundaries — knowledge check
Apply guard clauses and boundaries to examples and boundary conditions.
Which guard rejects integers outside inclusive 1–12?
What does return; do inside void main?
Which inputs best test the lower boundary of a valid range starting at 5?
Why does label == null || label.length() == 0 avoid a null call?
An invalid quantity prints an error, then code still divides by it. What is missing?