Java if else if else Statement
The if-else if-else
statement in Java allows you to evaluate multiple conditions in sequence. It is useful when you need to check several different conditions and execute different blocks of code based on which condition evaluates to true
.
Syntax of if-else if-else
Statement
if (condition1) {
// Block of code to be executed if condition1 is true
} else if (condition2) {
// Block of code to be executed if condition2 is true
} else if (condition3) {
// Block of code to be executed if condition3 is true
} else {
// Block of code to be executed if none of the above conditions are true
}
- condition1, condition2, condition3: These are boolean expressions that are evaluated in order.
- Blocks of code: The code inside the braces
{}
will execute for the first true condition found. If none of the conditions are true, the code in theelse
block executes.
Example of if-else if-else
Statement
Here’s a simple example demonstrating the use of an if-else if-else
statement to categorize a number:
public class IfElseIfElseExample {
public static void main(String[] args) {
int number = 0;
// Using if-else if-else statement to categorize the number
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
Explanation
- Declaration: The variable
number
is declared and initialized with the value0
. - Condition Check:
- The first condition (
number > 0
) checks if the number is positive. It evaluates tofalse
. - The second condition (
number < 0
) checks if the number is negative. It also evaluates tofalse
. - Since neither of the first two conditions is true, the code in the
else
block executes.
- The first condition (
- Execution: The program outputs:
The number is zero.
Example with Multiple Conditions
Here’s another example that uses an if-else if-else
statement to determine the grade based on a score:
public class GradeCalculator {
public static void main(String[] args) {
int score = 85;
// Using if-else if-else statement to determine the grade
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 if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
Explanation
In this example:
- The variable
score
is initialized with the value85
. - The program checks the score against several conditions:
- If the score is
90
or above, it assigns an "A". - If the score is
80
or above, it assigns a "B". - If the score is
70
or above, it assigns a "C". - If the score is
60
or above, it assigns a "D". - If the score is below
60
, it assigns an "F".
- If the score is
- Since
85
is greater than80
but less than90
, the output will be:Grade: B
Summary
The if-else if-else
statement is a powerful control structure in Java that allows you to evaluate multiple conditions sequentially. It helps in making decisions based on different scenarios, making your code more flexible and dynamic. Understanding how to use this control structure effectively is essential for creating robust Java applications.