Control Flow

Conditionals

BanglaCode uses jodi (if) and nahole (else) for conditional execution. These keywords allow your program to make decisions based on conditions.

Basic If Statement

The jodi keyword executes a block of code only if the condition is true:

1dhoro age = 20;
2
3jodi (age >= 18) {
4 dekho("You are an adult");
5}
6
7// Output: You are an adult

If-Else Statement

Use nahole (else) to specify code to run when the condition is false:

1dhoro age = 15;
2
3jodi (age >= 18) {
4 dekho("You are an adult");
5} nahole {
6 dekho("You are a minor");
7}
8
9// Output: You are a minor

Else-If Chain

Chain multiple conditions using nahole jodi:

1dhoro score = 85;
2
3jodi (score >= 90) {
4 dekho("Grade: A - Excellent!");
5} nahole jodi (score >= 80) {
6 dekho("Grade: B - Very Good!");
7} nahole jodi (score >= 70) {
8 dekho("Grade: C - Good");
9} nahole jodi (score >= 60) {
10 dekho("Grade: D - Satisfactory");
11} nahole {
12 dekho("Grade: F - Needs Improvement");
13}
14
15// Output: Grade: B - Very Good!

Nested Conditionals

You can nest conditionals inside each other:

1dhoro age = 25;
2dhoro hasLicense = sotti;
3dhoro hasInsurance = sotti;
4
5jodi (age >= 18) {
6 jodi (hasLicense) {
7 jodi (hasInsurance) {
8 dekho("You can drive legally!");
9 } nahole {
10 dekho("You need insurance to drive.");
11 }
12 } nahole {
13 dekho("You need a license to drive.");
14 }
15} nahole {
16 dekho("You must be 18 or older to drive.");
17}

Using Logical Operators

Combine conditions using ebong (AND), ba (OR), and na (NOT):

1dhoro age = 25;
2dhoro hasLicense = sotti;
3dhoro isSuspended = mittha;
4
5// AND - all conditions must be true
6jodi (age >= 18 ebong hasLicense ebong na isSuspended) {
7 dekho("You can drive!");
8}
9
10// OR - at least one condition must be true
11dhoro isWeekend = sotti;
12dhoro isHoliday = mittha;
13
14jodi (isWeekend ba isHoliday) {
15 dekho("No work today!");
16}
17
18// NOT - inverts the condition
19jodi (na isSuspended) {
20 dekho("License is valid");
21}
22
23// Complex conditions with parentheses
24dhoro role = "admin";
25dhoro isActive = sotti;
26
27jodi ((role == "admin" ba role == "moderator") ebong isActive) {
28 dekho("Access granted");
29}

Conditional Expressions

Conditions can include any expression that evaluates to a boolean:

1// Comparison operators
2dhoro x = 10;
3
4jodi (x == 10) { dekho("Equal to 10"); }
5jodi (x != 5) { dekho("Not equal to 5"); }
6jodi (x > 5) { dekho("Greater than 5"); }
7jodi (x < 20) { dekho("Less than 20"); }
8jodi (x >= 10) { dekho("Greater than or equal to 10"); }
9jodi (x <= 10) { dekho("Less than or equal to 10"); }
10
11// String comparison
12dhoro name = "Rahim";
13
14jodi (name == "Rahim") {
15 dekho("Hello Rahim!");
16}
17
18// Array membership check
19dhoro fruits = ["apple", "banana", "mango"];
20
21jodi (ache(fruits, "mango")) {
22 dekho("Mango is in the list!");
23}

Truthiness in Conditions

Any value can be used in a condition. BanglaCode evaluates truthiness as follows:

1// Falsy values (treated as false)
2dhoro falsyValues = [mittha, khali, 0];
3
4jodi (mittha) { dekho("Won't print"); }
5jodi (khali) { dekho("Won't print"); }
6jodi (0) { dekho("Won't print"); }
7
8// Truthy values (treated as true)
9jodi (sotti) { dekho("Prints: boolean true"); }
10jodi (1) { dekho("Prints: non-zero number"); }
11jodi ("hello") { dekho("Prints: non-empty string"); }
12jodi ([]) { dekho("Prints: empty array is truthy"); }
13jodi ({}) { dekho("Prints: empty map is truthy"); }
14
15// Practical example: checking for value
16dhoro user = getUserFromDatabase();
17
18jodi (user) {
19 dekho("User found:", user.naam);
20} nahole {
21 dekho("User not found");
22}

Single Statement Conditionals

Always use braces for conditional blocks. BanglaCode requires braces even for single statements:

1// Correct way
2jodi (x > 0) {
3 dekho("Positive");
4}
5
6// Also correct - single line
7jodi (x > 0) { dekho("Positive"); }
8
9// Multiple statements need braces
10jodi (x > 0) {
11 dekho("Positive");
12 dekho("Greater than zero");
13}

Practical Examples

Input Validation

1kaj validateAge(age) {
2 jodi (age < 0) {
3 dekho("Error: Age cannot be negative");
4 ferao mittha;
5 } nahole jodi (age > 150) {
6 dekho("Error: Age seems unrealistic");
7 ferao mittha;
8 }
9 ferao sotti;
10}
11
12jodi (validateAge(25)) {
13 dekho("Age is valid");
14}

Menu Selection

1dhoro choice = 2;
2
3jodi (choice == 1) {
4 dekho("Starting new game...");
5} nahole jodi (choice == 2) {
6 dekho("Loading saved game...");
7} nahole jodi (choice == 3) {
8 dekho("Opening settings...");
9} nahole jodi (choice == 4) {
10 dekho("Goodbye!");
11} nahole {
12 dekho("Invalid choice. Please try again.");
13}

Range Checking

1kaj getTemperatureMessage(temp) {
2 jodi (temp < 0) {
3 ferao "Freezing cold!";
4 } nahole jodi (temp < 15) {
5 ferao "Cold";
6 } nahole jodi (temp < 25) {
7 ferao "Pleasant";
8 } nahole jodi (temp < 35) {
9 ferao "Warm";
10 } nahole {
11 ferao "Hot!";
12 }
13}
14
15dekho(getTemperatureMessage(22)); // "Pleasant"