If Statements – Practice & Nested
Practice with nested if blocks, combine conditions safely, and compare strings correctly.
If Statements – Practice & Nested is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Lesson Goals
Goals: Practice if/else, write nested checks, and compare strings safely.
- Nested if for multi-step decisions
- Combine conditions with && and ||
- Use equals for string comparison
Nested If
Nested if runs an inner check only when the outer condition is true.
public class Main {
public static void main(String[] args) {
int age = 16;
if (age >= 0) {
if (age < 18) {
System.out.println("Minor");
} else {
System.out.println("Adult");
}
}
}
}
Else-if vs Nested
else if chains mutually exclusive choices, while nested if is useful for multi-stage checks.
public class Main {
public static void main(String[] args) {
int score = 75;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("Other");
}
}
}
String Equality
Compare strings with equals or equalsIgnoreCase.
name.equals("Ada")"Ada".equals(name)avoids null pointer when name could be nullname.equalsIgnoreCase("ada")ignores case
Avoid == for strings; it compares references, not content.
Short-circuit
Short-circuit: && stops when the left side is false; || stops when the left side is true. Use this to guard checks:
public class Main {
public static void main(String[] args) {
String name = "Ada";
if (name != null && name.equals("Ada")) {
System.out.println("Welcome");
}
}
}
Nested If Demo
Try it: Enter a country code and age. See how nested checks choose the correct branch.
public class Main {
public static void main(String[] args) {
String country = "TR"; // manual input instead of Scanner
int age = 20; // manual input instead of Scanner
if (!country.isEmpty()) {
if (country.equalsIgnoreCase("TR")) {
if (age >= 18) {
System.out.println("Adult in TR");
} else {
System.out.println("Minor in TR");
}
} else {
if (age >= 18) {
System.out.println("Adult outside TR");
} else {
System.out.println("Minor outside TR");
}
}
} else {
System.out.println("No country provided");
}
}
}
String Compare Check
Quick check: Which are valid ways to check that name equals "Ada"?
Recap & Next
Recap: You practiced nested if, contrasted else-if vs nesting, and used equals safely with short-circuit guards.
Next: Learn more operators and patterns for complex conditions.
Frequently asked questions
Is the “If Statements – Practice & Nested” lesson free?
Yes — the full text of “If Statements – Practice & Nested” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “If Statements – Practice & Nested”?
Practice with nested if blocks, combine conditions safely, and compare strings correctly. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “If Statements – Practice & Nested” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- If Statements – Basics
- If Statements – Practice & Nested
- Common Pitfalls & Debugging Messages