The if statement is useful when we want to execute code only when a condition is true. But many real-world decisions have two possible paths: one action when a condition is true and another when it is false. Java provides the if-else statement for this situation.
Why Do We Need if-else?
Consider a student result system. If the marks are 40 or above, the student passes. Otherwise, the student fails.
With only an if statement, we can handle the passing case, but we also need a clear way to handle the other possibility. The else block provides that second path.
if handles the true condition, while else handles the false condition.
Real-World Analogy
Imagine a simple security gate. The rule is: “If you have a valid pass, allow entry; otherwise, deny entry.”
If valid pass
Allow entry
Else
Deny entry
The program must choose exactly one of these two paths. This is the basic idea behind if-else.
Basic Syntax
if (condition) {
// code when condition is true
} else {
// code when condition is false
}
Java first evaluates the condition. If it is true, the code inside the if block executes. If it is false, Java skips that block and executes the else block.
Simple Example
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}
The value of age is 20, so the condition age >= 18 is true. Therefore, Java executes the first block and prints You are an adult.
If the value of age were 15, the condition would be false, so Java would execute the else block instead.
How if-else Works
- Java evaluates the condition inside the parentheses.
- If the condition is true, the if block executes.
- If the condition is false, the else block executes.
- After one block finishes, Java continues with the statement after the complete if-else structure.
Remember: For a normal if-else statement, only one of the two blocks executes.
Example: Pass or Fail
int marks = 35;
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Here, marks >= 40 is false because the marks are 35. Therefore, Java executes the else block and prints Fail.
Example: Even or Odd
int number = 8;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
The modulus operator % returns the remainder after division. If a number divided by 2 leaves a remainder of 0, it is even. Otherwise, it is odd.
Since 8 divided by 2 has a remainder of 0, the condition is true and the program prints Even number.
Example: Login Decision
String password = "java123";
if (password.equals("java123")) {
System.out.println("Login successful.");
} else {
System.out.println("Invalid password.");
}
This example demonstrates a common application of if-else. The program compares the entered password with the expected password. If they match, login succeeds; otherwise, the failure message is displayed.
When comparing String values in Java, use equals() for content comparison instead of using ==.
if Without else vs if-else
| Structure | Purpose | When False |
|---|---|---|
| if | Handles a condition when it is true. | The block is skipped. |
| if-else | Handles both true and false cases. | The else block executes. |
Nested Code Inside if-else
Both the if and else blocks can contain multiple statements.
int balance = 5000;
int amount = 2000;
if (amount <= balance) {
balance = balance - amount;
System.out.println("Withdrawal successful.");
System.out.println("Remaining balance: " + balance);
} else {
System.out.println("Insufficient balance.");
}
When the withdrawal amount is within the available balance, Java executes all statements inside the if block. Otherwise, it executes the else block.
Common Beginner Mistakes
- Forgetting the braces when multiple statements belong to a block.
- Using = when a comparison is required.
- Writing an else without a corresponding if.
- Assuming both the if and else blocks will execute.
- Using == to compare String content.
Best Practices
- Keep the condition easy to read.
- Use braces consistently.
- Use meaningful variable names.
- Keep each decision focused on one clear business rule.
- Avoid deeply nested conditions when a simpler structure can express the same logic.
Interview Insight
A common interview question is: “Can an if statement exist without an else?” Yes. The else part is optional. However, an else cannot exist independently; it must belong to an if statement.
Quick Revision
| Concept | Key Point |
|---|---|
| if | Executes a block when the condition is true. |
| else | Executes a block when the if condition is false. |
| if-else | Provides two possible execution paths. |
| Condition true | The if block executes. |
| Condition false | The else block executes. |
The if-else statement is one of the most important decision-making structures in Java because it allows a program to respond differently to two possible situations. Once this pattern becomes comfortable, handling three or more possible conditions becomes the natural next step with the else-if ladder.
