Java If-Else Statements
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
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);
}
}You pay: Rs. 1100The block on line 5 runs only when the condition amount >= 1000 is true. Line 7 runs either way.
if-else: One of Two Paths
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");
}
}
}Change: Rs. 50Exactly 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:
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);
}
}82 marks: grade BHow it works
- Line 5:
82 >= 90is false, so Java moves to the next check. - Line 7:
82 >= 75is true, sogradebecomes"B". - The rest of the ladder is skipped, even though
82 >= 60and82 >= 35are also true. - The final
elsecatches 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:
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");
}
}
}
}Please show an ID for the concessionThe 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
- Easy: Print "Even" or "Odd" for a number using
if-elseand%. - 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).
Key Takeaways
Was this tutorial helpful?