Control Flow I — if, else, switch
শর্ত — if, else, switch (Java 14+ expression)
1. Branches — Making Decisions
Programs decide. if/else lets the program take different paths based
on a boolean condition. switch dispatches on a single value against many cases.
Modern Java (14+) turned switch into a first-class expression — so it
returns a value and eliminates fall-through bugs.
if/else boolean-এর ভিত্তিতে path বাছে। switch একটি মানকে অনেকগুলো case-এর সাথে মেলায়। Java 14+ থেকে switch expression হয়ে গেছে — value return করে এবং পুরনো fall-through সমস্যা দূর করে।
2. if / else if / else
class Main {
public static void main(String[] args) {
double cgpa = 3.72;
String honor;
if (cgpa >= 3.90) honor = "Summa Cum Laude";
else if (cgpa >= 3.70) honor = "Magna Cum Laude";
else if (cgpa >= 3.50) honor = "Cum Laude";
else if (cgpa >= 2.00) honor = "Pass";
else honor = "Fail";
System.out.println("CGPA " + cgpa + " → " + honor);
}
}
if শর্ত সত্যি হলে block চলে। শর্ত false হলে পরের else if, তারপর else। শুধু প্রথম match-ই চলবে। Condition parens-এ boolean expression দিতে হবে (Java-তে if (x) — x অবশ্যই boolean)।
3. The Ternary Operator ? :
class Main {
public static void main(String[] args) {
int temp = 33;
String advice = (temp >= 30) ? "Hot — drink water" : "Nice and cool";
System.out.println(advice);
int n = -5;
int abs = n < 0 ? -n : n;
System.out.println("|n| = " + abs);
}
}
condition ? a : b — condition সত্যি হলে a, না হলে b। সরল এক-লাইনের পছন্দের জন্য দুর্দান্ত; nested ternary এড়িয়ে চলুন, পড়তে কঠিন হয়।
4. Classic switch — and the Fall-Through Trap
The old C-style switch falls through from one case to the next unless you add
break. A forgotten break is a classic bug.
class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1: System.out.println("Saturday"); break;
case 2: System.out.println("Sunday"); break;
case 3: System.out.println("Monday"); break;
case 4: System.out.println("Tuesday"); break;
default: System.out.println("Other");
}
}
}
switch-এ প্রতিটি case-এর শেষে break না দিলে পরের case-গুলোও execute হয়ে যাবে (fall-through)। ভুলে গেলে গোপন bug জন্মায়। Java 14+ এর নতুন switch এই সমস্যা সমাধান করে।
5. Modern switch Expression (Java 14+)
The arrow form case X -> value; has no fall-through, can group labels with
commas, and can return a value straight into a variable.
class Main {
public static void main(String[] args) {
int day = 6;
String kind = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Invalid";
};
System.out.println(day + " → " + kind);
// When you need several statements, use a block with 'yield'
int score = 72;
String grade = switch (score / 10) {
case 10, 9 -> "A";
case 8 -> "B";
case 7 -> "C";
case 6 -> "D";
default -> {
System.out.println("Computing fail grade...");
yield "F";
}
};
System.out.println("grade = " + grade);
}
}
switch expression (->) — fall-through নেই, একাধিক label একসাথে (case 1,2,3 ->), এবং সরাসরি value return করে। একাধিক statement লাগলে block-এর ভেতর yield ব্যবহার করুন।
6. Pattern Matching in switch (Java 21)
Starting in Java 21, switch can match on types — a powerful way to handle
heterogeneous inputs.
class Main {
static String describe(Object o) {
return switch (o) {
case Integer i when i < 0 -> "negative int " + i;
case Integer i -> "int " + i;
case String s -> "string of length " + s.length();
case null -> "nothing";
default -> "something else";
};
}
public static void main(String[] args) {
System.out.println(describe(42));
System.out.println(describe(-3));
System.out.println(describe("Dhaka"));
System.out.println(describe(null));
}
}
switch type-এর উপর match করতে পারে (pattern matching)। case Integer i when i < 0 -> ... মানে Integer type এবং শর্ত দুই-ই মানানসই হলে। null-ও এখন case হিসেবে সরাসরি লেখা যায়।
7. Common Bugs in Branches
❌ Mistakes
if (x = 5)— assignment by mistake. Java refuses non-boolean; in other languages this silently compiles.if (x == null && x.foo())— wrong order; must be!= null.- Forgetting
breakin classic switch. - Comparing strings with
==instead of.equals(). - Overly nested
if— refactor to switch or early return.
✅ Good habits
- Prefer the modern
switchexpression. - Use early returns to flatten nested
ifs. - Put null checks first with
&&. - Always include a
default. - Keep ternary simple — one condition per expression.
8. Vocabulary
| Term | Meaning | বাংলায় |
|---|---|---|
| Branch | A code path taken when a condition matches. | শর্ত অনুযায়ী path। |
| Ternary | cond ? a : b — 3-operand conditional expression. | এক-লাইনের if/else। |
| Fall-through | Classic switch cases execute in sequence without break. | পুরনো switch-এ break না দিলে পরের case চলে। |
| Switch expression | Java 14+ arrow-form switch that returns a value. | Value return করা আধুনিক switch। |
| yield | Return a value from a block inside a switch expression. | switch expression-এর block থেকে value return। |
| Pattern match | Matching case by type (Java 21). | type অনুযায়ী case match। |
9. Practice Problems
-
Given an age, print Child (<13), Teen (13-19), Adult (20-59), Senior (60+).age দিয়ে Child, Teen, Adult, Senior print করুন।
✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { public static void main(String[] args) { int[] ages = { 7, 15, 28, 70 }; for (int a : ages) { String label; if (a < 13) label = "Child"; else if (a < 20) label = "Teen"; else if (a < 60) label = "Adult"; else label = "Senior"; System.out.println(a + " → " + label); } } } -
Using a modern switch expression, map a day number (1-7) to a Bangla day name.switch expression দিয়ে 1-7 → বাংলা বার-এর নাম।
✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { public static void main(String[] args) { for (int d = 1; d <= 7; d++) { String bn = switch (d) { case 1 -> "শনিবার"; case 2 -> "রবিবার"; case 3 -> "সোমবার"; case 4 -> "মঙ্গলবার"; case 5 -> "বুধবার"; case 6 -> "বৃহস্পতিবার"; case 7 -> "শুক্রবার"; default -> "?"; }; System.out.println(d + " → " + bn); } } } -
Rewrite an
if/elsethat returns "even" or "odd" as a single ternary expression."even/odd" ternary দিয়ে লিখুন।✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { public static void main(String[] args) { for (int n : new int[]{1,2,3,4}) { System.out.println(n + " : " + (n % 2 == 0 ? "even" : "odd")); } } } -
Explain what "fall-through" means and give one case where it is intentional.fall-through কী এবং কখন এটি ইচ্ছাকৃতভাবে কাজে আসে?
✨ Show Answer (উত্তর দেখুন)
Answer: In a classic
switch, if you omitbreak, execution continues into the next case — that is fall-through. It is occasionally intentional when several adjacent cases share the same body (e.g.,case 1: case 2: case 3: handleSmall(); break;). In modern switch expressions with arrows, fall-through is impossible; you group labels with a comma instead (case 1, 2, 3 -> handleSmall();). -
Write a helper that maps an HTTP status code (200, 301, 404, 500, anything else) to a description using a switch expression.HTTP status code → description — switch expression দিয়ে।
✨ Show Answer (উত্তর দেখুন)
Main.javaclass Main { static String describe(int code) { return switch (code) { case 200 -> "OK"; case 301 -> "Moved Permanently"; case 404 -> "Not Found"; case 500 -> "Server Error"; default -> "Unknown (" + code + ")"; }; } public static void main(String[] args) { for (int c : new int[]{200, 301, 404, 500, 418}) System.out.println(c + " → " + describe(c)); } }
Summary — Module 09
if / else if / else branches on booleans. The ternary ? : is a
handy one-liner. Classic switch falls through unless you add break.
The modern switch expression (Java 14+) removes fall-through, allows label
groups with commas, returns a value, and supports pattern matching on types from Java 21 —
prefer it in new code.
if/else, ternary এবং পুরনো switch-এর fall-through — সব জানুন, কিন্তু নতুন কোডে Java 14+ এর arrow-switch ব্যবহার করুন। Java 21-এ type-ভিত্তিক pattern match।