Control Flow

Break & Continue

BanglaCode provides thamo (break) to exit loops early andchharo (continue) to skip to the next iteration.

Break Statement (thamo)

The thamo keyword (meaning "stop") immediately exits the current loop:

1// Exit loop when condition is met
2ghuriye (dhoro i = 1; i <= 10; i = i + 1) {
3 jodi (i == 6) {
4 dekho("Stopping at", i);
5 thamo;
6 }
7 dekho(i);
8}
9
10dekho("Loop ended");
11
12// Output:
13// 1
14// 2
15// 3
16// 4
17// 5
18// Stopping at 6
19// Loop ended

Break in While Loop

1dhoro found = mittha;
2dhoro numbers = [4, 8, 15, 16, 23, 42];
3dhoro target = 16;
4dhoro index = 0;
5
6jotokkhon (index < dorghyo(numbers)) {
7 jodi (numbers[index] == target) {
8 found = sotti;
9 dekho("Found", target, "at index", index);
10 thamo;
11 }
12 index = index + 1;
13}
14
15jodi (na found) {
16 dekho(target, "not found");
17}

Break with Infinite Loop

1// Common pattern: infinite loop with break condition
2jotokkhon (sotti) {
3 dekho("Menu:");
4 dekho("1. Play");
5 dekho("2. Settings");
6 dekho("3. Exit");
7
8 dhoro choice = nao("Enter choice: ");
9
10 jodi (choice == "3") {
11 dekho("Goodbye!");
12 thamo;
13 }
14
15 jodi (choice == "1") {
16 dekho("Starting game...");
17 } nahole jodi (choice == "2") {
18 dekho("Opening settings...");
19 } nahole {
20 dekho("Invalid choice");
21 }
22}

Continue Statement (chharo)

The chharo keyword (meaning "skip" or "leave") skips the rest of the current iteration and continues with the next:

1// Skip even numbers
2ghuriye (dhoro i = 1; i <= 10; i = i + 1) {
3 jodi (i % 2 == 0) {
4 chharo; // Skip even numbers
5 }
6 dekho(i); // Only prints odd numbers
7}
8
9// Output: 1 3 5 7 9

Continue in While Loop

1dhoro i = 0;
2
3jotokkhon (i < 10) {
4 i = i + 1;
5
6 // Skip multiples of 3
7 jodi (i % 3 == 0) {
8 chharo;
9 }
10
11 dekho(i);
12}
13
14// Output: 1 2 4 5 7 8 10

Skipping Invalid Data

1dhoro data = [10, -5, 20, khali, 30, 0, 15];
2
3dhoro sum = 0;
4dhoro count = 0;
5
6ghuriye (dhoro i = 0; i < dorghyo(data); i = i + 1) {
7 dhoro value = data[i];
8
9 // Skip invalid values
10 jodi (value == khali ba value <= 0) {
11 chharo;
12 }
13
14 sum = sum + value;
15 count = count + 1;
16}
17
18dekho("Sum of positive numbers:", sum); // 75
19dekho("Count of valid numbers:", count); // 4
20dekho("Average:", sum / count); // 18.75

Nested Loops

thamo and chharo only affect the innermost loop:

1// Break only exits inner loop
2ghuriye (dhoro i = 1; i <= 3; i = i + 1) {
3 dekho("Outer loop i =", i);
4
5 ghuriye (dhoro j = 1; j <= 5; j = j + 1) {
6 jodi (j == 3) {
7 thamo; // Only breaks inner loop
8 }
9 dekho(" Inner loop j =", j);
10 }
11}
12
13// Output:
14// Outer loop i = 1
15// Inner loop j = 1
16// Inner loop j = 2
17// Outer loop i = 2
18// Inner loop j = 1
19// Inner loop j = 2
20// Outer loop i = 3
21// Inner loop j = 1
22// Inner loop j = 2

Breaking Outer Loop with Flag

1// Use a flag to break outer loop
2dhoro shouldBreak = mittha;
3
4ghuriye (dhoro i = 1; i <= 5; i = i + 1) {
5 ghuriye (dhoro j = 1; j <= 5; j = j + 1) {
6 dekho("i =", i, "j =", j);
7
8 jodi (i == 2 ebong j == 3) {
9 shouldBreak = sotti;
10 thamo;
11 }
12 }
13
14 jodi (shouldBreak) {
15 thamo;
16 }
17}
18
19dekho("Exited both loops");

Practical Examples

Finding First Match

1kaj findFirst(arr, predicate) {
2 ghuriye (dhoro i = 0; i < dorghyo(arr); i = i + 1) {
3 jodi (predicate(arr[i])) {
4 ferao arr[i];
5 }
6 }
7 ferao khali;
8}
9
10dhoro numbers = [1, 4, 9, 16, 25, 36];
11
12// Find first number > 10
13dhoro result = findFirst(numbers, kaj(n) { ferao n > 10; });
14dekho("First > 10:", result); // 16

Processing Until Error

1dhoro items = ["apple", "banana", "ERROR", "cherry", "date"];
2
3ghuriye (dhoro i = 0; i < dorghyo(items); i = i + 1) {
4 dhoro item = items[i];
5
6 jodi (item == "ERROR") {
7 dekho("Error encountered, stopping processing");
8 thamo;
9 }
10
11 dekho("Processing:", item);
12}
13
14// Output:
15// Processing: apple
16// Processing: banana
17// Error encountered, stopping processing

Filtering with Continue

1dhoro scores = [85, 42, 91, 67, 73, 38, 95, 88];
2
3// Print only passing scores (>= 60)
4dekho("Passing scores:");
5
6ghuriye (dhoro i = 0; i < dorghyo(scores); i = i + 1) {
7 jodi (scores[i] < 60) {
8 chharo; // Skip failing scores
9 }
10 dekho(scores[i]);
11}
12
13// Output: 85 91 67 73 95 88

Game Loop with Multiple Exit Conditions

1dhoro health = 100;
2dhoro rounds = 0;
3dhoro maxRounds = 10;
4
5jotokkhon (sotti) {
6 rounds = rounds + 1;
7
8 // Simulate damage
9 dhoro damage = kache(lotto() * 30);
10 health = health - damage;
11
12 dekho("Round", rounds, "- Damage:", damage, "- Health:", health);
13
14 // Check win condition
15 jodi (rounds >= maxRounds) {
16 dekho("You survived all rounds!");
17 thamo;
18 }
19
20 // Check lose condition
21 jodi (health <= 0) {
22 dekho("Game Over! You lasted", rounds, "rounds");
23 thamo;
24 }
25}

Best Practices

  • Use thamo for early exit when a goal is achieved (e.g., finding an element)
  • Use chharo to skip invalid or unwanted iterations
  • Avoid excessive use of thamo/chharo as it can make code harder to follow
  • Consider extracting complex loop logic into functions with ferao instead
  • For nested loops, use flags or functions to manage breaking outer loops