In short
Programs make decisions all day: is the password right, is the cart above the free-delivery limit, did the student pass? if-else is how Java chooses which path to take.
if, else if and else run different blocks based on boolean conditions. Java conditions must be boolean, so you cannot use an int where a true/false value is expected.

Think of it like this
The fork on a highway
Driving from Delhi, a signboard says: left for Agra, right for Jaipur, straight for everything else. You take exactly one road. An if, else if, else chain is that signboard: Java checks each sign in order and takes the first road whose condition is true.
Watch it happen
Grading marks = 72
1/4First check: is 72 at least 90? No, move on.
The big ideas
What you need to know
Conditions are boolean
if (count) does not compile in Java. Write if (count > 0). Only true or false is allowed.
Order matters
Java checks from top to bottom and stops at the first true condition. Put the most specific checks first.
Always use braces
Without { }, only the next single line belongs to the if. Braces prevent nasty surprises when you add lines.
Combine with && and ||
if (age >= 18 && hasLicence) keeps related checks in one readable condition.
See it in code
Walk through the code
1public class Grader {2 static String grade(int marks) {3 if (marks < 0 || marks > 100) {4 return "Invalid";5 } else if (marks >= 90) {6 return "A";7 } else if (marks >= 75) {8 return "B+";9 } else if (marks >= 60) {10 return "B";11 } else if (marks >= 40) {12 return "C";13 } else {14 return "Fail";15 }16 }17 18 public static void main(String[] args) {19 System.out.println(grade(72)); // B20 System.out.println(grade(95)); // A21 System.out.println(grade(120)); // Invalid22 }23}1/4Guard against bad input before anything else.
A grade calculator. Invalid input is caught first, then ranges are checked from highest to lowest.
Save yourself the pain
Common mistakes
Oops
Checking marks >= 40 before marks >= 90.
Do this instead
95 would match the first check and get a C. Order ranges from most to least specific.
Oops
if (isPaid = true)
Do this instead
That assigns true and is always true. Write if (isPaid).
Your mission
Electricity bill slabs
Quick check
With int n = 5;, which line compiles in Java?
Go deeper
- 1Write a program that prints a grade from a mark.
- 2Check if a year is a leap year using combined conditions.
- 3Always use braces, even for one-line blocks, to avoid bugs.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.