Control Flow

Loops

BanglaCode provides two loop constructs: jotokkhon (while) for condition-based loops and ghuriye (for) for counter-based iteration.

While Loop (jotokkhon)

The jotokkhon loop repeats a block of code while a condition is true. The Bengali word means "as long as":

1dhoro i = 0;
2
3jotokkhon (i < 5) {
4 dekho("Count:", i);
5 i = i + 1;
6}
7
8// Output:
9// Count: 0
10// Count: 1
11// Count: 2
12// Count: 3
13// Count: 4

While Loop Syntax

1jotokkhon (condition) {
2 // Code to repeat
3 // Must update condition to avoid infinite loop
4}

Example: Sum of Numbers

1dhoro sum = 0;
2dhoro n = 1;
3
4jotokkhon (n <= 10) {
5 sum = sum + n;
6 n = n + 1;
7}
8
9dekho("Sum of 1 to 10:", sum); // 55

Example: User Input Loop

1dhoro input = "";
2
3jotokkhon (input != "quit") {
4 dekho("Enter a command (type 'quit' to exit):");
5 input = nao();
6
7 jodi (input != "quit") {
8 dekho("You entered:", input);
9 }
10}
11
12dekho("Goodbye!");

For Loop (ghuriye)

The ghuriye loop is used for counter-based iteration. The Bengali word means "rotating" or "looping":

1ghuriye (dhoro i = 0; i < 5; i = i + 1) {
2 dekho("Number:", i);
3}
4
5// Output:
6// Number: 0
7// Number: 1
8// Number: 2
9// Number: 3
10// Number: 4

For Loop Syntax

1ghuriye (initialization; condition; update) {
2 // Code to repeat
3}
4
5// Parts:
6// - initialization: Run once before loop starts
7// - condition: Checked before each iteration
8// - update: Run after each iteration

Counting Up

1// Count from 1 to 10
2ghuriye (dhoro i = 1; i <= 10; i = i + 1) {
3 dekho(i);
4}
5
6// Count by 2s
7ghuriye (dhoro i = 0; i <= 10; i = i + 2) {
8 dekho(i); // 0, 2, 4, 6, 8, 10
9}

Counting Down

1// Countdown from 10 to 1
2ghuriye (dhoro i = 10; i >= 1; i = i - 1) {
3 dekho(i);
4}
5
6dekho("Blast off!");

Iterating Over Arrays

1dhoro fruits = ["Apple", "Banana", "Mango", "Orange"];
2
3// Using index-based for loop
4ghuriye (dhoro i = 0; i < dorghyo(fruits); i = i + 1) {
5 dekho(i + 1, "-", fruits[i]);
6}
7
8// Output:
9// 1 - Apple
10// 2 - Banana
11// 3 - Mango
12// 4 - Orange

Nested Loops

1// Multiplication table
2ghuriye (dhoro i = 1; i <= 5; i = i + 1) {
3 dhoro row = "";
4 ghuriye (dhoro j = 1; j <= 5; j = j + 1) {
5 row = row + lipi(i * j) + "\t";
6 }
7 dekho(row);
8}
9
10// Output:
11// 1 2 3 4 5
12// 2 4 6 8 10
13// 3 6 9 12 15
14// 4 8 12 16 20
15// 5 10 15 20 25

Pattern Printing

1// Print a triangle pattern
2dhoro n = 5;
3
4ghuriye (dhoro i = 1; i <= n; i = i + 1) {
5 dhoro stars = "";
6 ghuriye (dhoro j = 1; j <= i; j = j + 1) {
7 stars = stars + "* ";
8 }
9 dekho(stars);
10}
11
12// Output:
13// *
14// * *
15// * * *
16// * * * *
17// * * * * *

Loop Control

Use thamo (break) to exit a loop early and chharo (continue) to skip to the next iteration. See the Break & Continue page for details.

1// Using thamo (break)
2ghuriye (dhoro i = 1; i <= 10; i = i + 1) {
3 jodi (i == 6) {
4 thamo; // Exit loop when i is 6
5 }
6 dekho(i);
7}
8// Output: 1 2 3 4 5
9
10// Using chharo (continue)
11ghuriye (dhoro i = 1; i <= 5; i = i + 1) {
12 jodi (i == 3) {
13 chharo; // Skip when i is 3
14 }
15 dekho(i);
16}
17// Output: 1 2 4 5

Practical Examples

Finding Prime Numbers

1kaj isPrime(n) {
2 jodi (n < 2) {
3 ferao mittha;
4 }
5
6 ghuriye (dhoro i = 2; i * i <= n; i = i + 1) {
7 jodi (n % i == 0) {
8 ferao mittha;
9 }
10 }
11
12 ferao sotti;
13}
14
15// Print primes up to 50
16dekho("Prime numbers up to 50:");
17ghuriye (dhoro n = 2; n <= 50; n = n + 1) {
18 jodi (isPrime(n)) {
19 dekho(n);
20 }
21}

Fibonacci Sequence

1dhoro n = 10;
2dhoro a = 0;
3dhoro b = 1;
4
5dekho("First", n, "Fibonacci numbers:");
6
7ghuriye (dhoro i = 0; i < n; i = i + 1) {
8 dekho(a);
9 dhoro temp = a + b;
10 a = b;
11 b = temp;
12}

Array Processing

1dhoro numbers = [4, 2, 9, 1, 7, 5, 3, 8, 6];
2
3// Find maximum
4dhoro max = numbers[0];
5ghuriye (dhoro i = 1; i < dorghyo(numbers); i = i + 1) {
6 jodi (numbers[i] > max) {
7 max = numbers[i];
8 }
9}
10dekho("Maximum:", max); // 9
11
12// Calculate sum
13dhoro total = 0;
14ghuriye (dhoro i = 0; i < dorghyo(numbers); i = i + 1) {
15 total = total + numbers[i];
16}
17dekho("Sum:", total); // 45
18
19// Calculate average
20dekho("Average:", total / dorghyo(numbers)); // 5

String Manipulation

1dhoro text = "Hello";
2
3// Reverse a string
4dhoro reversed = "";
5ghuriye (dhoro i = dorghyo(text) - 1; i >= 0; i = i - 1) {
6 reversed = reversed + text[i];
7}
8dekho("Reversed:", reversed); // "olleH"
9
10// Count vowels
11dhoro vowels = "aeiouAEIOU";
12dhoro count = 0;
13ghuriye (dhoro i = 0; i < dorghyo(text); i = i + 1) {
14 jodi (khojo(vowels, text[i]) >= 0) {
15 count = count + 1;
16 }
17}
18dekho("Vowels:", count); // 2

Infinite Loops

Be careful to ensure your loop condition eventually becomes false, or use thamoto exit:

1// Infinite loop with break
2jotokkhon (sotti) {
3 dhoro input = nao("Enter command: ");
4
5 jodi (input == "exit") {
6 thamo; // Exit the infinite loop
7 }
8
9 dekho("Processing:", input);
10}
11
12// Warning: This would run forever!
13// dhoro i = 0;
14// jotokkhon (i < 10) {
15// dekho(i);
16// // Forgot to increment i!
17// }