Control Flow
Conditional Control Statements
Complete lesson for Conditional Control Statements using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.
Basics — Conditional Control Statements
Section: Control Flow. if, else-if, nested decisions, classic switch, and Java 14+ switch expressions.
if, else if, else
- The condition must be
boolean— you cannot writeif (x)for anintlike C/C++. - Braces are optional for one statement but you should always use them to avoid dangling-else bugs.
public class GradeIf {
public static void main(String[] args) {
int marks = 76;
if (marks >= 90) {
System.out.println("A");
} else if (marks >= 75) {
System.out.println("B");
} else {
System.out.println("C or below");
}
}
} Classic switch (statement)
- Supported selector types:
byte,short,char,intand their wrappers,String(Java 7+), and enums. long,float,double, andbooleancannot be switch selectors.- Without
break, execution falls through to the next case — usually a bug, sometimes intentional.
Switch expressions (Java 14+)
- Arrow cases (
case 1 ->) do not fall through. A switch can produce a value. yieldis used in a block case to produce the value.defaultis required when the switch is used as an expression (unless the enum is exhaustive).
| Style | Fall-through | Produces a value? |
|---|---|---|
switch statement + break | Yes, if you omit break | No (side effects only) |
switch expression + -> | No | Yes |
switch expression + {} / yield | No | Yes |
Advanced — Conditional Control Statements
Nested decisions and pattern matching
- Deeply nested
ifis hard to test — extract methods or use a switch/table. - Java 16+
if (obj instanceof String s)bindssif the test succeeds (pattern matching for instanceof). - Java 17 preview/21+ expands switch pattern matching; interviews for Java 17 still focus on classic + arrow switch.
Tricky Interview Points
Q1) Can you write if (1) in Java?
Answer No. The condition must be boolean. if (1) is a C/C++ habit and does not compile.
Q2) Which types work in a switch?
Answer byte, short, char, int (and wrappers), String, and enum. Not long, float, double, or boolean.
Q3) What is fall-through?
Answer In a classic switch, after a matching case, execution continues into later cases until break, return, or the end.
Q4) What did Java 14 add for switch?
Answer Switch expressions: arrow cases without fall-through, and the ability to return a value (yield in block cases).
Q5) Is default mandatory?
Answer For a switch statement, no. For a switch expression, yes — unless the compiler can prove every enum constant is covered.
Q6) Can two cases share a body?
Answer Yes. Classic: stacked case labels. Arrow switch: case 1, 2, 3 -> ...
Simple Programs
Switch expression (Java 14+)
public class DayType {
public static void main(String[] args) {
String day = "SAT";
String kind = switch (day) {
case "SAT", "SUN" -> "weekend";
case "MON", "TUE", "WED", "THU", "FRI" -> "weekday";
default -> "unknown";
};
System.out.println(kind);
}
} Classic switch with break
public class ClassicSwitch {
public static void main(String[] args) {
int n = 2;
switch (n) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
default:
System.out.println("other");
}
}
} Problem Solving Tricks
- Always use braces on
if/else. - Prefer arrow
switchwhen you only need a value — no forgottenbreak. - Switch on enums instead of magic strings when the set is closed.
- Put the most common / cheapest condition first in an
else ifchain only if it stays readable.
Practice Programs List
- Map marks 0–100 to letter grades with
else if. - Rewrite that mapping as a switch expression over
marks / 10. - Write a classic switch that *intentionally* falls through (e.g. days in a month) and comment why.
- Reject
if (x = 5)vsif (x == 5)and explain the compile error for assignment in a boolean context. - Use
instanceofpattern matching to print the length if anObjectis aString.