Introduction to Java · Introduction to Java Programming

If-Else Statement

Watch lectures

If-Else Statement

1. Plain-English Idea (beginner)

If condition true → do this block, else → do that block. The only way to branch. Condition must be boolean (Java won't accept if(5) like C!).

int marks = 75;
if (marks >= 40) System.out.println("Pass"); // single line (no braces ok, risky!)

if (marks >= 40) { System.out.println("Pass"); }      // block (safe style)
else { System.out.println("Fail"); }

if (marks >= 90) System.out.println("A");             // ladder (grade!)
else if (marks >= 75) System.out.println("B");
else if (marks >= 40) System.out.println("C");
else System.out.println("F");

Always use braces even for one line — prevents dangling-else bugs.

4. Practice (pass/fail + even/odd)

if (n % 2 == 0) System.out.println("even");
else System.out.println("odd");

5. Exam Points

  • Ladder program (grades) + output trace — 5-7 marks, very frequent.
  • = vs ==, dangling else — 3-5 marks, favourite traps.
Budget AppAndroid App

Pocketly on Android

Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

← All Introduction to Java Programming modules