allthingsare๐Ÿ…ฟ๏ธ.com Books โฌ…๏ธ Back allthingsare

โ€œDonโ€™t watch the clock; do what it does. Keep going.โ€
[Sam Levenson (1911โ€“1980) โ€“ American humorist and writer]


if-else Statements


4.1.1 Basic if Statement

The if statement is the simplest and most commonly used conditional statement in Java โ€” and for good reason.
It gives your program the ability to make a decision based on whether a condition is true or false, and then execute a block of code only if that condition is true.

In practical web development, simple if statements are everywhere.
For example, you might check if a userโ€™s age meets the minimum requirement to sign up for a service, if a form input is empty, or if an HTTP request contains a valid parameter.

Because the if statement evaluates a boolean expression, it works perfectly with comparison operators and logical operators.
This means you can build more complex checks like if (age >= 18 && isVerified) to combine multiple rules.

Another important point is scope: only the code inside the {} braces will run if the condition is true.
If there are no braces, only the next single statement runs.
Itโ€™s a good habit to always use braces, even for single statements, to avoid bugs when adding lines later.

Mastering the if statement is one of the first steps to writing clear, readable, and safe logic.
Even the most complex web frameworks depend on this simple structure under the hood.

Mini Lab

java
public class BasicIf { public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println("You are an adult."); } System.out.println("Check complete."); } }

4.1.2 if-else Structure

The if-else structure extends the simple if by adding an else block, letting your program do one thing if the condition is true and another thing if itโ€™s false.

This is extremely useful for logic that needs a clear either/or decision.
For example, you might check if a user is logged in. If they are, show them a personalized dashboard; if not, show them a login prompt.
Or you might check if payment has been completed and either confirm the order or ask the user to try again.

The else block is always optional โ€” but using it avoids leaving the false case unhandled, which is important for writing defensive code.
When you know exactly what should happen for both outcomes, your program is less likely to fail in unexpected ways.

In real-world projects, many developers format if-else carefully for readability: use clear indentation, write the condition cleanly, and keep the true/false blocks balanced.
If each block gets too large, consider moving the logic into helper methods.

The if-else is the backbone of many user flows in a web app โ€” authentication, authorization, feature toggles, or error handling all rely on it heavily.

Mini Lab

java
public class IfElse { public static void main(String[] args) { boolean isMember = false; if (isMember) { System.out.println("Welcome back, member!"); } else { System.out.println("Please sign up to become a member."); } } }

4.1.3 Multiple Conditions with else-if

Sometimes you need to check not just two but several possible conditions.
In this case, chaining else-if blocks gives you a clear, readable structure for testing multiple possibilities in order.

The program checks each if or else-if in sequence, top to bottom.
As soon as one condition matches, its block runs and the rest are skipped.
This makes else-if perfect for handling categories or ranges that donโ€™t overlap.

A classic real-world example is grading systems: 90 and above might be A, 80โ€“89 B, 70โ€“79 C, and so on.
Itโ€™s also useful for multi-step validation, user role checks (admin, editor, viewer), or mapping API response codes.

When the logic has many branches, keep each condition clear.
Donโ€™t repeat overlapping conditions, and always include a final else to catch anything that doesnโ€™t match earlier rules โ€” this acts as a safe fallback.

If you find yourself writing dozens of else-if blocks, thatโ€™s a sign to switch to switch-case or consider using more advanced structures like enums or strategy patterns.

Mini Lab

java
public class ElseIfChain { public static void main(String[] args) { int score = 72; if (score >= 90) { System.out.println("Excellent"); } else if (score >= 75) { System.out.println("Good"); } else if (score >= 60) { System.out.println("Pass"); } else { System.out.println("Fail"); } } }