Skip to content
Laviyx Learn

Try

Java course menu

Java if, else if and else: Making Decisions

Learn Java conditions with if, else if and else: writing boolean conditions, combining them with && and ||, nested if statements, comparing strings correctly with equals, the ternary operator, and common mistakes.

Beginner10 min readUpdated
On this page

The if statement lets a program choose what to do: apply a discount if the order is large, show an error if the password is wrong. The condition must be a boolean expression.

if#

Java
public class Main {
    public static void main(String[] args) {
        int temperature = 34;

        if (temperature > 30) {
            System.out.println("It's a hot day.");
        }
        System.out.println("Have a nice day!");
    }
}
Output
It's a hot day.
Have a nice day!

The condition goes in parentheses and the code to run in a block. If the condition is false, the block is skipped.

if ... else#

Java
public class Main {
    public static void main(String[] args) {
        int marks = 35;

        if (marks >= 40) {
            System.out.println("Pass");
        } else {
            System.out.println("Fail");
        }
    }
}
Output
Fail

else if: several choices#

Conditions are checked top to bottom, and only the first true branch runs:

Java
public class Main {
    public static void main(String[] args) {
        int marks = 82;

        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 75) {
            System.out.println("Grade B");
        } else if (marks >= 60) {
            System.out.println("Grade C");
        } else {
            System.out.println("Grade F");
        }
    }
}
Output
Grade B

Because of the order, the marks >= 75 branch already knows the marks are below 90.

Combining conditions#

Use && (and), || (or) and ! (not):

Java
public class Main {
    public static void main(String[] args) {
        int age = 16;
        boolean withParent = true;

        if (age >= 18 || withParent) {
            System.out.println("Entry allowed");
        }
        if (age >= 13 && age <= 19) {
            System.out.println("Teenager");
        }
        if (!(age >= 18)) {
            System.out.println("Minor");
        }
    }
}
Output
Entry allowed
Teenager
Minor

Nested if statements#

Java
public class Main {
    public static void main(String[] args) {
        boolean loggedIn = true;
        boolean isAdmin = false;

        if (loggedIn) {
            if (isAdmin) {
                System.out.println("Admin dashboard");
            } else {
                System.out.println("User dashboard");
            }
        } else {
            System.out.println("Please log in");
        }
    }
}
Output
User dashboard

Deep nesting is hard to read. Often you can flatten it with &&, or handle simple cases first.

Comparing strings in conditions#

This is the most common beginner bug in Java. == on strings checks whether two variables refer to the same object, not whether they contain the same text. Use equals (or equalsIgnoreCase):

Java
public class Main {
    public static void main(String[] args) {
        String command = new String("start");

        if (command == "start") {
            System.out.println("== says equal");
        } else {
            System.out.println("== says different");
        }

        if (command.equals("start")) {
            System.out.println("equals says equal");
        }
        if ("START".equalsIgnoreCase(command)) {
            System.out.println("equalsIgnoreCase says equal");
        }
    }
}
Output
== says different
equals says equal
equalsIgnoreCase says equal

Writing the literal first, as in "START".equalsIgnoreCase(command), also avoids a NullPointerException when command is null.

Always use braces#

Braces are optional when a branch has one statement, but leaving them out invites bugs: a second indented line added later will not be part of the if. Use braces every time.

The ternary operator for simple choices#

For choosing between two values, condition ? a : b is shorter than if/else:

Java
public class Main {
    public static void main(String[] args) {
        int stock = 0;
        String label = stock > 0 ? "In stock" : "Sold out";
        System.out.println(label);
    }
}
Output
Sold out

Common mistakes#

Practice#

Practice exercise

A shop gives 20% off orders of 5000 or more, 10% off orders of 2000 or more, and nothing otherwise. For an order of 3500, print the discount percentage and the amount to pay.

Show solution
Java
public class Discount {
    public static void main(String[] args) {
        double order = 3500;
        int discount = 0;

        if (order >= 5000) {
            discount = 20;
        } else if (order >= 2000) {
            discount = 10;
        }

        double pay = order - order * discount / 100;
        System.out.println("Discount: " + discount + "%");
        System.out.println("Pay: " + pay);
    }
}
Output
Discount: 10%
Pay: 3150.0

Practice exercise

Check whether year = 2024 is a leap year: divisible by 4, except years divisible by 100, unless they are also divisible by 400.

Show solution
Java
public class LeapYear {
    public static void main(String[] args) {
        int year = 2024;
        boolean leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

        if (leap) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }
    }
}
Output
2024 is a leap year

The standard library also has java.time.Year.isLeap(2024).

Key takeaway#

Finished this lesson?