Aral Code

Control Flow Lesson 10 of 21

Java if, else if and else

Make decisions in Java with if, else if and else, nest conditions, combine them with && and ||, and use the ternary operator. Includes a DepEd grade descriptor program.

Decisions turn a list of instructions into a smart program. "If the grade is 75 or above, print PASSED; otherwise print FAILED." In Java, decisions are written with if, else if and else.

if

public class Main {
    public static void main(String[] args) {
        int grade = 88;
        if (grade >= 75) {
            System.out.println("PASSED");
        }
        System.out.println("Done.");
    }
}

Output

PASSED
Done.

The condition goes in parentheses, and the code to run goes inside curly braces.

if-else

public class Main {
    public static void main(String[] args) {
        int grade = 72;
        if (grade >= 75) {
            System.out.println("PASSED");
        } else {
            System.out.println("FAILED - see your adviser for remediation.");
        }
    }
}

Output

FAILED - see your adviser for remediation.

else if: many choices

public class Main {
    public static void main(String[] args) {
        int grade = 87;
        String descriptor;

        if (grade >= 90) {
            descriptor = "Outstanding";
        } else if (grade >= 85) {
            descriptor = "Very Satisfactory";
        } else if (grade >= 80) {
            descriptor = "Satisfactory";
        } else if (grade >= 75) {
            descriptor = "Fairly Satisfactory";
        } else {
            descriptor = "Did Not Meet Expectations";
        }

        System.out.println(grade + " - " + descriptor);
    }
}

Output

87 - Very Satisfactory

Java checks from top to bottom and runs only the first true block. That's why the highest range comes first.

Combining conditions

public class Main {
    public static void main(String[] args) {
        int age = 19;
        boolean hasVoterId = true;
        double average = 91.2;
        int lowestGrade = 83;

        if (age >= 18 && hasVoterId) {
            System.out.println("You can vote.");
        }

        if (average >= 90 && lowestGrade >= 85) {
            System.out.println("With honors");
        } else if (average >= 90) {
            System.out.println("Average qualifies, but a grade is below 85");
        }
    }
}

Output

You can vote.
Average qualifies, but a grade is below 85

Comparing Strings in conditions

public class Main {
    public static void main(String[] args) {
        String answer = "Yes";
        if (answer.equalsIgnoreCase("yes")) {
            System.out.println("Proceeding...");
        } else {
            System.out.println("Cancelled.");
        }
    }
}

Output

Proceeding...

Nested if

public class Main {
    public static void main(String[] args) {
        double amount = 1500;
        boolean isMember = true;

        if (amount >= 1000) {
            if (isMember) {
                System.out.println("15% member discount");
            } else {
                System.out.println("5% discount");
            }
        } else {
            System.out.println("No discount");
        }
    }
}

Output

15% member discount

Braces and one-line ifs

Java allows an if without braces when there's only one statement, but this is risky:

if (grade >= 75)
    System.out.println("PASSED");
    System.out.println("Congratulations!");   // ALWAYS runs! Not part of the if.

Indentation doesn't matter to Java; only the braces do. Always use braces.

The ternary operator

public class Main {
    public static void main(String[] args) {
        int items = 1;
        String label = items == 1 ? "item" : "items";
        System.out.println(items + " " + label);
    }
}

Output

1 item

Even or odd, positive or negative

public class Main {
    public static void main(String[] args) {
        int n = -14;
        String parity = (n % 2 == 0) ? "even" : "odd";
        String sign;
        if (n > 0) sign = "positive";
        else if (n < 0) sign = "negative";
        else sign = "zero";
        System.out.println(n + " is " + parity + " and " + sign + ".");
    }
}

Output

-14 is even and negative.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. What's wrong with if (x > 5); { System.out.println("big"); }?
2. In an if/else if chain, which block runs when several conditions are true?
3. How do you check if a String s equals "admin"?
4. Why always use braces with if?

By Sir Reyel · Last updated 2026-12-04