Thuta Learning
BasicProgrammingbeginner

If...Else

Relax. We'll talk through this in plain words — no textbook voice.

if...else is a control flow structure that lets a program make decisions. If the condition is true, one block runs; if not, another block runs. Whenever you want your program to "do different things depending on the situation," you use if/else.

java
public class Main {
  public static void main(String[] args) {
    int mark = 75;

    if (mark >= 80) {
      System.out.println("Distinction");
    } else if (mark >= 40) {
      System.out.println("Pass");
    } else {
      System.out.println("Fail");
    }
  }
}

The program checks mark from the top down. If it's not 80 or above, it checks the next condition: 40 or above. Since 75 is 40 or above, it prints Pass.

You should see
Pass

Real-world use

if/else is used for things like login success/failure, payment approved/declined, grade results, age restrictions, and permission checks.

Easy traps

  • Writing if (mark = 75) with the assignment operator, or getting your condition ranges wrong, can lead to logic bugs.