One of the first and most important things you’ll learn in Java programming is how to make decisions in your code. That’s where
if-else statements come in. They let your program decide what to do next based on a condition—just like how we make decisions in everyday life.
For example:
“If it’s raining, take an umbrella. Otherwise, wear sunglasses.”
In Java, we can write this kind of logic using
if,
else if, and
else. In this article, you’ll learn what if-else statements are, how they work, and how to use them to add decision-making power to your Java programs.
Let’s get started!
What Is an If-Else Statement?
An
if-else statement is a control structure that lets you run different blocks of code based on whether a condition is
true
or
false
.
Here’s the basic idea:
if (condition) {
// do this if the condition is true
} else {
// do this if the condition is false
}
The Basic If Statement
The
if
statement checks a condition. If it’s
true
, the code inside the block runs.
Example:
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
}
Since age
is 20, the condition age >= 18
is true, so the message is printed.
If the condition is false, nothing happens—unless you add an else
.
Adding an Else Block
Use
else
to tell the program what to do when the
if
condition is not true.
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
In this case, the program prints “You are a minor.” because age >= 18
is false.
Using Else If for Multiple Conditions
What if you want to check more than one condition? That’s where
else if
comes in.
Example:
int score = 75;
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: F");
}
This checks the conditions one by one until it finds one that’s true. In this case, since 75 is greater than 70, it prints “Grade: C”.
Important Rules to Remember
- Java checks conditions from top to bottom.
- Once it finds a true condition, it runs that block and skips the rest.
- The
else
block is optional, but useful when you want to handle all other cases.
- Curly braces
{}
are required for multiple statements, but optional for just one line (though still recommended).
Example without braces:
if (score > 50)
System.out.println("Passed");
else
System.out.println("Failed");
This works, but using braces makes your code easier to read and avoids mistakes:
if (score > 50) {
System.out.println("Passed");
} else {
System.out.println("Failed");
}
Nested If-Else Statements
Sometimes, you might need to put one
if
inside another. This is called
nesting.
Example:
int age = 25;
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.");
}
While nesting is useful, try not to go too deep—it can make your code harder to follow.
Real-World Example: Login Check
Here’s a simple example of how you might use if-else in a login system:
String username = "admin";
String password = "1234";
if (username.equals("admin") && password.equals("1234")) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid credentials.");
}
We use &&
to make sure both conditions are true.
Comparing If-Else to Switch (Bonus Tip)
Sometimes
if-else
chains can be replaced with a
switch
statement—for example, when comparing the same variable to different values.
But for range-based conditions (like scores or ages),
if-else
is the better choice.
Common Mistakes to Avoid
- Forgetting the double equals (
==
): Use ==
to compare numbers, but use .equals()
to compare strings.
- Missing curly braces: This can lead to unexpected results, especially with more than one statement.
- Wrong logic order: Always write the most specific conditions first to avoid incorrect matches.
Example of wrong order:
if (score >= 70) {
System.out.println("C");
} else if (score >= 90) {
System.out.println("A"); // This will never run!
}
image quote pre code