Programming and Java Fundamentals
Switch Expressions and Statements
আপনি একটি free preview lesson দেখছেন।
Lesson Overview
একটি program-এ অনেক সময় একটি value-এর ওপর ভিত্তি করে একাধিক possible action-এর মধ্যে একটি select করতে হয়।
উদাহরণ:
- Day number থেকে day name বের করা
- Menu option অনুযায়ী operation চালানো
- User role অনুযায়ী dashboard নির্ধারণ করা
- Course level অনুযায়ী description দেখানো
- Status অনুযায়ী message তৈরি করা
এই ধরনের logic if-else দিয়ে লেখা যায়। তবে একটি value-এর exact match অনুযায়ী multiple branch select করতে হলে switch অনেক সময় বেশি readable হয়।
এই lesson-এ আমরা শিখব:
switchকী- Traditional
switchstatement casedefaultbreak- Fall-through
- Multiple case labels
- Arrow-style
switch switchexpressionyield- String এবং enum value নিয়ে
switch if-elseএবংswitchনির্বাচন- Common
switch-related mistakes
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
switchstatement-এর purpose ব্যাখ্যা করতেcaseএবংdefaultbranch লিখতেbreakব্যবহার করতে- Fall-through বুঝতে
- Multiple case একসঙ্গে group করতে
- Modern arrow-style
switchলিখতে switchexpression থেকে value return করতে- Block case-এর মধ্যে
yieldব্যবহার করতে - String এবং enum value switch করতে
if-elseওswitch-এর মধ্যে উপযুক্তটি নির্বাচন করতে
What Is a Switch?
switch একটি control-flow structure, যা একটি value compare করে matching branch execute করে।
Example:
int dayNumber = 2;
switch (dayNumber) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
Output:
Tuesday
Basic Switch Syntax
switch (value) {
case firstValue:
// Statements
break;
case secondValue:
// Statements
break;
default:
// Statements
}
এখানে:
switchvalue evaluate করেcasepossible matching value define করেbreakswitch থেকে বের করেdefaultকোনো case match না করলে execute হয়
How Switch Works
switch (value) {
case 1:
// Branch 1
break;
case 2:
// Branch 2
break;
default:
// Default branch
}
Execution:
valueevaluate হয়- Java matching
caseখোঁজে - Matching case-এর statement execute হয়
breakপাওয়া গেলে switch শেষ হয়- কোনো case match না করলে
defaultexecute হয়
Traditional Switch Statement
int menuOption = 2;
switch (menuOption) {
case 1:
System.out.println("Create course");
break;
case 2:
System.out.println("View courses");
break;
case 3:
System.out.println("Exit");
break;
default:
System.out.println("Invalid option");
}
Output:
View courses
The case Label
প্রতিটি case একটি possible exact value represent করে।
case 1:
case 2:
case 3:
Switch value case value-এর সঙ্গে match করলে সেই branch execute হয়।
The default Branch
কোনো case match না করলে default execute হয়।
int option = 9;
switch (option) {
case 1:
System.out.println("Start");
break;
case 2:
System.out.println("Stop");
break;
default:
System.out.println("Unknown option");
}
Output:
Unknown option
default technically optional, তবে unexpected input handle করতে এটি useful।
The break Statement
Traditional switch-এ break current switch block থেকে execution বের করে।
case 1:
System.out.println("Monday");
break;
break না থাকলে পরবর্তী case-এর statement-ও execute হতে পারে।
এটিকে fall-through বলা হয়।
Fall-Through
Example:
int option = 1;
switch (option) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Done");
}
Output:
One
Two
Three
Done
কারণ কোনো break নেই।
Execution matching case 1 থেকে শুরু হয়ে নিচের সব statement চালিয়েছে।
Accidental Fall-Through
Wrong:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
}
Output:
Tuesday
Wednesday
Expected যদি শুধু Tuesday হয়, তাহলে এটি bug।
Correct:
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
}
Intentional Fall-Through
কখনো multiple case একই behaviour share করতে পারে।
Traditional style:
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}
case 6-এ statement নেই, তাই execution case 7-এ যায়।
Output:
Weekend
Multiple Cases for the Same Action
Traditional syntax:
char grade = 'A';
switch (grade) {
case 'A':
case 'B':
System.out.println("Good result");
break;
case 'C':
case 'D':
System.out.println("Average result");
break;
default:
System.out.println("Needs improvement");
}
Modern Arrow-Style Switch
Modern Java-তে arrow-style syntax ব্যবহার করা যায়।
int day = 2;
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> System.out.println("Wednesday");
default -> System.out.println("Invalid day");
}
Output:
Tuesday
Arrow-style switch-এ automatic fall-through হয় না।
break প্রয়োজন হয় না।
Arrow-Style Multiple Case Labels
int day = 6;
switch (day) {
case 1, 2, 3, 4, 5 ->
System.out.println("Weekday");
case 6, 7 ->
System.out.println("Weekend");
default ->
System.out.println("Invalid day");
}
Output:
Weekend
এটি traditional fall-through grouping-এর তুলনায় বেশি readable।
Multiple Statements in an Arrow Case
একটি case-এ multiple statement থাকলে block ব্যবহার করুন।
int option = 1;
switch (option) {
case 1 -> {
System.out.println("Creating course");
System.out.println("Opening course form");
}
case 2 -> {
System.out.println("Loading courses");
System.out.println("Displaying course list");
}
default -> {
System.out.println("Invalid option");
}
}
Arrow block-এর শেষে break প্রয়োজন নেই।
Switch Statement vs Switch Expression
Traditional switch statement action execute করে।
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
default:
dayName = "Invalid";
}
Modern switch expression সরাসরি একটি value produce করতে পারে।
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Invalid";
};
Switch Expression
int day = 3;
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println(dayName);
Output:
Wednesday
Semicolon After a Switch Expression
Switch expression একটি value return করে এবং assignment statement-এর অংশ।
তাই শেষে semicolon প্রয়োজন:
String result = switch (option) {
case 1 -> "Create";
case 2 -> "Update";
default -> "Unknown";
};
শেষের:
;
ভুলে গেলে compilation error হবে।
Switch Expression Must Produce a Value
String result = switch (option) {
case 1 -> "Create";
case 2 -> "Update";
default -> "Unknown";
};
প্রতিটি possible branch একটি compatible value দেয়।
Why default Is Important in Switch Expressions
Example:
int option = 1;
String result = switch (option) {
case 1 -> "Create";
case 2 -> "Update";
};
Compiler বলতে পারে switch সব possible value cover করছে না।
int-এর অনেক possible value রয়েছে।
Correct:
String result = switch (option) {
case 1 -> "Create";
case 2 -> "Update";
default -> "Unknown";
};
Using yield
Arrow case-এর block থেকে switch expression-এর value return করতে yield ব্যবহার করা হয়।
int score = 85;
String result = switch (score / 10) {
case 10, 9, 8 -> {
System.out.println("High score");
yield "Excellent";
}
case 7, 6 -> {
System.out.println("Good score");
yield "Good";
}
default -> {
System.out.println("More practice needed");
yield "Needs improvement";
}
};
System.out.println(result);
Output:
High score
Excellent
yield Is Not return
yield current switch expression থেকে value দেয়।
yield "Excellent";
return পুরো method থেকে বের হয়ে যায়।
তাই switch expression block-এর মধ্যে value produce করতে yield ব্যবহার করা হয়।
Simple Arrow Case Does Not Need yield
String result = switch (option) {
case 1 -> "Create";
case 2 -> "Update";
default -> "Unknown";
};
এখানে expression সরাসরি arrow-এর পরে রয়েছে।
yield প্রয়োজন নেই।
Traditional Switch Expression with yield
Colon-style switch expression-এও yield ব্যবহার করা যায়।
String result = switch (option) {
case 1:
yield "Create";
case 2:
yield "Update";
default:
yield "Unknown";
};
তবে arrow-style সাধারণত বেশি concise এবং less error-prone।
Supported Switch Types
Commonly switch করা যায়:
byteshortcharint- Corresponding wrapper types
Stringenum
Example:
int option = 1;
char grade = 'A';
String role = "ADMIN";
Unsupported Basic Types
Traditional Java switch-এ সাধারণত নিচের types ব্যবহার করা যায় না:
longfloatdoubleboolean
Invalid:
long value = 10L;
switch (value) {
}
Invalid:
double price = 99.50;
switch (price) {
}
Range বা decimal comparison-এর জন্য if-else ব্যবহার করুন।
Switching on a String
String role = "INSTRUCTOR";
switch (role) {
case "ADMIN" ->
System.out.println("Admin dashboard");
case "INSTRUCTOR" ->
System.out.println("Instructor dashboard");
case "LEARNER" ->
System.out.println("Learner dashboard");
default ->
System.out.println("Unknown role");
}
Output:
Instructor dashboard
String matching case-sensitive।
INSTRUCTOR
এবং:
instructor
একই নয়।
Normalizing String Before Switch
String role = " instructor ";
String normalizedRole =
role
.strip()
.toUpperCase();
switch (normalizedRole) {
case "ADMIN" ->
System.out.println("Admin dashboard");
case "INSTRUCTOR" ->
System.out.println("Instructor dashboard");
case "LEARNER" ->
System.out.println("Learner dashboard");
default ->
System.out.println("Unknown role");
}
Input normalize করলে case এবং surrounding whitespace problem কমে।
Null and Switch
null value switch করলে NullPointerException হতে পারে, বিশেষ করে common class বা String switch-এ।
Risky:
String role = null;
switch (role) {
case "ADMIN":
System.out.println("Admin");
break;
default:
System.out.println("Unknown");
}
Safer:
if (role == null) {
System.out.println("Role is required");
} else {
switch (role) {
case "ADMIN" ->
System.out.println("Admin");
default ->
System.out.println("Unknown");
}
}
Switching on char
char grade = 'B';
String description = switch (grade) {
case 'A' -> "Excellent";
case 'B' -> "Very good";
case 'C' -> "Good";
case 'D' -> "Needs improvement";
case 'F' -> "Failed";
default -> "Unknown grade";
};
System.out.println(description);
Output:
Very good
Switching on Enum Values
enum predefined constant values represent করে।
Example:
enum CourseStatus {
DRAFT,
PUBLISHED,
ARCHIVED
}
Switch:
CourseStatus status =
CourseStatus.PUBLISHED;
String message = switch (status) {
case DRAFT ->
"Course is not visible";
case PUBLISHED ->
"Course is available";
case ARCHIVED ->
"Course is archived";
};
System.out.println(message);
Output:
Course is available
সব enum constants cover করলে default প্রয়োজন নাও হতে পারে।
Enum future module-এ বিস্তারিত শেখানো হবে।
Switch with Day Names
int dayNumber = 5;
String dayName = switch (dayNumber) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println(dayName);
Output:
Friday
Grouping Weekdays and Weekends
int dayNumber = 7;
String dayType = switch (dayNumber) {
case 1, 2, 3, 4, 5 ->
"Weekday";
case 6, 7 ->
"Weekend";
default ->
"Invalid day";
};
System.out.println(dayType);
Output:
Weekend
Menu Program with Switch
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.print(
"Choose an option: "
);
int option =
Integer.parseInt(
scanner
.nextLine()
.strip()
);
System.out.print(
"Enter the first number: "
);
double firstNumber =
Double.parseDouble(
scanner
.nextLine()
.strip()
);
System.out.print(
"Enter the second number: "
);
double secondNumber =
Double.parseDouble(
scanner
.nextLine()
.strip()
);
switch (option) {
case 1 ->
System.out.println(
"Result: "
+ (
firstNumber
+ secondNumber
)
);
case 2 ->
System.out.println(
"Result: "
+ (
firstNumber
- secondNumber
)
);
case 3 ->
System.out.println(
"Result: "
+ (
firstNumber
* secondNumber
)
);
case 4 -> {
if (secondNumber == 0) {
System.out.println(
"Cannot divide by zero"
);
} else {
System.out.println(
"Result: "
+ (
firstNumber
/ secondNumber
)
);
}
}
default ->
System.out.println(
"Invalid option"
);
}
scanner.close();
}
}
Calculator with Switch Expression
int option = 1;
double firstNumber = 10;
double secondNumber = 20;
double result = switch (option) {
case 1 ->
firstNumber + secondNumber;
case 2 ->
firstNumber - secondNumber;
case 3 ->
firstNumber * secondNumber;
case 4 -> {
if (secondNumber == 0) {
throw new IllegalArgumentException(
"Cannot divide by zero"
);
}
yield firstNumber / secondNumber;
}
default ->
throw new IllegalArgumentException(
"Invalid option"
);
};
System.out.println(result);
Exception handling future lesson-এ বিস্তারিত শেখানো হবে।
Course Level Example
String level = "BEGINNER";
String description = switch (level) {
case "BEGINNER" ->
"No previous experience required";
case "INTERMEDIATE" ->
"Basic programming knowledge required";
case "ADVANCED" ->
"Strong backend knowledge required";
default ->
"Unknown course level";
};
System.out.println(description);
HTTP Status Example
int statusCode = 404;
String message = switch (statusCode) {
case 200 -> "Success";
case 201 -> "Created";
case 400 -> "Bad request";
case 401 -> "Unauthorized";
case 403 -> "Forbidden";
case 404 -> "Not found";
case 500 -> "Internal server error";
default -> "Unknown status";
};
System.out.println(message);
Output:
Not found
Month and Number of Days
int month = 4;
int numberOfDays = switch (month) {
case 1, 3, 5, 7, 8, 10, 12 ->
31;
case 4, 6, 9, 11 ->
30;
case 2 ->
28;
default ->
0;
};
System.out.println(numberOfDays);
Leap year handling এখানে include করা হয়নি।
Switch vs if-else
দুটিই program flow control করে, কিন্তু use case আলাদা।
Use Switch When
একটি value-এর exact match অনুযায়ী branch select করতে হবে।
Example:
switch (role) {
case "ADMIN" -> ...
case "INSTRUCTOR" -> ...
case "LEARNER" -> ...
}
Suitable for:
- Menu option
- Status
- Role
- Enum
- Day number
- Command
- Fixed category
Use if-else When
Condition range, inequality বা multiple unrelated expression-এর ওপর depend করে।
Example:
if (mark >= 80) {
} else if (mark >= 70) {
}
Suitable for:
- Numeric ranges
- Greater than বা less than
- Multiple logical conditions
- Null checking
- Floating-point comparison
- Complex business rules
Switch Cannot Directly Express Ranges
Invalid concept:
switch (mark) {
case mark >= 80:
}
case exact constant value expect করে।
Range-এর জন্য:
if (mark >= 80) {
System.out.println("A");
} else if (mark >= 70) {
System.out.println("B");
}
Switch After Categorization
কখনো value আগে category-তে convert করে switch করা যায়।
int mark = 85;
String grade = switch (mark / 10) {
case 10, 9, 8 -> "A";
case 7 -> "B";
case 6 -> "C";
case 5 -> "D";
case 4 -> "E";
default -> "F";
};
System.out.println(grade);
তবে invalid mark আগে validate করতে হবে।
if (mark < 0 || mark > 100) {
System.out.println("Invalid mark");
} else {
String grade = switch (mark / 10) {
case 10, 9, 8 -> "A";
case 7 -> "B";
case 6 -> "C";
case 5 -> "D";
case 4 -> "E";
default -> "F";
};
System.out.println(grade);
}
Common Error: Missing break
Wrong:
switch (option) {
case 1:
System.out.println("Create");
case 2:
System.out.println("Update");
default:
System.out.println("Unknown");
}
Option 1 হলে তিনটি output আসতে পারে।
Correct:
switch (option) {
case 1:
System.out.println("Create");
break;
case 2:
System.out.println("Update");
break;
default:
System.out.println("Unknown");
}
অথবা arrow-style ব্যবহার করুন।
Common Error: Duplicate Cases
Invalid:
switch (option) {
case 1:
System.out.println("Create");
break;
case 1:
System.out.println("Update");
break;
}
একই switch-এ duplicate case value ব্যবহার করা যায় না।
Common Error: Non-Constant Case Value
Case label compile-time constant হতে হয়।
Wrong:
int firstOption = 1;
int option = 1;
switch (option) {
case firstOption:
System.out.println("Selected");
break;
}
Local non-final variable case label হিসেবে valid নাও হতে পারে।
Better:
static final int CREATE_OPTION = 1;
Then:
case CREATE_OPTION:
অথবা literal ব্যবহার করুন।
Common Error: Wrong Case Type
int option = 1;
switch (option) {
case "1":
System.out.println("One");
}
Switch value int, কিন্তু case value String।
Correct:
case 1:
Common Error: Forgetting the Default Case
switch (option) {
case 1 -> System.out.println("Create");
case 2 -> System.out.println("Update");
}
Option unexpected হলে কোনো output হবে না।
Action statement-এ এটি valid হতে পারে, তবে usually default handling useful।
default ->
System.out.println("Invalid option");
Common Error: Missing Semicolon After Expression
Wrong:
String result = switch (option) {
case 1 -> "Create";
default -> "Unknown";
}
Correct:
String result = switch (option) {
case 1 -> "Create";
default -> "Unknown";
};
Common Error: Missing yield
Wrong:
String result = switch (option) {
case 1 -> {
System.out.println("Creating");
"Create";
}
default -> "Unknown";
};
Block case-এ value return করতে yield প্রয়োজন।
Correct:
String result = switch (option) {
case 1 -> {
System.out.println("Creating");
yield "Create";
}
default -> "Unknown";
};
Common Error: Mixing Colon and Arrow Carelessly
Avoid inconsistent structure:
switch (option) {
case 1:
System.out.println("One");
break;
case 2 ->
System.out.println("Two");
}
কিছু Java syntax context-এ mixing disallowed বা confusing হতে পারে।
একটি switch block-এ consistent style ব্যবহার করুন।
Common Error: Switching on Null
String command = null;
switch (command) {
case "START" -> System.out.println("Starting");
default -> System.out.println("Unknown");
}
Runtime-এ error হতে পারে।
Switch-এর আগে null validate করুন।
Common Error: Case-Sensitive String Input
String command = "start";
switch (command) {
case "START" ->
System.out.println("Starting");
default ->
System.out.println("Unknown");
}
Output:
Unknown
Normalize:
String normalizedCommand =
command
.strip()
.toUpperCase();
Common Error: Using Switch for Complex Ranges
Hard and unclear categorization:
switch (mark / 10) {
// Many special cases
}
যদি rules irregular হয়, if-else বেশি readable।
Example:
if (mark >= 85) {
} else if (mark >= 72) {
} else if (mark >= 55) {
}
Practical Decision Guide
| Situation | Better Choice |
|---|---|
| Exact menu option | switch |
| Exact role or status | switch |
| Enum value | switch |
| Day or month number | switch |
| Numeric range | if-else |
| Multiple boolean conditions | if-else |
| Null validation | if |
| Floating-point comparison | if |
| Simple exact mapping to value | Switch expression |
Practice Exercises
Exercise 1: Day Name
Day number 1–7 input নিন এবং day name print করুন।
Invalid value handle করুন।
Exercise 2: Weekday or Weekend
Rules:
1–5 → Weekday
6–7 → Weekend
Arrow-style switch ব্যবহার করুন।
Exercise 3: Menu Selection
Menu:
1. Create course
2. View courses
3. Update course
4. Exit
User option অনুযায়ী message দেখান।
Exercise 4: Grade Description
char grade অনুযায়ী description দিন:
A → Excellent
B → Very good
C → Good
D → Needs improvement
F → Failed
Exercise 5: User Role
String role অনুযায়ী dashboard name return করুন:
ADMIN
INSTRUCTOR
LEARNER
Input normalize করুন।
Exercise 6: HTTP Status
Status code অনুযায়ী message return করুন:
200 → Success
201 → Created
400 → Bad request
401 → Unauthorized
404 → Not found
500 → Server error
Switch expression ব্যবহার করুন।
Exercise 7: Month Days
Month number অনুযায়ী number of days return করুন।
February-এর জন্য 28 ব্যবহার করুন।
Exercise 8: Basic Calculator
User থেকে নিন:
- Operation
- First number
- Second number
Operations:
+
-
*
/
String বা char switch ব্যবহার করুন।
Division by zero handle করুন।
Exercise 9: Course Level
Course level অনুযায়ী description দিন:
BEGINNER
INTERMEDIATE
ADVANCED
Switch expression ব্যবহার করুন।
Exercise 10: Rewrite Traditional Switch
নিচের traditional switch arrow-style-এ rewrite করুন:
switch (option) {
case 1:
System.out.println("Create");
break;
case 2:
System.out.println("Update");
break;
default:
System.out.println("Unknown");
}
Exercise 11: Fix Fall-Through
নিচের code fix করুন:
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
default:
System.out.println("Invalid");
}
Exercise 12: Switch Expression with yield
একটি case block-এ:
- Message print করুন
- একটি result String
yieldকরুন
Exercise 13: Null-Safe Switch
একটি nullable role switch করার আগে null validation করুন।
Exercise 14: Enum Switch
একটি enum তৈরি করুন:
DRAFT
PUBLISHED
ARCHIVED
প্রতিটি status-এর জন্য message return করুন।
Exercise 15: Choose if-else or switch
নিচের situation-এর জন্য উপযুক্ত structure লিখুন:
- Age
18বা বেশি - Menu option
1–5 - Mark range থেকে grade
- User role
- Email verified এবং account active
- Day number
- Product price discount range
- Course status enum
Knowledge Check
Question 1
switch কী কাজে ব্যবহৃত হয়?
Question 2
case কী represent করে?
Question 3
default কখন execute হয়?
Question 4
Traditional switch-এ break কেন প্রয়োজন?
Question 5
Fall-through কী?
Question 6
Arrow-style switch-এ break প্রয়োজন হয় কি?
Question 7
Multiple case label arrow-style-এ কীভাবে লেখা হয়?
Question 8
Switch statement এবং switch expression-এর পার্থক্য কী?
Question 9
Switch expression-এর শেষে semicolon লাগে কি?
Question 10
yield কী করে?
Question 11
Simple arrow expression-এ yield লাগে কি?
Question 12
String switch case-sensitive কি?
Question 13
Null String switch করলে কী হতে পারে?
Question 14
long বা double সাধারণ switch type হিসেবে supported কি?
Question 15
Exact match-এর জন্য switch নাকি if-else বেশি suitable?
Question 16
Numeric range-এর জন্য কোনটি বেশি suitable?
Question 17
Switch expression-এ default কেন প্রয়োজন হতে পারে?
Question 18
Duplicate case allowed কি?
Question 19
একটি case block থেকে value দিতে কোন keyword ব্যবহার করা হয়?
Question 20
Enum-এর সব constants cover করলে default সবসময় প্রয়োজন কি?
Knowledge Check Answers
Answer 1
একটি value-এর exact match অনুযায়ী multiple branch-এর মধ্যে একটি select করতে switch ব্যবহার করা হয়।
Answer 2
একটি possible matching value।
Answer 3
কোনো case match না করলে।
Answer 4
Matching case execute হওয়ার পর switch থেকে বের হতে এবং accidental fall-through prevent করতে।
Answer 5
একটি matching case থেকে execution পরবর্তী caseগুলোতে continue হওয়া।
Answer 6
না।
Answer 7
case 1, 2, 3 -> ...
Answer 8
Switch statement action execute করে। Switch expression একটি value produce করতে পারে।
Answer 9
হ্যাঁ।
String result = switch (...) {
// Cases
};
Answer 10
Switch expression-এর block case থেকে value produce করে।
Answer 11
না।
Answer 12
হ্যাঁ।
Answer 13
NullPointerException হতে পারে।
Answer 14
না, সাধারণ traditional switch usage-এ supported নয়।
Answer 15
switch।
Answer 16
if-else।
Answer 17
সব possible input-এর জন্য একটি value নিশ্চিত করতে।
Answer 18
না।
Answer 19
yield
Answer 20
না। Compiler যদি সব possible enum value covered বুঝতে পারে, default প্রয়োজন নাও হতে পারে।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
switchexact value matching-এর জন্য usefulcasepossible branch define করেdefaultunmatched input handle করে- Traditional switch-এ
breakfall-through prevent করে - Missing
breakaccidental multiple branch execution ঘটাতে পারে - Multiple traditional cases একই action share করতে পারে
- Arrow-style switch automatic fall-through prevent করে
- Arrow syntax-এ
breakপ্রয়োজন হয় না - Comma দিয়ে multiple case label group করা যায়
- Switch expression সরাসরি value return করে
- Switch expression-এর শেষে semicolon প্রয়োজন
- Block case থেকে value দিতে
yieldব্যবহার করা হয় - String switch case-sensitive
- String input switch করার আগে normalize করা useful
- Null value switch করার আগে validate করা উচিত
char,int, String এবং enum switch-এর common inputlong,float,doubleএবংbooleanসাধারণ switch input নয়- Exact fixed values-এর জন্য
switchreadable - Numeric range এবং complex boolean logic-এর জন্য
if-elseভালো - Arrow-style switch traditional switch-এর অনেক fall-through bug কমায়
Next Lesson
পরবর্তী lesson-এ আমরা for loop শিখব।
আমরা জানব:
- Repetition এবং iteration
- Basic
forloop - Initialization
- Condition
- Update expression
- Loop counter
- Forward এবং backward counting
- Step size
- Nested loop
- Accumulator
- Common loop mistakes