Java If Else and Switch

Java if else statements let your program make decisions: run one block of code when a condition is true and another when it is false. For choosing between many fixed values, the switch statement is often cleaner.

if, else if and else

Conditions are checked from top to bottom, and only the first matching block runs.

public class Main {
    public static void main(String[] args) {
        int marks = 73;
        if (marks >= 85) {
            System.out.println("Grade A");
        } else if (marks >= 70) {
            System.out.println("Grade B");
        } else if (marks >= 50) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}
Grade B

Because 73 is not at least 85, the first block is skipped. The second condition is true, so "Grade B" prints and the rest of the chain is ignored. Always use braces, even for single-line blocks; it prevents subtle bugs.

Combining Conditions

public class Main {
    public static void main(String[] args) {
        int hour = 14;
        boolean weekend = false;
        if (!weekend && hour >= 9 && hour < 17) {
            System.out.println("Office is open");
        } else {
            System.out.println("Office is closed");
        }
    }
}
Office is open

Switch Expressions (Modern Java)

Since Java 14, switch can be an expression that returns a value. The arrow form (case X ->) has no fall-through, so you never need break, and several labels can share one case.

public class Main {
    public static void main(String[] args) {
        String day = "SAT";
        String type = switch (day) {
            case "SAT", "SUN" -> "Weekend";
            case "MON", "TUE", "WED", "THU", "FRI" -> "Weekday";
            default -> "Unknown";
        };
        System.out.println(type);

        int month = 2;
        int days = switch (month) {
            case 2 -> 28;
            case 4, 6, 9, 11 -> 30;
            default -> {
                int result = 31;
                yield result;
            }
        };
        System.out.println(days);
    }
}
Weekend
28

When a case needs several statements, use a block and return the value with yield. The compiler checks that a switch expression covers every possibility, which is why default is required here.

Classic Switch Statement

Older code uses the colon form. Without break, execution falls through into the next case, which is a common source of bugs.

public class Main {
    public static void main(String[] args) {
        int level = 2;
        switch (level) {
            case 1:
                System.out.println("Beginner");
                break;
            case 2:
                System.out.println("Intermediate");
                break;
            default:
                System.out.println("Expert");
        }
    }
}
Intermediate

Frequently asked questions

When should I use switch instead of if else in Java?

Use switch when you compare one value against many fixed constants like strings, enums or numbers. Use if else for ranges and complex conditions.

Do I need break in a Java switch?

Only in the classic colon form. The arrow form (case X ->) never falls through, so break is not needed.

Can you use strings in a Java switch?

Yes, since Java 7. The comparison uses equals, so it is case sensitive.