Programming and Java Fundamentals
Break, Continue, and Loop Control
You are viewing a free preview lesson.
Lesson Overview
Loop সাধারণত condition false না হওয়া পর্যন্ত চলতে থাকে।
কিন্তু কিছু situation-এ আমাদের প্রয়োজন হতে পারে:
- Expected result পাওয়ার সঙ্গে সঙ্গে loop বন্ধ করা
- একটি invalid value skip করে পরবর্তী iteration-এ যাওয়া
- User exit option select করলে menu loop শেষ করা
- একটি String-এর মধ্যে target character পাওয়া গেলে search বন্ধ করা
- Nested loop-এর outer loop থেকে বের হওয়া
Java loop-এর execution control করার জন্য প্রধানত দুটি statement দেয়:
breakcontinue
এই lesson-এ আমরা শিখব:
breakকীভাবে loop বন্ধ করেcontinueকীভাবে current iteration skip করে- Search loop
- Sentinel এবং menu exit
- Input filtering
breakএবংcontinueinfor,while, anddo-while- Nested loop control
- Labeled
break - Labeled
continue - Early exit এবং readable loop design
- Common control-flow mistakes
Learning Objectives
এই lesson শেষ করার পর আপনি পারবেন:
breakব্যবহার করে loop early terminate করতেcontinueব্যবহার করে current iteration skip করতে- Search result পাওয়া গেলে loop বন্ধ করতে
- Invalid data skip করতে
- Infinite menu loop safely exit করতে
- Nested loop-এর execution control করতে
- Labeled
breakএবং labeledcontinueবুঝতে breakওcontinue-এর difference explain করতে- Common loop-control bugs শনাক্ত করতে
- Cleaner এবং readable loop লিখতে
What Is Loop Control?
Loop control হলো loop-এর normal execution flow পরিবর্তন করা।
Normal loop:
for (int number = 1; number <= 5; number++) {
System.out.println(number);
}
Output:
1
2
3
4
5
break ব্যবহার করলে loop আগেই শেষ হতে পারে।
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
}
System.out.println(number);
}
Output:
1
2
continue ব্যবহার করলে একটি iteration skip হতে পারে।
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
Output:
1
2
4
5
The break Statement
break nearest loop বা switch statement immediately শেষ করে।
Syntax:
break;
Loop-এর মধ্যে:
for (...) {
if (condition) {
break;
}
}
Basic Break Example
for (int number = 1; number <= 10; number++) {
if (number == 5) {
break;
}
System.out.println(number);
}
Output:
1
2
3
4
যখন:
number == 5
true হয়েছে, break loop বন্ধ করেছে।
5 print হয়নি, কারণ break print statement-এর আগে ছিল।
Code After the Loop
for (int number = 1; number <= 10; number++) {
if (number == 5) {
break;
}
System.out.println(number);
}
System.out.println("Loop finished");
Output:
1
2
3
4
Loop finished
break শুধু loop শেষ করে। পুরো method বা program শেষ করে না।
Break Position Matters
Break Before Printing
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
}
System.out.println(number);
}
Output:
1
2
Break After Printing
for (int number = 1; number <= 5; number++) {
System.out.println(number);
if (number == 3) {
break;
}
}
Output:
1
2
3
Statement order result পরিবর্তন করে।
Break in a While Loop
int number = 1;
while (number <= 10) {
if (number == 5) {
break;
}
System.out.println(number);
number++;
}
Output:
1
2
3
4
Break in a Do-While Loop
int number = 1;
do {
if (number == 5) {
break;
}
System.out.println(number);
number++;
} while (number <= 10);
Output:
1
2
3
4
Searching with Break
ধরা যাক একটি String-এর মধ্যে target character খুঁজতে হবে।
String text = "Java Foundation";
char target = 'F';
int foundIndex = -1;
for (
int index = 0;
index < text.length();
index++
) {
if (text.charAt(index) == target) {
foundIndex = index;
break;
}
}
System.out.println(
"Found index: " + foundIndex
);
Output:
Found index: 5
Target পাওয়া গেলে remaining characters check করার প্রয়োজন নেই।
Why Break Helps Search Performance
Without break:
for (
int index = 0;
index < text.length();
index++
) {
if (text.charAt(index) == target) {
foundIndex = index;
}
}
Loop পুরো String process করবে।
With break:
if (text.charAt(index) == target) {
foundIndex = index;
break;
}
First match পাওয়ার সঙ্গে সঙ্গে loop শেষ হবে।
Finding Whether a Value Exists
String text = "Learn Java";
char target = 'J';
boolean found = false;
for (
int index = 0;
index < text.length();
index++
) {
if (text.charAt(index) == target) {
found = true;
break;
}
}
System.out.println(
"Found: " + found
);
Output:
Found: true
Break in an Infinite Loop
Intentional infinite loop থেকে condition অনুযায়ী বের হতে break ব্যবহার করা যায়।
int number = 1;
while (true) {
System.out.println(number);
if (number == 5) {
break;
}
number++;
}
Output:
1
2
3
4
5
while (true) নিজে শেষ হয় না। break exit condition দেয়।
Menu Loop with Break
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
while (true) {
System.out.println();
System.out.println("1. View courses");
System.out.println("2. Create course");
System.out.println("3. Exit");
System.out.print(
"Choose an option: "
);
int option =
Integer.parseInt(
scanner
.nextLine()
.strip()
);
if (option == 3) {
System.out.println("Goodbye");
break;
}
switch (option) {
case 1 ->
System.out.println(
"Loading courses"
);
case 2 ->
System.out.println(
"Opening course form"
);
default ->
System.out.println(
"Invalid option"
);
}
}
scanner.close();
}
}
Break and Sentinel Values
Sentinel loop:
int total = 0;
while (true) {
System.out.print(
"Enter a number or -1 to stop: "
);
int number =
Integer.parseInt(
scanner
.nextLine()
.strip()
);
if (number == -1) {
break;
}
total += number;
}
Sentinel input process হওয়ার আগে break করা হয়েছে।
তাই -1 total-এর মধ্যে যোগ হবে না।
Limited Password Attempts
import java.util.Scanner;
public class Main {
static final String CORRECT_PASSWORD =
"java123";
static final int MAXIMUM_ATTEMPTS = 3;
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
boolean authenticated = false;
for (
int attempt = 1;
attempt <= MAXIMUM_ATTEMPTS;
attempt++
) {
System.out.print(
"Enter password: "
);
String password =
scanner.nextLine();
if (
CORRECT_PASSWORD.equals(
password
)
) {
authenticated = true;
break;
}
System.out.println(
"Incorrect password"
);
}
if (authenticated) {
System.out.println(
"Access granted"
);
} else {
System.out.println(
"Account locked"
);
}
scanner.close();
}
}
Correct password পাওয়া গেলে remaining attempts unnecessary, তাই break।
What Is Continue?
continue current iteration-এর remaining statements skip করে পরবর্তী iteration-এ যায়।
Syntax:
continue;
Example:
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
Output:
1
2
4
5
Loop শেষ হয়নি। শুধু number == 3 iteration-এর remaining code skip হয়েছে।
How Continue Works in a For Loop
for (initialization; condition; update) {
if (skipCondition) {
continue;
}
// Remaining body
}
continue হলে:
- Current body-এর remaining statements skip হয়
forloop-এর update expression execute হয়- Condition আবার check হয়
- Next iteration শুরু হয়
Continue Example with Even Numbers
for (int number = 1; number <= 10; number++) {
if (number % 2 != 0) {
continue;
}
System.out.println(number);
}
Output:
2
4
6
8
10
Odd number হলে current iteration skip হয়েছে।
Continue Example with Odd Numbers
for (int number = 1; number <= 10; number++) {
if (number % 2 == 0) {
continue;
}
System.out.println(number);
}
Output:
1
3
5
7
9
Filtering Invalid Values
int total = 0;
for (int number = -3; number <= 5; number++) {
if (number < 0) {
continue;
}
total += number;
}
System.out.println(total);
Processed values:
0
1
2
3
4
5
Output:
15
Negative values skip হয়েছে।
Skipping Invalid Marks
int[] marks = {
80,
-10,
90,
120,
70
};
int validTotal = 0;
int validCount = 0;
for (int mark : marks) {
if (mark < 0 || mark > 100) {
continue;
}
validTotal += mark;
validCount++;
}
Enhanced for loop এবং arrays future lesson-এ বিস্তারিত শেখানো হবে।
Core idea:
Invalid value skip করুন, valid value process করুন।
Continue in a While Loop
while loop-এ continue ব্যবহার করার সময় update carefully handle করতে হয়।
Correct:
int number = 0;
while (number < 5) {
number++;
if (number == 3) {
continue;
}
System.out.println(number);
}
Output:
1
2
4
5
Update continue-এর আগে হয়েছে।
Continue Causing an Infinite While Loop
Wrong:
int number = 1;
while (number <= 5) {
if (number == 3) {
continue;
}
System.out.println(number);
number++;
}
Execution:
number = 3
condition true
continue
number++ skipped
number remains 3
Loop infinite হয়ে যায়।
Fixing Continue in While
Option 1: update first।
int number = 0;
while (number < 5) {
number++;
if (number == 3) {
continue;
}
System.out.println(number);
}
Option 2: update before continue।
int number = 1;
while (number <= 5) {
if (number == 3) {
number++;
continue;
}
System.out.println(number);
number++;
}
প্রথম version সাধারণত cleaner।
Continue in Do-While
int number = 0;
do {
number++;
if (number == 3) {
continue;
}
System.out.println(number);
} while (number < 5);
Output:
1
2
4
5
continue হলে do-while condition check হয়, তারপর next iteration শুরু হয়।
Break vs Continue
| Statement | Effect |
|---|---|
break | পুরো nearest loop শেষ করে |
continue | শুধু current iteration skip করে |
return | পুরো method শেষ করে |
Break Example
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
}
System.out.println(number);
}
Output:
1
2
Continue Example
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
Output:
1
2
4
5
Return Is Different
static void displayNumbers() {
for (int number = 1; number <= 5; number++) {
if (number == 3) {
return;
}
System.out.println(number);
}
System.out.println("Method completed");
}
Output:
1
2
return method শেষ করেছে।
তাই:
Method completed
print হয়নি।
Continue for Input Filtering
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
int total = 0;
int validCount = 0;
for (int inputNumber = 1; inputNumber <= 5; inputNumber++) {
System.out.print(
"Enter mark "
+ inputNumber
+ ": "
);
int mark =
Integer.parseInt(
scanner
.nextLine()
.strip()
);
if (mark < 0 || mark > 100) {
System.out.println(
"Invalid mark skipped"
);
continue;
}
total += mark;
validCount++;
}
System.out.println(
"Valid total: " + total
);
System.out.println(
"Valid marks: " + validCount
);
scanner.close();
}
}
এখানে invalid input-এর replacement নেওয়া হচ্ছে না। এটি শুধু skip করা হচ্ছে।
Avoiding Deep Nesting with Continue
Without continue:
for (int mark : marks) {
if (mark >= 0 && mark <= 100) {
total += mark;
count++;
if (mark >= 40) {
passedCount++;
}
}
}
With continue:
for (int mark : marks) {
if (mark < 0 || mark > 100) {
continue;
}
total += mark;
count++;
if (mark >= 40) {
passedCount++;
}
}
Invalid case আগে skip করায় main logic less nested।
Continue as a Guard
for (int number = 1; number <= 10; number++) {
if (number % 2 != 0) {
continue;
}
int square =
number * number;
System.out.println(
number + " → " + square
);
}
Output:
2 → 4
4 → 16
6 → 36
8 → 64
10 → 100
Nested Loops and Break
Normal break শুধু nearest loop শেষ করে।
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (column == 2) {
break;
}
System.out.println(
"Row "
+ row
+ ", Column "
+ column
);
}
}
Output:
Row 1, Column 1
Row 2, Column 1
Row 3, Column 1
break inner loop শেষ করেছে।
Outer loop continue করেছে।
Nested Loops and Continue
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (column == 2) {
continue;
}
System.out.println(
"Row "
+ row
+ ", Column "
+ column
);
}
}
Output:
Row 1, Column 1
Row 1, Column 3
Row 2, Column 1
Row 2, Column 3
Row 3, Column 1
Row 3, Column 3
Inner loop-এর column 2 iteration skip হয়েছে।
Searching a Grid
int targetRow = 2;
int targetColumn = 3;
boolean found = false;
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (
row == targetRow
&& column == targetColumn
) {
found = true;
break;
}
}
if (found) {
break;
}
}
System.out.println(
"Found: " + found
);
Inner break শুধু inner loop বন্ধ করে।
তারপর outer loop বন্ধ করতে second break প্রয়োজন হয়েছে।
Labeled Break
Label ব্যবহার করে নির্দিষ্ট outer loop থেকে সরাসরি বের হওয়া যায়।
Syntax:
labelName:
for (...) {
for (...) {
break labelName;
}
}
Example:
search:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (row == 2 && column == 3) {
System.out.println(
"Target found"
);
break search;
}
System.out.println(
"Checking "
+ row
+ ", "
+ column
);
}
}
Output:
Checking 1, 1
Checking 1, 2
Checking 1, 3
Checking 2, 1
Checking 2, 2
Target found
break search; labeled outer loop শেষ করেছে।
Label Syntax
outerLoop:
for (...) {
}
Label:
outerLoop
তারপর:
break outerLoop;
Label identifier naming rule follow করে।
Labeled Continue
Labeled continue outer loop-এর next iteration-এ যেতে পারে।
outer:
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (column == 2) {
continue outer;
}
System.out.println(
"Row "
+ row
+ ", Column "
+ column
);
}
}
Output:
Row 1, Column 1
Row 2, Column 1
Row 3, Column 1
যখন column 2, current outer iteration-এর remaining inner work skip হয়ে next row শুরু হয়েছে।
Use Labels Carefully
Labeled control flow powerful, কিন্তু overuse করলে code বুঝতে কঠিন হয়।
Prefer করতে পারেন:
- Helper method
- Boolean flag
- Early
return - Clear search abstraction
Labels mainly useful যখন nested loop থেকে controlled exit প্রয়োজন এবং alternative আরও complex।
Breaking from a Search Method
Method-এর মধ্যে result পাওয়া গেলে return অনেক সময় labeled break-এর চেয়ে cleaner।
static boolean containsCharacter(
String text,
char target
) {
for (
int index = 0;
index < text.length();
index++
) {
if (text.charAt(index) == target) {
return true;
}
}
return false;
}
Methods পরবর্তী module-এ বিস্তারিত শেখানো হবে।
Search for the First Divisible Number
int foundNumber = -1;
for (int number = 10; number <= 100; number++) {
if (
number % 7 == 0
&& number % 5 == 0
) {
foundNumber = number;
break;
}
}
System.out.println(
"First match: " + foundNumber
);
Output:
First match: 35
Search for the First Uppercase Letter
String text = "java Foundation";
char uppercaseLetter = '\0';
for (
int index = 0;
index < text.length();
index++
) {
char character =
text.charAt(index);
if (Character.isUpperCase(character)) {
uppercaseLetter = character;
break;
}
}
if (uppercaseLetter != '\0') {
System.out.println(
"First uppercase: "
+ uppercaseLetter
);
} else {
System.out.println(
"No uppercase letter found"
);
}
Skipping Whitespace
String text = "Java Foundation";
for (
int index = 0;
index < text.length();
index++
) {
char character =
text.charAt(index);
if (Character.isWhitespace(character)) {
continue;
}
System.out.println(character);
}
Output:
J
a
v
a
F
o
u
n
d
a
t
i
o
n
Space skip হয়েছে।
Counting Only Letters
String text = "Java 21!";
int letterCount = 0;
for (
int index = 0;
index < text.length();
index++
) {
char character =
text.charAt(index);
if (!Character.isLetter(character)) {
continue;
}
letterCount++;
}
System.out.println(
"Letters: " + letterCount
);
Output:
Letters: 4
Processing Positive Numbers Only
int[] numbers = {
10,
-5,
20,
0,
-2,
8
};
int total = 0;
for (int number : numbers) {
if (number <= 0) {
continue;
}
total += number;
}
System.out.println(total);
Output:
38
Processed:
10 + 20 + 8
Practical Example: First Passing Mark
int[] marks = {
20,
35,
42,
80
};
int firstPassingMark = -1;
for (int mark : marks) {
if (mark >= 40) {
firstPassingMark = mark;
break;
}
}
System.out.println(
"First passing mark: "
+ firstPassingMark
);
Output:
First passing mark: 42
Practical Example: Valid Mark Statistics
int[] marks = {
80,
-5,
90,
110,
30,
75
};
int total = 0;
int validCount = 0;
int passedCount = 0;
for (int mark : marks) {
if (mark < 0 || mark > 100) {
continue;
}
total += mark;
validCount++;
if (mark >= 40) {
passedCount++;
}
}
System.out.println(
"Valid marks: " + validCount
);
System.out.println(
"Passed: " + passedCount
);
if (validCount > 0) {
double average =
total / (double) validCount;
System.out.println(
"Average: "
+ "%.2f".formatted(
average
)
);
}
Practical Example: Command Loop
import java.util.Locale;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
while (true) {
System.out.print(
"Enter a command "
+ "(help, status, exit): "
);
String command =
scanner
.nextLine()
.strip()
.toLowerCase(
Locale.ROOT
);
if (command.isBlank()) {
System.out.println(
"Command is required"
);
continue;
}
if (command.equals("exit")) {
System.out.println(
"Application closed"
);
break;
}
switch (command) {
case "help" ->
System.out.println(
"Available commands: "
+ "help, status, exit"
);
case "status" ->
System.out.println(
"Application is running"
);
default ->
System.out.println(
"Unknown command"
);
}
}
scanner.close();
}
}
Practical Example: Input Until Valid Number
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner =
new Scanner(System.in);
int validNumber;
while (true) {
System.out.print(
"Enter a number between 1 and 10: "
);
int number =
Integer.parseInt(
scanner
.nextLine()
.strip()
);
if (number < 1 || number > 10) {
System.out.println(
"Number is outside the range"
);
continue;
}
validNumber = number;
break;
}
System.out.println(
"Accepted number: "
+ validNumber
);
scanner.close();
}
}
When to Use Break
Use break when:
- Search result পাওয়া গেছে
- Exit option selected
- Sentinel পাওয়া গেছে
- Correct password entered
- Further processing unnecessary
- Invalid system state requires loop termination
- Infinite loop-এর exit condition reached
When to Use Continue
Use continue when:
- Invalid item skip করতে হবে
- Current value process করা উচিত নয়
- Filter condition fail করেছে
- Empty input ignore করতে হবে
- Specific number বা character বাদ দিতে হবে
- Main processing block-এর nesting কমাতে হবে
When Not to Use Break or Continue
Overuse করলে loop flow difficult to follow হতে পারে।
Hard to read:
for (...) {
if (...) {
continue;
}
if (...) {
break;
}
if (...) {
continue;
}
if (...) {
break;
}
}
Improve by:
- Conditions simplify করা
- Helper method extract করা
- Clear variable names ব্যবহার করা
- Main loop responsibility ছোট রাখা
Common Error: Code After Break
for (int number = 1; number <= 5; number++) {
if (number == 3) {
break;
System.out.println("Stopped");
}
}
break-এর পরের statement unreachable।
Compiler error হতে পারে।
Correct:
if (number == 3) {
System.out.println("Stopped");
break;
}
Common Error: Code After Continue
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
System.out.println("Skipped");
}
}
continue-এর পরের statement unreachable।
Correct:
if (number == 3) {
System.out.println("Skipped");
continue;
}
Common Error: Expecting Break to End All Loops
for (int row = 1; row <= 3; row++) {
for (int column = 1; column <= 3; column++) {
if (column == 2) {
break;
}
}
}
break শুধু inner loop শেষ করে।
Outer loop চলতে থাকে।
সব loop end করতে:
- Flag ব্যবহার করুন
- Labeled
break - Method থেকে
return - Logic refactor করুন
Common Error: Continue Before Update in While
Wrong:
int number = 1;
while (number <= 5) {
if (number == 3) {
continue;
}
number++;
}
number 3-এ আটকে যায়।
Common Error: Processing Sentinel
Wrong:
while (true) {
int number =
Integer.parseInt(
scanner.nextLine()
);
total += number;
if (number == -1) {
break;
}
}
-1 total-এর মধ্যে যোগ হয়েছে।
Correct:
if (number == -1) {
break;
}
total += number;
Common Error: Wrong Continue Condition
Requirement:
Only valid marks process করুন।
Wrong:
if (mark >= 0 && mark <= 100) {
continue;
}
total += mark;
এখানে valid marks skip হচ্ছে।
Correct:
if (mark < 0 || mark > 100) {
continue;
}
total += mark;
Common Error: Breaking Too Early
for (int number = 1; number <= 10; number++) {
break;
System.out.println(number);
}
Loop first iteration-এই শেষ হয়ে যায় এবং print statement unreachable।
Common Error: Continue Hides Important Work
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
processedCount++;
}
যদি processedCount সব input count করার জন্য হয়, তাহলে 3 incorrectly বাদ পড়ছে।
continue কোন statements skip করছে তা carefully review করুন।
Common Error: Label Overuse
first:
for (...) {
second:
for (...) {
third:
for (...) {
// Complex jumps
}
}
}
এটি maintain করা কঠিন।
Nested logic method বা separate function-এ ভাগ করা better হতে পারে।
Testing Loop Control
Test করুন:
- Break condition first iteration-এ true
- Break condition middle iteration-এ true
- Break condition কখনো true নয়
- Continue condition first value-এ true
- Consecutive values skip হয়
- সব values skip হয়
- কোনো value skip হয় না
- Sentinel first input
- Sentinel processing-এর আগে check হয়
- Nested break কোন loop শেষ করে
- While continue-এর আগে update হয় কি না
- Search target missing হলে default value সঠিক থাকে কি না
Trace Example: Break
for (int number = 1; number <= 5; number++) {
if (number == 4) {
break;
}
System.out.println(number);
}
number | Condition number == 4 | Action |
|---|---|---|
| 1 | false | Print 1 |
| 2 | false | Print 2 |
| 3 | false | Print 3 |
| 4 | true | Break |
Output:
1
2
3
Trace Example: Continue
for (int number = 1; number <= 5; number++) {
if (number == 3) {
continue;
}
System.out.println(number);
}
number | Condition number == 3 | Action |
|---|---|---|
| 1 | false | Print 1 |
| 2 | false | Print 2 |
| 3 | true | Skip |
| 4 | false | Print 4 |
| 5 | false | Print 5 |
Output:
1
2
4
5
Practice Exercises
Exercise 1: Stop at Five
1 থেকে 10 পর্যন্ত loop চালান।
5 আসার আগে loop stop করুন।
Expected output:
1
2
3
4
Exercise 2: Include Five Before Stopping
1 থেকে print করুন এবং 5 print হওয়ার পরে loop stop করুন।
Expected output:
1
2
3
4
5
Exercise 3: Skip Five
1 থেকে 10 পর্যন্ত print করুন, কিন্তু 5 বাদ দিন।
Exercise 4: Print Even Numbers with Continue
1 থেকে 20 পর্যন্ত odd values skip করে even values print করুন।
Exercise 5: Print Odd Numbers with Continue
Even values skip করুন।
Exercise 6: Search for a Character
একটি String-এর মধ্যে প্রথম 'a' character-এর index বের করুন।
Match পাওয়া গেলে loop stop করুন।
Exercise 7: Search for a Number
1 থেকে 100-এর মধ্যে প্রথম number খুঁজুন, যা 6 এবং 7 উভয় দিয়ে divisible।
Expected:
42
Exercise 8: Sentinel Total
User numbers input দেবে।
-1 দিলে loop stop হবে।
Sentinel total-এর মধ্যে যোগ করবেন না।
Exercise 9: Command Menu
Commands:
help
status
exit
Rules:
- Blank command হলে
continue exitহলেbreak- Other commands process করুন
Exercise 10: Skip Invalid Marks
Five marks read করুন।
Range 0–100-এর বাইরে হলে skip করুন।
Calculate:
- Valid count
- Total
- Average
Exercise 11: Password Attempts
Maximum three attempts দিন।
Correct password পেলে immediately loop stop করুন।
Exercise 12: Fix the Infinite Continue Loop
int number = 1;
while (number <= 5) {
if (number == 3) {
continue;
}
System.out.println(number);
number++;
}
Exercise 13: Nested Break
একটি 3 × 3 grid iterate করুন।
প্রতিটি row-তে column 2 এলে inner loop stop করুন।
Expected:
1,1
2,1
3,1
Exercise 14: Labeled Break
Nested loop-এর মধ্যে target coordinate পাওয়া গেলে দুই loop থেকেই বের হয়ে যান।
Exercise 15: Labeled Continue
Column 2 এলে current row-এর remaining columns skip করে next row শুরু করুন।
Exercise 16: Skip Whitespace
একটি sentence-এর সব non-whitespace characters নতুন line-এ print করুন।
Exercise 17: Count Letters Only
Input:
Java 21!
Digits, spaces এবং symbols skip করে শুধু letter count করুন।
Expected:
4
Exercise 18: First Passing Mark
Marks:
20, 35, 42, 80
First passing mark খুঁজে loop stop করুন।
Passing mark:
40
Exercise 19: Positive Values Only
একটি number list থেকে zero এবং negative values skip করে positive total calculate করুন।
Exercise 20: Explain the Difference
নিজের ভাষায় explain করুন:
breakcontinuereturn
Knowledge Check
Question 1
break কী করে?
Question 2
continue কী করে?
Question 3
break-এর পরে loop-এর বাইরের code execute হয় কি?
Question 4
continue কি পুরো loop শেষ করে?
Question 5
for loop-এ continue-এর পরে কোন part execute হয়?
Question 6
while loop-এ continue কেন infinite loop ঘটাতে পারে?
Question 7
Search loop-এ break কেন useful?
Question 8
Normal break nested loop-এর কোন loop শেষ করে?
Question 9
Labeled break কী করে?
Question 10
Labeled continue কী করে?
Question 11
Sentinel কখন check করা উচিত?
Question 12
break এবং return-এর পার্থক্য কী?
Question 13
Invalid item filter করতে কোন statement useful?
Question 14
Correct password পাওয়া গেলে কোন statement useful?
Question 15
continue-এর পরের statements কি current iteration-এ execute হয়?
Question 16
break-এর পরের statements কি current loop iteration-এ execute হয়?
Question 17
Arrow-style switch-এ break সাধারণত প্রয়োজন হয় কি?
Question 18
Label overuse কেন avoid করা উচিত?
Question 19
while (true) loop কীভাবে safely শেষ করা যায়?
Question 20
Loop control test করার একটি গুরুত্বপূর্ণ boundary case কী?
Knowledge Check Answers
Answer 1
Nearest loop বা switch immediately শেষ করে।
Answer 2
Current iteration-এর remaining statements skip করে next iteration-এ যায়।
Answer 3
হ্যাঁ। break শুধু loop শেষ করে।
Answer 4
না।
Answer 5
Update expression execute হয়, তারপর condition check হয়।
Answer 6
Counter update continue-এর পরে থাকলে update skip হতে পারে এবং condition variable একই value-তে আটকে যেতে পারে।
Answer 7
Target পাওয়া গেলে unnecessary remaining iterations বন্ধ করে।
Answer 8
Nearest বা innermost loop।
Answer 9
Named outer loop পর্যন্ত execution terminate করতে পারে।
Answer 10
Named outer loop-এর next iteration শুরু করে।
Answer 11
Sentinel data process করার আগে।
Answer 12
break loop শেষ করে। return পুরো method শেষ করে।
Answer 13
continue
Answer 14
break
Answer 15
না।
Answer 16
না।
Answer 17
না।
Answer 18
Control flow বুঝতে এবং maintain করতে কঠিন হয়ে যায়।
Answer 19
Clear exit condition এবং break, return বা exception ব্যবহার করে।
Answer 20
Break condition first iteration-এই true হওয়া, অথবা sentinel প্রথম input হিসেবে আসা।
Lesson Summary
এই lesson-এ আমরা শিখেছি:
breaknearest loop immediately শেষ করেcontinuecurrent iteration-এর remaining code skip করেbreakপুরো method শেষ করে নাreturnmethod শেষ করে- Search result পাওয়া গেলে
breakuseful - Sentinel process করার আগে check করতে হয়
- Infinite menu loop থেকে exit করতে
breakব্যবহার করা যায় - Invalid values skip করতে
continueuseful continuenesting কমাতে guard হিসেবে কাজ করতে পারেforloop-এ continue update expression-এর দিকে যায়whileloop-এ continue counter update skip করলে infinite loop হতে পারে- Normal
breaknested loop-এর শুধু nearest loop শেষ করে - Labeled
breakouter loop শেষ করতে পারে - Labeled
continueouter loop-এর next iteration শুরু করতে পারে - Labels carefully ব্যবহার করা উচিত
- Break বা continue-এর পরের statement unreachable হতে পারে
- Loop control statement-এর position result পরিবর্তন করে
- Overuse করলে control flow difficult to understand হতে পারে
- Clear conditions এবং small loop body maintainability improve করে
Next Lesson
পরবর্তী lesson-এ আমরা Module 1-এর practical project তৈরি করব:
Build a Console-Based Grade Calculator
আমরা ব্যবহার করব:
- Variables
- Primitive data types
- Strings
- Operators
- Type conversion
Scanner- Conditional statements
switchforloopwhileবাdo-whilebreakএবংcontinue- Input validation
- Total, average, grade এবং final result calculation