Working with Control Flow Statements
1. Using if-else Statements
| Form | Syntax |
|---|---|
| Simple | if (cond) { ... } |
| if-else | if (cond) { ... } else { ... } |
| if-else if | if (a) {} else if (b) {} else {} |
2. Using Nested if Statements
Example: Nested if
if (user != null) {
if (user.isAdmin()) {
if (user.hasPermission("DELETE")) {
performDelete();
}
}
}
| Tip | Reason |
|---|---|
| Use guard clauses | Reduces nesting depth |
Combine with && | Replaces shallow nesting |
3. Using switch Statement (traditional)
Example: Traditional switch
switch (day) {
case MONDAY:
case TUESDAY:
System.out.println("Workday");
break;
case SATURDAY:
System.out.println("Weekend");
break;
default:
System.out.println("Other");
}
| Supported Type | Notes |
|---|---|
| Integral | byte short int char + wrappers |
String | Java 7+ |
enum | Use unqualified constant names |
| Sealed types | Pattern matching (Java 21+) |
4. Using Switch Expressions JAVA 14+
Example: Switch expression with arrow
String type = switch (day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Workday";
case SATURDAY, SUNDAY -> "Weekend";
};
int len = switch (obj) {
case String s -> s.length();
case Integer i -> i;
default -> 0;
};
| Feature | Detail |
|---|---|
| Arrow syntax | No fall-through, single expression |
yield keyword | Return value from block: { ... yield x; } |
| Exhaustive | Required for sealed/enum (no default needed if all cases covered) |
5. Understanding Pattern Matching in switch JAVA 21+
Example: Type and record patterns
sealed interface Shape permits Circle, Square, Triangle {}
record Circle(double r) implements Shape {}
record Square(double s) implements Shape {}
record Triangle(double b, double h) implements Shape {}
double area = switch (shape) {
case Circle(double r) -> Math.PI * r * r;
case Square(double s) -> s * s;
case Triangle(double b, double h) -> 0.5 * b * h;
};
| Pattern | Syntax |
|---|---|
| Type | case String s -> ... |
| Guarded | case Integer i when i > 0 -> ... |
| Record | case Point(int x, int y) -> ... |
| Null | case null -> ... |
6. Using for Loop
| Form | Syntax |
|---|---|
| Classic | for (int i=0; i<n; i++) {} |
| Enhanced | for (var s : collection) {} |
| Multi-init | for (int i=0, j=n; i<j; i++, j--) {} |
| Infinite | for (;;) {} |
7. Using while Loop
| Aspect | Detail |
|---|---|
| Condition | Checked before each iteration |
| Zero iterations | Possible if condition false initially |
8. Using do-while Loop
| Aspect | Detail |
|---|---|
| Execution | Body runs at least once |
| Termination | Semicolon required after while(...) |
9. Using break Statement
| Use | Behavior |
|---|---|
Plain break | Exits innermost loop/switch |
Labeled break label | Exits labeled outer loop |
Example: Labeled break
outer:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == target) break outer;
}
}
10. Using continue Statement
| Use | Behavior |
|---|---|
Plain continue | Skip rest of current iteration |
Labeled continue label | Continue outer loop |
11. Using return Statement
| Form | Use |
|---|---|
return; | void methods |
return value; | typed methods |
In finally | Overrides any prior return — avoid |
12. Using assert Statement
| Form | Throws |
|---|---|
assert expr; | AssertionError if false |
assert expr : message; | With detail message |
| Enable | JVM flag -ea (disabled by default) |
Warning: Don't use assertions for argument validation in public APIs — use explicit exceptions instead.