SyntaxStudy
Sign Up
Java if, else if, else and the Switch Statement
Java Beginner 1 min read

if, else if, else and the Switch Statement

The if statement is the most basic control-flow construct in Java. It evaluates a boolean expression and executes the associated block if the expression is true. An optional else clause provides code to execute when the condition is false. Multiple conditions can be chained using else if. The condition must be a boolean; unlike C, an integer cannot be used directly as a condition. The switch statement selects one of several execution paths based on the value of an expression. Before Java 14 the expression had to be a byte, short, int, char, String, or enum. The traditional switch uses fall-through semantics: execution continues past a case label unless you add a break statement. This is often a source of bugs. Java 14 introduced switch expressions as a preview feature (finalized in Java 16), which use an arrow syntax that prevents fall-through and can return a value directly. The new form is more concise and less error-prone. The yield keyword is used inside a block arm to provide the resulting value of a switch expression.
Example
public class ControlFlowDemo {

    // Traditional switch with fall-through (avoid unless intentional)
    static String seasonTraditional(int month) {
        String season;
        switch (month) {
            case 12:
            case 1:
            case 2:
                season = "Winter";
                break;
            case 3: case 4: case 5:
                season = "Spring";
                break;
            case 6: case 7: case 8:
                season = "Summer";
                break;
            case 9: case 10: case 11:
                season = "Autumn";
                break;
            default:
                season = "Unknown";
        }
        return season;
    }

    // Modern switch expression (Java 14+) — no fall-through, returns value
    static String seasonModern(int month) {
        return switch (month) {
            case 12, 1, 2  -> "Winter";
            case 3,  4, 5  -> "Spring";
            case 6,  7, 8  -> "Summer";
            case 9, 10, 11 -> "Autumn";
            default -> {
                yield "Unknown (month=" + month + ")";  // yield in block arm
            }
        };
    }

    public static void main(String[] args) {
        // if / else-if / else
        int score = 82;
        String grade;
        if (score >= 90) {
            grade = "A";
        } else if (score >= 80) {
            grade = "B";
        } else if (score >= 70) {
            grade = "C";
        } else {
            grade = "F";
        }
        System.out.println("Score " + score + " => Grade " + grade);

        // Ternary operator (inline if-else for simple assignments)
        String result = (score >= 60) ? "Pass" : "Fail";
        System.out.println("Result: " + result);

        // Switch demos
        for (int m = 1; m <= 12; m++) {
            System.out.printf("Month %2d: traditional=%s, modern=%s%n",
                m, seasonTraditional(m), seasonModern(m));
        }
    }
}