Control Flow Statements in Java
Learn how Java controls the order in which statements are executed using conditional and decision-making statements.
What is Control Flow?
By default, Java executes statements from top to bottom. Control flow statements allow a program to make decisions, choose between alternatives, and execute different blocks depending on conditions.
Statement 1
↓
Condition
↓
Choose a path
↓
Statement 2
Types of Control Flow Statements
| Statement | Purpose |
|---|---|
if |
Executes code when a condition is true. |
if-else |
Chooses between two alternatives. |
else-if |
Checks multiple conditions. |
switch |
Chooses among multiple discrete cases. |
| Nested Conditions | Places one decision inside another. |
| Ternary Operator | Provides a compact conditional expression. |
1. if Statement
The if statement executes a block of code only
when its condition evaluates to true.
if (condition) {
// statements
}
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
2. if-else Statement
Use if-else when the program needs to choose
between two alternatives.
int age = 16;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
3. else-if Ladder
An else-if ladder is useful when multiple
mutually exclusive conditions need to be checked.
int marks = 82;
if (marks >= 90) {
System.out.println("A+");
} else if (marks >= 80) {
System.out.println("A");
} else if (marks >= 70) {
System.out.println("B");
} else if (marks >= 60) {
System.out.println("C");
} else {
System.out.println("Fail");
}
4. Nested if
A nested if statement is an if
statement placed inside another conditional block.
int age = 25;
boolean hasId = true;
if (age >= 18) {
if (hasId) {
System.out.println("Entry allowed");
}
}
5. switch Statement
The switch statement is useful when one value
needs to be compared against several possible case labels.
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
break
prevents execution from falling through into the next
case.
Multiple Case Labels
Multiple case labels can execute the same block.
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}
switch with String
Java supports switching on String values.
String role = "admin";
switch (role) {
case "admin":
System.out.println("CIIT Account 👨🏫 Full access");
break;
case "user":
System.out.println("CIIT Account 👨🏫 Limited access");
break;
default:
System.out.println("Unknown role ?..");
}
switch with enum
Enumerations are a natural fit for switch statements because the possible values are known.
enum Status {
ACTIVE,
INACTIVE,
PENDING
}
Status status = Status.ACTIVE;
switch (status) {
case ACTIVE:
System.out.println("CIIT Account 👨🏫 is active");
break;
case INACTIVE:
System.out.println("CIIT Account 👨🏫 is inactive");
break;
case PENDING:
System.out.println("CIIT Account 👨🏫 is pending");
break;
}
Modern switch Expression
Modern Java supports switch expressions that can directly produce a value.
int day = 2;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Other";
};
System.out.println(name);
6. Ternary Operator
The ternary operator provides a concise way to choose between two values.
int age = 21;
String result =
age >= 18 ? "Eligible" : "Not Eligible";
System.out.println(result);
General syntax:
condition ? valueIfTrue : valueIfFalse
Combining Conditions
Logical operators can be used to combine multiple conditions.
int age = 25;
boolean hasId = true;
if (age >= 18 && hasId) {
System.out.println("Access granted");
}
Checking for null
Reference variables can contain null.
Check for null before calling methods on a potentially
null reference.
String name = null;
if (name != null) {
System.out.println(name.length());
} else {
System.out.println("Name is not available");
}
Complete Example
public class Main {
public static void main(String[] args) {
int marks = 76;
String grade;
if (marks >= 90) {
grade = "A+";
} else if (marks >= 75) {
grade = "A";
} else if (marks >= 60) {
grade = "B";
} else if (marks >= 40) {
grade = "C";
} else {
grade = "Fail";
}
System.out.println("Grade: " + grade);
}
}
Best Practices
- Keep conditions simple and readable.
- Use braces even when a block contains only one statement.
- Prefer switch when comparing one expression against multiple discrete values.
- Avoid deeply nested conditional structures.
- Use meaningful boolean expressions.
- Use modern switch expressions when they make the code clearer and the Java version supports them.
Summary
Control flow statements allow Java programs to make
decisions and execute different blocks of code based on
conditions. The most important constructs are
if, if-else,
else-if, switch, and the
ternary operator.