⬅ Previous Topic
Java if-else StatementNext Topic ⮕
Java Switch Statement⬅ Previous Topic
Java if-else StatementNext Topic ⮕
Java Switch StatementIn Java, if-else-if
ladder is used to evaluate multiple conditions in a structured manner. If you’ve already understood simple if and if-else blocks, this is the next natural step.
The if-else-if
ladder allows you to test multiple conditions, one after another. As soon as one condition evaluates to true
, the corresponding block is executed, and the rest are skipped.
if else if
in Javaif (condition1) {
// code block 1
} else if (condition2) {
// code block 2
} else if (condition3) {
// code block 3
} else {
// default code block
}
true
, it executes that block and ignores the rest.true
, the else
block runs (if provided).public class GradeExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D or lower");
}
}
}
Grade: B
Here, score
is 85. It first checks if it's greater than or equal to 90 (false), then checks if it's greater than or equal to 80 (true). So, it prints "Grade: B" and exits the ladder.
public class WeatherExample {
public static void main(String[] args) {
int temperature = 5;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else if (temperature > 20) {
System.out.println("It's a warm day.");
} else if (temperature > 10) {
System.out.println("It's a bit chilly.");
} else {
System.out.println("It's cold outside.");
}
}
}
It's cold outside.
In this program, we're using a series of if-else if-else
conditions to print a message based on the current temperature.
temperature
is 5.if (temperature > 30)
– this is false
.else if (temperature > 20)
– still false
.else if (temperature > 10)
– again false
.else
block executes.Therefore, it prints: It's cold outside.
This type of conditional logic is useful for decision-making based on numeric ranges or categories.
public class NestedIfElseIf {
public static void main(String[] args) {
int marks = 78;
char section = 'A';
if (marks >= 75) {
if (section == 'A') {
System.out.println("Eligible for Scholarship in Section A");
} else if (section == 'B') {
System.out.println("Eligible for Scholarship in Section B");
} else {
System.out.println("Eligible but section not identified");
}
} else {
System.out.println("Not eligible for scholarship");
}
}
}
Eligible for Scholarship in Section A
This program demonstrates how nested if-else
conditions work in Java. The outer if
checks whether the student has scored 75 or more marks. If this condition is true, the program then checks the section
to determine the specific eligibility message:
else
)In this specific case, since marks = 78
and section = 'A'
, the output is:
Eligible for Scholarship in Section A
if else if
=
instead of ==
in conditions (assignment vs comparison).else
block when it's needed to handle all unexpected cases.Use if-else-if
when:
The if-else-if
structure in Java is all about making your program responsive to different inputs and situations. By practicing with everyday logic like grading systems, weather classifications, or eligibility checks, you build the instinct to apply this structure naturally in your code.
Change the values in the above examples and see how the outputs differ. Experiment with adding new conditions and see where they fall in the ladder. This is the best way to gain confidence with flow control.
if-else-if
ladder behave when multiple conditions evaluate to true
?if-else-if
structure, the else
block is mandatory and must always be included.if-else-if
ladder?
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: D or lower");
}
if-else-if
ladder can be replaced with a switch
statement in all cases.
int temperature = 5;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else if (temperature > 20) {
System.out.println("It's a warm day.");
} else if (temperature > 10) {
System.out.println("It's a bit chilly.");
} else {
System.out.println("It's cold outside.");
}
⬅ Previous Topic
Java if-else StatementNext Topic ⮕
Java Switch StatementYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.