Java If…Else
Java if...else statements let a program choose between different blocks of code. They run code only when a boolean condition is true.
This is the start of control flow: instead of running every statement from top to bottom, your program can make decisions based on values such as scores, ages, prices, or user input.
The if Statement
An if statement checks one condition. If the condition is true, Java runs the code inside the braces. If the condition is false, Java skips that block.
public class Main {
public static void main(String[] args) {
int score = 85;
if (score >= 70) {
System.out.println("You passed.");
}
System.out.println("Program finished.");
}
}
Output:
You passed.
Program finished.
How It Works
The condition score >= 70 is a boolean expression. Because 85 is greater than or equal to 70, the condition is true, so Java prints You passed.. The final print statement is outside the if block, so it always runs.
The condition must be inside parentheses, and the code controlled by the condition goes inside braces. For beginner code, always use braces, even when the block has only one statement. Braces make the decision clear and help prevent mistakes later.
Adding else
An else block runs when the if condition is false. Use it when your program needs one action for true and another action for false.
public class Main {
public static void main(String[] args) {
int temperature = 45;
if (temperature >= 60) {
System.out.println("Wear a light jacket.");
} else {
System.out.println("Wear a warm coat.");
}
}
}
Output:
Wear a warm coat.
Here, temperature >= 60 is false because 45 is less than 60. Java skips the first block and runs the else block instead.
Checking Multiple Conditions
Use else if when you need to test more than two possible paths. Java checks the conditions from top to bottom and runs the first block whose condition is true. After one block runs, the rest of the chain is skipped.
public class Main {
public static void main(String[] args) {
int score = 82;
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: Needs practice");
}
}
}
Output:
Grade: B
The score is not at least 90, so Java moves to the next condition. The condition score >= 80 is true, so Java prints Grade: B and skips the remaining branches.
Using Boolean Logic In Conditions
An if condition can use logical operators. Use && when both conditions must be true, || when at least one condition must be true, and ! to reverse a boolean value.
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasTicket = true;
if (age >= 18 && hasTicket) {
System.out.println("Entry allowed.");
} else {
System.out.println("Entry denied.");
}
}
}
Output:
Entry allowed.
Both parts of the condition are true: the age is at least 18, and hasTicket is true. Because the expression uses &&, both parts must be true for the first block to run.
Common Mistakes
- Using
=instead of==when comparing values. A single=assigns a value. - Forgetting parentheses around the condition.
- Putting a semicolon right after the
ifcondition, which ends the statement too early. - Writing broad conditions before specific ones in an
else ifchain. Java runs only the first matching branch.
Takeaway: if...else statements let Java programs make decisions by running different code for different boolean conditions.
