Java If...Else

Making decisions in your Java programs

🤔 What is Java If...Else?

Java If...Else statements allow your program to make decisions and execute different code blocks based on conditions. They control the flow of your program execution.


// Simple if statement
int age = 18;
if (age >= 18) {
    System.out.println("You are an adult!");
} else {
    System.out.println("You are a minor!");
}
                                    

Output:

You are an adult!

Types of If Statements

➡️

Simple If

Execute code if condition is true

if (x > 0) {
    System.out.println("Positive");
}
🔀

If-Else

Choose between two options

if (x > 0) {
    System.out.println("Positive");
} else {
    System.out.println("Not positive");
}
🔗

Else If

Multiple conditions to check

if (x > 0) {
    System.out.println("Positive");
} else if (x < 0) {
    System.out.println("Negative");
} else {
    System.out.println("Zero");
}
🎯

Nested If

If statements inside other if statements

if (x > 0) {
    if (x > 10) {
        System.out.println("Big positive");
    }
}

🔹 Basic If-Else Example

Simple decision making with if-else:

public class IfElseExample {
    public static void main(String[] args) {
        int score = 85;
        
        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 80) {
            System.out.println("Grade: B");
        } else if (score >= 70) {
            System.out.println("Grade: C");
        } else if (score >= 60) {
            System.out.println("Grade: D");
        } else {
            System.out.println("Grade: F");
        }
        
        System.out.println("Your score: " + score);
    }
}

Output:

Grade: B

Your score: 85

🔹 Multiple Conditions

Using logical operators in if statements:

public class MultipleConditions {
    public static void main(String[] args) {
        int age = 25;
        boolean hasJob = true;
        double salary = 50000;
        
        // Multiple conditions with AND
        if (age >= 18 && hasJob && salary > 30000) {
            System.out.println("Eligible for loan");
        } else {
            System.out.println("Not eligible for loan");
        }
        
        // Multiple conditions with OR
        if (age < 18 || salary < 20000) {
            System.out.println("Special discount available");
        } else {
            System.out.println("Regular pricing applies");
        }
    }
}

Output:

Eligible for loan

Regular pricing applies

🔹 Nested If Statements

If statements inside other if statements:

public class NestedIf {
    public static void main(String[] args) {
        int temperature = 25;
        boolean isSunny = true;
        
        if (temperature > 20) {
            System.out.println("Weather is warm");
            
            if (isSunny) {
                System.out.println("Perfect day for outdoor activities!");
            } else {
                System.out.println("Warm but cloudy");
            }
        } else {
            System.out.println("Weather is cold");
            
            if (temperature < 0) {
                System.out.println("It's freezing!");
            } else {
                System.out.println("Just a bit chilly");
            }
        }
    }
}

Output:

Weather is warm

Perfect day for outdoor activities!

🔹 Ternary Operator

Short form of if-else statement:

public class TernaryOperator {
    public static void main(String[] args) {
        int age = 20;
        
        // Ternary operator: condition ? value_if_true : value_if_false
        String status = (age >= 18) ? "Adult" : "Minor";
        System.out.println("Status: " + status);
        
        // Another example
        int a = 10, b = 20;
        int max = (a > b) ? a : b;
        System.out.println("Maximum: " + max);
        
        // Can be used in print statements
        System.out.println("You are " + ((age >= 18) ? "eligible" : "not eligible") + " to vote");
    }
}

Output:

Status: Adult

Maximum: 20

You are eligible to vote

🧠 Test Your Knowledge

What will be printed if x = 5?
if (x > 10) { System.out.println("Big"); } else { System.out.println("Small"); }