100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

if-else in Java

Learn how Java's if-else statement controls program flow with conditional branching, including if, if-else, else-if ladders, and nested if statements.

Control FlowBeginner8 min readJul 7, 2026
Analogies

1. Introduction

The if-else statement is Java's fundamental decision-making construct. It allows a program to execute different blocks of code depending on whether a boolean condition evaluates to true or false, letting programs react intelligently to varying input and state.

🏏

Cricket analogy: The if-else statement is like a captain deciding whether to review an umpire's decision: if the ball clearly hit the stumps, review; else, accept the call — the program branches based on a true/false read of the situation.

Java provides several forms of conditional branching built on if: the plain if, the if-else pair, the else-if ladder for multiple alternatives, and nested if statements for checking conditions within conditions.

🏏

Cricket analogy: Java's conditional forms mirror match-day decisions: a plain if is 'if it rains, cover the pitch'; if-else adds an alternative like 'else play on'; an else-if ladder handles multiple weather thresholds; and a nested if checks sub-conditions, like whether the outfield is also wet once rain is confirmed.

2. Syntax

java
// Simple if
if (condition) {
    // executes when condition is true
}

// if-else
if (condition) {
    // true branch
} else {
    // false branch
}

// else-if ladder
if (condition1) {
    // branch 1
} else if (condition2) {
    // branch 2
} else {
    // default branch
}

// nested if
if (outerCondition) {
    if (innerCondition) {
        // runs only if both are true
    }
}

3. Explanation

The condition inside the parentheses of an if statement must be a boolean expression (an expression of type boolean, or one that evaluates to true/false). Unlike C or C++, Java does not allow integers to be used directly as conditions — if (1) is a compile-time error.

🏏

Cricket analogy: Unlike an old-school scorer who might accept a scribbled '1' as shorthand for 'out', Java's if statement insists on a proper boolean verdict — if (1) is rejected outright, just as an umpire must give a clear out/not-out, never a number.

In an else-if ladder, conditions are evaluated top to bottom, and the first one that evaluates to true has its block executed; all remaining branches are skipped. If no condition matches, the final else block (if present) runs.

🏏

Cricket analogy: In an else-if ladder, umpires check appeals top to bottom — lbw first, then caught behind, then run out — and the moment one condition is confirmed true, that's the decision given and the rest are skipped; if none apply, the batter is given not-out by default.

Exam trap: Java requires boolean expressions in if conditions — writing if (x = 5) (assignment instead of ==) is a compile error in Java because the assignment result is an int, not a boolean, unlike C where this silently compiles.

Braces {} are optional when a branch contains a single statement, but omitting them is a common source of bugs (the classic 'dangling else' problem) and is generally discouraged in production code.

4. Example

java
public class GradeChecker {
    public static void main(String[] args) {
        int marks = 76;

        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");
        }

        // Nested if example
        boolean hasPassed = marks >= 40;
        if (hasPassed) {
            if (marks >= 75) {
                System.out.println("Eligible for merit scholarship");
            } else {
                System.out.println("Passed but not eligible for scholarship");
            }
        } else {
            System.out.println("Did not pass");
        }
    }
}

5. Output

text
Grade: B
Passed but not eligible for scholarship

6. Key Takeaways

  • if conditions must be boolean expressions; Java does not implicitly convert int to boolean.
  • else-if ladders execute only the first matching branch, then skip the rest.
  • Nested if statements let you test a condition only after an outer condition is satisfied.
  • Omitting braces for single-statement blocks is legal but risky and best avoided.
  • The final else in a ladder acts as a catch-all default case.
  • Assignment (=) inside a condition is a compile-time type error in Java, unlike some other languages.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#IfElseInJava#Else#Syntax#Explanation#Example#StudyNotes#SkillVeris