If Else

If Else

A school program needs reliable data and clear rules. An if-else statement chooses exactly one of two branches. A student either meets this report’s pass threshold or needs further practice.

  1. Understand

    An if-else statement chooses exactly one of two branches.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    Two separate if statements are not the same as one if-else decision: both independent conditions could be true.

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.

Flow diagram: key actions and decisions

  1. Start
  2. condition?
  3. if-block
  4. else-block
  5. End

These labels identify the actions and decisions in the original diagram; the list does not represent branch order. Follow the Java code for the exact execution path.

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!

Important boundaries and mistakes

Two separate if statements are not the same as one if-else decision: both independent conditions could be true.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Two separate if statements are not the same as one if-else decision: both independent conditions could be true..

Java 17 · Main.java · complete practice program

public class Main  {
  public static void main(String[] args)  {
    int marks = 40;
    if (marks >= 50)  {
      System.out.println("Pass");
    }
    else  {
      System.out.println("Practise");
    }
  }
}

Now change one thing: Change marks to 72. Predict the new behavior before checking the worked variation.

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  public static void main(String[] args)  {
    int marks = 40;
    if (marks >= 50)  {
      System.out.println("Pass");
    }
    else  {
      System.out.println("Practise");
    }
  }
}

Expected output

Practise

An if-else statement chooses exactly one of two branches. A student either meets this report’s pass threshold or needs further practice.

Two separate if statements are not the same as one if-else decision: both independent conditions could be true.

Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.

Worked variation: Change marks to 72. The Pass branch runs and the Practise branch does not.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

Practise

An if-else statement chooses exactly one of two branches. A student either meets this report’s pass threshold or needs further practice. Two separate if statements are not the same as one if-else decision: both independent conditions could be true.

What to remember

An if-else statement chooses exactly one of two branches. A student either meets this report’s pass threshold or needs further practice. Two separate if statements are not the same as one if-else decision: both independent conditions could be true.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: If Else If →