π Control Statements in Java: if, if-else and Nested if
π Introduction
Control statements in Java are used to control the flow of execution of a program. By default, Java executes statements sequentially, but real-world programs require decision-making based on conditions.
The if, if-else, and nested if statements allow a program to choose different execution paths depending on whether a condition is true or false.
π§ What Are Control Statements?
π Definition
Control statements are statements that alter the normal flow of execution based on conditions or decisions.
Java control statements are broadly classified into:
- Decision-making statements
- Looping statements
- Branching statements
π In this article, we focus on decision-making statements.
β
The if Statement in Java
πΉ Syntax of if Statement
if (condition) {
// statements to execute if condition is true
}
πΉ How if Works
- The condition must be of boolean type
- If the condition is
true, the block executes - If
false, the block is skipped
π§ͺ Example: Simple if
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
}
β Output:
Eligible to vote
β οΈ Important Rule
The condition inside if must be boolean.
β Invalid:
if (age) // compilation error
β Correct:
if (age > 0)
β Common Mistakes with if
β Mistake 1: Using Assignment Instead of Comparison
if (x = 5) // ERROR
π§ = is assignment, not comparison.
β Correct:
if (x == 5)
β Mistake 2: Semicolon After if
if (x > 0); {
System.out.println("Positive");
}
β οΈ This block executes always, regardless of condition.
π The if-else Statement in Java
πΉ Syntax of if-else
if (condition) {
// executes if condition is true
} else {
// executes if condition is false
}
π§ͺ Example: if-else
int number = 7;
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
β Output:
Odd number
π§ Flow Explanation
- Condition checked
- One and only one block executes
- Either
iforelse, never both
β οΈ Tricky Example: Braces Matter
int x = 10;
if (x > 5)
System.out.println("Greater than 5");
System.out.println("End");
β Output:
Greater than 5
End
π§ Only the first statement belongs to if.
β Best practice:
if (x > 5) {
System.out.println("Greater than 5");
}
π Nested if Statement in Java
πΉ What Is Nested if?
An if inside another if is called a nested if.
It is used when decisions depend on multiple conditions.
πΉ Syntax
if (condition1) {
if (condition2) {
// statements
}
}
π§ͺ Example: Nested if
int marks = 85;
if (marks >= 35) {
if (marks >= 75) {
System.out.println("Distinction");
}
}
β Output:
Distinction
π§ Explanation
- First condition checks pass/fail
- Second condition checks grade
- Inner
ifexecutes only if outerifis true
β οΈ The βDangling elseβ Problem (VERY IMPORTANT)
π₯ Confusing Code
int x = 10;
if (x > 5)
if (x > 8)
System.out.println("A");
else
System.out.println("B");
β Output:
A
π§ else is associated with the nearest if, not the first one.
β Correct Version (Using Braces)
if (x > 5) {
if (x > 8) {
System.out.println("A");
} else {
System.out.println("B");
}
}
β οΈ Common Compilation Errors
β Using Non-Boolean Conditions
if (5) // ERROR
β Missing Parentheses
if x > 5 // ERROR
β Correct:
if (x > 5)
β Missing Braces in Nested Logic
Leads to logical errors, not compilation errors.
π§ Best Practices for Control Statements
β Always Use Braces
Even for single statements.
β Keep Conditions Simple
Complex conditions reduce readability.
β Avoid Deep Nesting
Too many nested if statements make code hard to maintain.
π Real-World Examples
- Login validation
- Exam grading systems
- Eligibility checks
- Decision-based workflows
Example:
if (username.equals("admin")) {
if (password.equals("1234")) {
System.out.println("Login successful");
}
}
π Quick Summary
ifβ executes block when condition is trueif-elseβ chooses between two paths- Nested
ifβ handles multiple dependent conditions - Conditions must be boolean
- Braces prevent logical errors
π Conclusion
Control statements form the foundation of decision-making in Java programs. Mastering if, if-else, and nested if helps in writing logical, readable, and error-free programs. Understanding common mistakes and tricky cases ensures better debugging skills and confidence in problem-solving.

