If and If-Else Statements in Java
The if statement is the most frequently used conditional statement in Java — it is hard to write a program without it. It directs program execution down one of two (or more) branches depending on whether a condition is true or false.
1. If Statement in Java
The general syntax of the if statement:
if (booleanExpression) {
System.out.println("In the if block");
} Here booleanExpression must evaluate to a value of type boolean or Boolean. If it evaluates to true, the code inside the curly braces executes. If it evaluates to false, the block is skipped and execution continues after the closing brace.
The following example demonstrates the if statement:
public class IfExample {
public static void main(String[] args) {
int a = 1;
if (a < 10) {
System.out.println("In the if block");
}
}
} 2. If-Else Statement
The if statement can include an else block. When booleanExpression evaluates to false, the code after else executes instead:
if (booleanExpression) {
System.out.println("Inside if block");
} else {
System.out.println("Inside else block");
} The following example shows the if-else statement in action:
public class IfElseExample {
public static void main(String[] args) {
int a = 1;
if (a == 10) {
System.out.println("In the if block");
} else {
System.out.println("In the else block");
}
}
} The block that follows if or else may contain several statements.
Important
Curly braces are optional when a branch contains exactly one statement — the code still compiles. However, the Oracle Code Conventions recommend always using braces: it prevents subtle bugs when a second line is added to the branch later.
3. If-Else-If Ladder
The if-else-if ladder is a special case of the if-else structure. An if statement may have zero or more else if branches, which must come before the optional final else. Conditions are tested from top to bottom; as soon as one of them is true, its block runs and none of the remaining else if or else branches are evaluated.
Consider an example:
public class IfElseExample1 {
public static void main(String[] args) {
int month = 4; // April
String season;
if (month == 12 || month == 1 || month == 2) {
season = "Winter";
} else if (month == 3 || month == 4 || month == 5) {
season = "Spring";
} else if (month == 6 || month == 7 || month == 8) {
season = "Summer";
} else if (month == 9 || month == 10 || month == 11) {
season = "Autumn";
} else {
season = "Not a Month";
}
System.out.println("April is in the " + season + ".");
}
} Output:
April is in the Spring. Which Form to Choose
| Form | Structure | When to use |
|---|---|---|
if | One condition, one block | Run code only when a condition is true; do nothing otherwise |
if-else | One condition, two branches | Choose between exactly two alternatives |
if-else-if | Several conditions checked top to bottom | Pick one branch out of many based on different conditions |
switch | One value compared against constants | Many branches that all compare the same variable to constant values |
Where Beginners Slip Up
- A semicolon right after the condition:
if (a > 5);compiles, but the semicolon is an empty statement — the block below it runs unconditionally. - Skipping braces on single-line branches, then adding a second line that silently ends up outside the branch. Always use
{}. - Comparing objects with
==: forStringand other objects useequals()inside conditions;==compares references.
By mastering if, if-else, and the if-else-if ladder, you gain full control over the flow of your Java programs.
Frequently Asked Questions
Is if a loop in Java?
No. Many beginners say "if loop", but if is a conditional (decision-making) statement: its block executes at most once. Loops — for, while, do-while — repeat a block multiple times. An if never repeats anything.
What is the difference between if-else-if and switch in Java?
An if-else-if ladder can test any boolean conditions, including ranges like x > 10 and calls such as s.isEmpty(). A switch compares one value against constant labels and is usually cleaner and easier to read when every branch checks the same variable for equality.
Can I write an if statement without curly braces?
Yes — if the branch contains exactly one statement, the braces may be omitted and the code compiles. Oracle's code conventions still recommend braces in all cases, because adding a second line to a braceless branch is a classic source of bugs.
Why doesn't if (x = 5) compile in Java?
Because = is assignment, not comparison. The expression x = 5 has type int, and an if condition must be boolean, so the compiler rejects it. Watch out with booleans though: if (flag = true) does compile — it assigns true and the condition always passes. Use == for comparison, or simply if (flag).
Comments