Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java if-else Statement
Syntax and Examples



In Java, the if-else statement lets your program choose between different paths based on conditions. Imagine telling a story: if it rains, you stay home; otherwise, you go out. That’s if-else in action.

Syntax of if-else Statement

if (condition) {
    // Runs if condition is true
} else {
    // Runs if condition is false
}

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

Real-world Example: Login Attempt

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

        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?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M