Java If-Else Statements

Naveen AnamaneniUpdated · 5 min read

A shopkeeper giving change doesn't do the same thing for every customer. If you hand over exact money, there's nothing to return. If you pay more, you get change back. If you pay less, you're asked for more. That's a decision with conditions, and in Java it's written with if, else if and else.

if: Do It Only When True

Discount.java▶ Try it
public class Discount {
    public static void main(String[] args) {
        int amount = 1200;
        if (amount >= 1000) {
            amount = amount - 100;
        }
        System.out.println("You pay: Rs. " + amount);
    }
}
Output
You pay: Rs. 1100

The block on line 5 runs only when the condition amount >= 1000 is true. Line 7 runs either way.

if-else: One of Two Paths

Change.java▶ Try it
public class Change {
    public static void main(String[] args) {
        int price = 450, paid = 500;
        if (paid >= price) {
            System.out.println("Change: Rs. " + (paid - price));
        } else {
            System.out.println("Please pay Rs. " + (price - paid) + " more");
        }
    }
}
Output
Change: Rs. 50

Exactly one of the two blocks runs: the if block when the condition is true, the else block when it's false.

The else-if Ladder: Many Paths

When there are more than two possibilities, chain conditions with else if. Java checks them from top to bottom and runs the first one that's true:

The first true condition wins; the rest are skipped
Grade.java▶ Try it
public class Grade {
    public static void main(String[] args) {
        int marks = 82;
        String grade;
        if (marks >= 90) {
            grade = "A";
        } else if (marks >= 75) {
            grade = "B";
        } else if (marks >= 60) {
            grade = "C";
        } else if (marks >= 35) {
            grade = "D";
        } else {
            grade = "F";
        }
        System.out.println(marks + " marks: grade " + grade);
    }
}
Output
82 marks: grade B

How it works

  1. Line 5: 82 >= 90 is false, so Java moves to the next check.
  2. Line 7: 82 >= 75 is true, so grade becomes "B".
  3. The rest of the ladder is skipped, even though 82 >= 60 and 82 >= 35 are also true.
  4. The final else catches everything that matched nothing above it.

Try it yourself: move the marks >= 35 check to the top of the ladder. What grade does 82 get now, and why?

Nested if

An if can sit inside another if when the second question only makes sense after the first:

Concession.java▶ Try it
public class Concession {
    public static void main(String[] args) {
        int age = 64;
        boolean hasId = false;
        if (age >= 60) {
            if (hasId) {
                System.out.println("Senior concession applied");
            } else {
                System.out.println("Please show an ID for the concession");
            }
        }
    }
}
Output
Please show an ID for the concession

The inner question, "do you have an ID?", is only asked of seniors. When two conditions simply both need to be true, if (age >= 60 && hasId) is often clearer than nesting.

Common Mistakes

Separate ifs where you meant a ladder. Three separate if (marks >= ...) statements can each run, overwriting the grade. A ladder runs at most one branch.

Checking ranges in the wrong order. if (marks >= 35) first catches nearly everyone, so the higher grades are never reached.

Comparing Strings with ==. Use city.equals("Pune"). The strings lessons, later in the course, explain why == can fail for text.

Practice

  1. Easy: Print "Even" or "Odd" for a number using if-else and %.
  2. Harder: Write an electricity bill calculator: the first 100 units cost Rs. 3 each, the next 100 cost Rs. 5 each, and anything above 200 costs Rs. 8 each. Print the bill for 250 units (it should be Rs. 1,200).

Quick check: if-else

4 questions
1In an else-if ladder, how many branches run at most?
2marks = 95. The first check is marks >= 35. Which grade results from the ladder in this lesson, reordered that way?
3Without braces, which if does an else belong to?
4When is a nested if better than &&?

Key Takeaways

Was this tutorial helpful?

Lesson 54 of 482