Java if-else Condition

The if-else condition in Java is used to perform decision-making. It allows the program to execute one block of code if a condition is true and another block of code if the condition is false.

Syntax


if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

Working Process:

  1. The if statement evaluates a condition (a boolean expression).
  2. If the condition evaluates to true, the code inside the if block is executed.
  3. If the condition evaluates to false, the code inside the else block is executed.

Example:


public class Example1 {
    public static void main(String[] args) {
        int number = 12;

        if (number % 2 == 0) {
            System.out.println("The number is even.");
        } else {
            System.out.println("The number is odd.");
        }
    }
}

Output: The number is even.

Java Nested if-else

You can place one if-else block inside another for multiple conditions.

Syntax:


if (condition1) {
    // Code if condition1 is true
} else {
    if (condition2) {
        // Code if condition2 is true
    } else {
        // Code if both conditions are false
    }
}

Example:


public class Example2 {
    public static void main(String[] args) {
        int marks = 95;

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

Output: Grade: A

Java if-else if Ladder Statement

When there are multiple conditions to check, you can use the if-else if ladder.

Syntax:


if (condition1) {
    // Code if condition1 is true
} else if (condition2) {
    // Code if condition2 is true
} else {
    // Code if none of the above conditions are true
}

Example:


public class Example3 {
    public static void main(String[] args) {
        int temperature = 25;

        if (temperature > 40) {
            System.out.println("It's very hot.");
        } else if (temperature > 30) {
            System.out.println("It's warm.");
        } else if (temperature > 20) {
            System.out.println("It's pleasant.");
        } else {
            System.out.println("It's cold.");
        }
    }
}

Output: It’s pleasant.

Important Points

1. Conditions must return a boolean value. Non-boolean expressions (like integers) cannot be used directly in if conditions.


if (1) { // This is NOT allowed in Java
    System.out.println("This will give a compilation error.");
}

2. The else block is optional. You can use if without else if no alternate action is required.


if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

3. Braces {} are optional for a single statement. However, it is a good practice to include them for better readability.


if (number > 0)
    System.out.println("Positive number");
else
    System.out.println("Negative number");