How to Use – Java if else statement
0 CommentsLast Updated on October 26, 2024 by jt
The if else
statement is a fundamental control structures in Java.. This structure evaluates conditions and executes code based on whether those conditions are true
or false
. Let’s look at how to use if
, if else
, and else if
statements effectively.
The Java if Statement
An if
statement evaluates a condition and runs the code block only if the condition is true
.
For example:
int age = 18;
if (age >= 18) {
System.out.println("You are an adult.");
}
In this example, if age
is 18 or above, the message will print. If not, the if block is skipped and nothing happens.
The Java if else Statement
To add an alternative action when the condition is false
, use an if-else
statement:
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are not an adult.");
}
-----------------------output-------------------
You are not an adult.
In this example the age is 16, which is less than 18, so the first output statement is skipped. Since the first block is skipped, the else
block is executed.
The Java if else if
for Multiple Conditions
The previous example was limited to the evaluation of one condition. You can chain multiple conditions using else if
. This is useful when there are more than two possible outcomes:
For example:
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");
}
-----------output--------------
Grade: B
The program checks each condition in sequence, and the first true condition executes. If none of the conditions are true, the else
block runs.
Note: Use of an else block is optional. The purpose of the else block is to provide a failsafe if none of the prior conditions are executed. Sometimes you will want to do nothing, or simply log that you encountered an unexpected condition.
Nested if
Statements
In cases requiring more complex logic, you can nest if
statements inside each other. However, avoid excessive nesting, is considered a code smell. Excessing nesting produces code that is hard to read, and difficult to maintain.
int age = 22;
boolean hasLicense = true;
if (age >= 18) {
if (hasLicense) {
System.out.println("You can drive.");
} else {
System.out.println("You need a license to drive.");
}
} else {
System.out.println("You are too young to drive.");
}
-----------output-----------
You can drive.
Best Practices
- Simplify Conditions: Avoid overly complex conditions for readability.
- Use
else
only when needed: If theelse
case is not meaningful, it’s often better to leave it out. Remember, this is optional. - Minimize Nesting: Excessive nesting can make the code difficult to follow. If you have excessive nested statements, reevaluate the code and try to simplify.
Conclusion
The if-else
structure is a key component of Java programming, allowing you to control the flow of your program based on conditions. Mastering this structure will enable you to write clear, efficient, and adaptable code in any Java application.