Lesson 20 of 50 – Java If-Else Statements
40%

Java If-Else Statements

Decision-making statements allow a Java program to execute different blocks of code depending on whether a condition is true or false.

1. What Is an If Statement?

The if statement executes a block of code only when its condition is true.

Syntax:

if (condition) {
    // Code executes if the condition is true
}

Example:

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

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

The condition age >= 18 is true, so Java executes the statement inside the if block.

2. If Statement with a False Condition

If the condition is false, Java skips the code inside the if block.

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

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

        System.out.println("Program finished.");
    }
}
Output:
Program finished.

The first message is not displayed because the condition is false.

3. If-Else Statement

The if-else statement selects one of two blocks. If the condition is true, the if block runs; otherwise, the else block runs.

Syntax:

if (condition) {
    // Executes when condition is true
} else {
    // Executes when condition is false
}

Example:

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");
        }
    }
}
Output:
Odd number

The remainder of 7 % 2 is 1, so the condition is false and the else block runs.

4. Else-If Ladder

An else-if ladder checks multiple conditions in order. The first true condition's block executes, and the remaining conditions are skipped.

Syntax:

if (condition1) {
    // First block
} else if (condition2) {
    // Second block
} else if (condition3) {
    // Third block
} else {
    // Default block
}

Example: Checking Marks

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

        if (marks >= 90) {
            System.out.println("Grade A+");
        } else if (marks >= 80) {
            System.out.println("Grade A");
        } else if (marks >= 60) {
            System.out.println("Grade B");
        } else if (marks >= 40) {
            System.out.println("Grade C");
        } else {
            System.out.println("Fail");
        }
    }
}
Output:
Grade A
Conditions are checked from top to bottom. Put more restrictive or higher-grade conditions first.
5. Nested If Statement

A nested if means placing one if statement inside another if statement.

public class Main {
    public static void main(String[] args) {
        int age = 20;
        boolean hasID = true;

        if (age >= 18) {
            if (hasID) {
                System.out.println("Entry allowed.");
            } else {
                System.out.println("Please show your ID.");
            }
        } else {
            System.out.println("Entry not allowed.");
        }
    }
}
Output:
Entry allowed.

The inner if statement is checked only after the outer condition is true.

6. Checking Positive, Negative, or Zero

An if-else-if ladder can classify a number as positive, negative, or zero.

public class Main {
    public static void main(String[] args) {
        int number = -5;

        if (number > 0) {
            System.out.println("Positive number");
        } else if (number < 0) {
            System.out.println("Negative number");
        } else {
            System.out.println("Zero");
        }
    }
}
Output:
Negative number

Try changing number to 10 or 0 to see the other outputs.

7. Checking Whether a Number Is Even or Odd

The remainder operator % helps determine whether a number is even or odd.

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

        if (number % 2 == 0) {
            System.out.println("Even number");
        } else {
            System.out.println("Odd number");
        }
    }
}
Output:
Even number

If the remainder after dividing by 2 is zero, the number is even; otherwise, it is odd.

8. Using Logical Operators in Conditions

Logical operators combine conditions inside an if statement.

Operator Meaning
&& Both conditions must be true.
|| At least one condition must be true.
! Reverses a Boolean condition.
public class Main {
    public static void main(String[] args) {
        int age = 22;
        boolean hasID = true;

        if (age >= 18 && hasID) {
            System.out.println("You can enter.");
        } else {
            System.out.println("Entry denied.");
        }
    }
}
Output:
You can enter.

Both conditions must be true for the && expression to be true.

9. Finding the Largest of Two Numbers

You can use an if-else statement to compare two numbers.

public class Main {
    public static void main(String[] args) {
        int a = 25;
        int b = 40;

        if (a > b) {
            System.out.println(a + " is larger.");
        } else if (b > a) {
            System.out.println(b + " is larger.");
        } else {
            System.out.println("Both numbers are equal.");
        }
    }
}
Output:
40 is larger.

The final else handles the case when both numbers are equal.

10. Checking a Leap Year

A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not divisible by 100.

public class Main {
    public static void main(String[] args) {
        int year = 2024;

        if (year % 400 == 0) {
            System.out.println("Leap year");
        } else if (year % 100 == 0) {
            System.out.println("Not a leap year");
        } else if (year % 4 == 0) {
            System.out.println("Leap year");
        } else {
            System.out.println("Not a leap year");
        }
    }
}
Output:
Leap year

Try testing the program with years 2000, 1900, 2024, and 2023.

11. Checking Pass or Fail

Use conditions to check whether a student has passed an examination.

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

        if (marks >= 40 && marks <= 100) {
            System.out.println("Pass");
        } else if (marks >= 0 && marks < 40) {
            System.out.println("Fail");
        } else {
            System.out.println("Invalid marks");
        }
    }
}
Output:
Pass

This example checks that marks are between 0 and 100 and treats values outside that range as invalid.

12. Common If-Else Mistakes
Mistake 1: Using = instead of ==
// Incorrect
// if (number = 10) { }

// Correct
if (number == 10) {
    System.out.println("Number is 10");
}
Mistake 2: Missing braces in complex conditions
if (age >= 18) {
    System.out.println("Adult");
    System.out.println("Eligible to apply");
}
Mistake 3: Incorrect condition order
// Incorrect order for grading
if (marks >= 40) {
    System.out.println("Grade C");
} else if (marks >= 90) {
    System.out.println("Grade A");
}

In this example, marks of 95 match the first condition, so the Grade A condition is never reached.

Correct order:

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 40) {
    System.out.println("Grade C");
} else {
    System.out.println("Fail");
}
13. Practical Example: Simple Calculator

This program uses if-else-if statements to perform arithmetic operations based on an operator.

public class Main {
    public static void main(String[] args) {
        double a = 20;
        double b = 5;
        char operator = '*';

        if (operator == '+') {
            System.out.println("Result: " + (a + b));
        } else if (operator == '-') {
            System.out.println("Result: " + (a - b));
        } else if (operator == '*') {
            System.out.println("Result: " + (a * b));
        } else if (operator == '/') {
            if (b != 0) {
                System.out.println("Result: " + (a / b));
            } else {
                System.out.println("Cannot divide by zero.");
            }
        } else {
            System.out.println("Invalid operator.");
        }
    }
}
Output:
Result: 100.0
Lesson Summary:
  • if executes code when a condition is true.
  • if-else selects between two blocks.
  • else-if checks multiple conditions.
  • Nested if statements check conditions inside other conditions.
  • Logical operators combine conditions.
  • Always check condition order and boundary values.

🧠 Quick Quiz

Q1. Which statement executes code only when a condition is true?



Q2. What is the output if number = 8?

if (number % 2 == 0) {
    System.out.println("Even");
} else {
    System.out.println("Odd");
}


Q3. Which operator means logical AND in Java?