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

Java if-else Condition – Interview Questions

Q 1: What is if-else statement in Java?

Ans: It executes code based on a condition.

Q 2: When is else block executed?

Ans: When the if condition is false.

Q 3: Can if exist without else?

Ans: Yes, else is optional.

Q 4: What is nested if-else?

Ans: An if-else inside another if-else.

Q 5: Can conditions be combined in if?

Ans: Yes, using logical operators.

Java If Else Condition – Objective Questions (MCQs)

Q1. Which of the following is the correct syntax for an if statement in Java?






Q2. What will be the output of the following code?

int x = 10;
if (x > 5)
System.out.println('Greater');
else
System.out.println('Smaller');






Q3. Which of the following can replace condition in an if statement?






Q4. What will the following code print?

int a = 5, b = 10;
if (a > b)
System.out.println('A is greater');
else if (a == b) 
System.out.println('Equal');
else
System.out.println('B is greater'); 






Q5. What will be the output of this code?

int num = 0;
if (num)
System.out.println('Positive');
else
System.out.println('Zero or Negative'); 






Related Java If Else Condition Topics