-
Notifications
You must be signed in to change notification settings - Fork 0
If, Else If, and Else – Making Decisions in Java
Seanti edited this page Mar 24, 2025
·
1 revision
Just like in real life, Java needs a way to make decisions. If-else statements allow your program to take different actions based on conditions.
Imagine school grade levels:
- If your grade is 13 or higher, you're in college.
- Else if your grade is 11 or 12, you're in senior high school.
- Else if your grade is 7 to 10, you're in junior high school.
- Else if your grade is 1 to 6, you're in elementary.
- Else, you’re not in school yet!
In Java, we use if, else if, and else to create this decision-making logic.
Let’s check what school level a student is in based on their grade:
int grade = 10; // The grade level of the student
if (grade >= 13) {
System.out.println("You are in college.");
} else if (grade >= 11) {
System.out.println("You are in senior high school.");
} else if (grade >= 7) {
System.out.println("You are in junior high school.");
} else if (grade >= 1) {
System.out.println("You are in elementary.");
} else {
System.out.println("You are not in school yet.");
}- If
gradeis 13 or more, we print"You are in college."and skip the rest. - Else if
gradeis 11 or 12, we print"You are in senior high school.". - Else if
gradeis 7 to 10, we print"You are in junior high school.". - Else if
gradeis 1 to 6, we print"You are in elementary.". - Else, we print
"You are not in school yet.".
Input (grade) |
Output |
|---|---|
14 |
"You are in college." |
11 |
"You are in senior high school." |
8 |
"You are in junior high school." |
5 |
"You are in elementary." |
0 |
"You are not in school yet." |
Some beginners write multiple if statements like this:
int grade = 10;
if (grade >= 13) {
System.out.println("You are in college.");
}
if (grade >= 11) {
System.out.println("You are in senior high school.");
}
if (grade >= 7) {
System.out.println("You are in junior high school.");
}
if (grade >= 1) {
System.out.println("You are in elementary.");
}Even if grade = 14, Java checks all four if-statements, even though the first one already tells us they are in college. This is inefficient!
int grade = 10;
if (grade >= 13) {
System.out.println("You are in college.");
} else if (grade >= 11) {
System.out.println("You are in senior high school.");
} else if (grade >= 7) {
System.out.println("You are in junior high school.");
} else if (grade >= 1) {
System.out.println("You are in elementary.");
} else {
System.out.println("You are not in school yet.");
}- If the student is in college, we print only
"You are in college."and skip the rest. - If the student is in senior high, we print
"You are in senior high school."and skip checking for junior high. - If the student is in junior high, we print
"You are in junior high school."and skip everything else.
✅ Each student only gets checked once!
There are times when using separate if statements is correct.
boolean isRaining = true;
boolean isCold = true;
if (isRaining) {
System.out.println("Bring an umbrella.");
}
if (isCold) {
System.out.println("Wear a jacket.");
}- Whether it’s raining does not affect whether it’s cold.
- Both messages should be printed independently if both are true.
Bring an umbrella.
Wear a jacket.
✅ Both if statements run because they are unrelated!
| Approach | When to Use It | Example |
|---|---|---|
if-else if-else |
When conditions are connected (one decision path) | Checking grade level |
if if if |
When conditions are independent (not part of the same decision flow) | Checking if it's raining and if it's cold |