Java if-else Statement - Syntax and Examples

if-else Statement

In Java, the if-else statement is used to make decisions in your program. It checks a condition and runs different code depending on whether the condition is true or false.

For example: if it rains, you stay home; else, you go outside. That's how if-else works.

Syntax of if-else Statement

if (condition) {
    // Runs if condition is true
} else {
    // Runs if condition is false
}
  • condition: This is a boolean expression that evaluates to either true or false.
  • If the condition evaluates to true, the code block inside the if block runs.
  • If the condition is false, the code block inside the else block runs instead.
flowchart TD Start([Start]) --> Condition{condition?} Condition -- True --> TrueBlock[if-block] Condition -- False --> FalseBlock[else-block] TrueBlock --> End([End]) FalseBlock --> End %% Apply styles class Start,End startEnd; class Condition decision; class TrueBlock,FalseBlock process;

Basic Example: Check if a Number is Positive

public class Main {
    public static void main(String[] args) {
        int number = 10;
        if (number > 0) {
            System.out.println("The number is positive.");
        }
    }
}
The number is positive.

Example with else: Even or Odd

public class Main {
    public static void main(String[] args) {
        int number = 7;
        if (number % 2 == 0) {
            System.out.println("Even number");
        } else {
            System.out.println("Odd number");
        }
    }
}
Odd number

Nested if Statements

Sometimes you want to check another condition only if the first one passes. That’s when nesting helps.

public class Main {
    public static void main(String[] args) {
        int age = 20;
        int score = 85;

        if (age >= 18) {
            if (score > 80) {
                System.out.println("Eligible for scholarship.");
            } else {
                System.out.println("Eligible, but no scholarship.");
            }
        } else {
            System.out.println("Not eligible.");
        }
    }
}
Eligible for scholarship.

Key Takeaways

  • if checks a condition and runs code when it’s true.
  • else provides an alternate path when the if fails.
  • if-else-if allows multiple conditions to be checked in order.
  • Nested if helps for dependent checks.

Real-world Example: Login Attempt

public class Main {
    public static void main(String[] args) {
        String username = "admin";
        String password = "1234859";

        if (username.equals("admin")) {
            if (password.equals("1234")) {
                System.out.println("Login successful!");
            } else {
                System.out.println("Incorrect password.");
            }
        } else {
            System.out.println("Username not found.");
        }
    }
}
Login successful!

QUIZ

Question 1:What is the role of the else block in an if-else statement in Java?

Question 2:Nested if statements in Java allow you to check a second condition only if the first one is true.

Question 3:Which of the following are true about if-else in Java?

Question 4:What is the output of the following code?
int number = 7;
if (number % 2 == 0) {
    System.out.println("Even number");
} else {
    System.out.println("Odd number");
}

Question 5:In Java, the if block only executes when the condition evaluates to true.

Question 6:Consider this snippet:
int age = 20;
int score = 85;

if (age >= 18) {
    if (score > 80) {
        System.out.println("Eligible for scholarship.");
    } else {
        System.out.println("Eligible, but no scholarship.");
    }
} else {
    System.out.println("Not eligible.");
}
What will be the output?

Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗