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:
| 1 | dhoro age = 20; |
| 2 | |
| 3 | jodi (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:
| 1 | dhoro age = 15; |
| 2 | |
| 3 | jodi (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:
| 1 | dhoro score = 85; |
| 2 | |
| 3 | jodi (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:
| 1 | dhoro age = 25; |
| 2 | dhoro hasLicense = sotti; |
| 3 | dhoro hasInsurance = sotti; |
| 4 | |
| 5 | jodi (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):
| 1 | dhoro age = 25; |
| 2 | dhoro hasLicense = sotti; |
| 3 | dhoro isSuspended = mittha; |
| 4 | |
| 5 | // AND - all conditions must be true |
| 6 | jodi (age >= 18 ebong hasLicense ebong na isSuspended) { |
| 7 | dekho("You can drive!"); |
| 8 | } |
| 9 | |
| 10 | // OR - at least one condition must be true |
| 11 | dhoro isWeekend = sotti; |
| 12 | dhoro isHoliday = mittha; |
| 13 | |
| 14 | jodi (isWeekend ba isHoliday) { |
| 15 | dekho("No work today!"); |
| 16 | } |
| 17 | |
| 18 | // NOT - inverts the condition |
| 19 | jodi (na isSuspended) { |
| 20 | dekho("License is valid"); |
| 21 | } |
| 22 | |
| 23 | // Complex conditions with parentheses |
| 24 | dhoro role = "admin"; |
| 25 | dhoro isActive = sotti; |
| 26 | |
| 27 | jodi ((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 |
| 2 | dhoro x = 10; |
| 3 | |
| 4 | jodi (x == 10) { dekho("Equal to 10"); } |
| 5 | jodi (x != 5) { dekho("Not equal to 5"); } |
| 6 | jodi (x > 5) { dekho("Greater than 5"); } |
| 7 | jodi (x < 20) { dekho("Less than 20"); } |
| 8 | jodi (x >= 10) { dekho("Greater than or equal to 10"); } |
| 9 | jodi (x <= 10) { dekho("Less than or equal to 10"); } |
| 10 | |
| 11 | // String comparison |
| 12 | dhoro name = "Rahim"; |
| 13 | |
| 14 | jodi (name == "Rahim") { |
| 15 | dekho("Hello Rahim!"); |
| 16 | } |
| 17 | |
| 18 | // Array membership check |
| 19 | dhoro fruits = ["apple", "banana", "mango"]; |
| 20 | |
| 21 | jodi (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) |
| 2 | dhoro falsyValues = [mittha, khali, 0]; |
| 3 | |
| 4 | jodi (mittha) { dekho("Won't print"); } |
| 5 | jodi (khali) { dekho("Won't print"); } |
| 6 | jodi (0) { dekho("Won't print"); } |
| 7 | |
| 8 | // Truthy values (treated as true) |
| 9 | jodi (sotti) { dekho("Prints: boolean true"); } |
| 10 | jodi (1) { dekho("Prints: non-zero number"); } |
| 11 | jodi ("hello") { dekho("Prints: non-empty string"); } |
| 12 | jodi ([]) { dekho("Prints: empty array is truthy"); } |
| 13 | jodi ({}) { dekho("Prints: empty map is truthy"); } |
| 14 | |
| 15 | // Practical example: checking for value |
| 16 | dhoro user = getUserFromDatabase(); |
| 17 | |
| 18 | jodi (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 |
| 2 | jodi (x > 0) { |
| 3 | dekho("Positive"); |
| 4 | } |
| 5 | |
| 6 | // Also correct - single line |
| 7 | jodi (x > 0) { dekho("Positive"); } |
| 8 | |
| 9 | // Multiple statements need braces |
| 10 | jodi (x > 0) { |
| 11 | dekho("Positive"); |
| 12 | dekho("Greater than zero"); |
| 13 | } |
Practical Examples
Input Validation
| 1 | kaj 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 | |
| 12 | jodi (validateAge(25)) { |
| 13 | dekho("Age is valid"); |
| 14 | } |
Menu Selection
| 1 | dhoro choice = 2; |
| 2 | |
| 3 | jodi (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
| 1 | kaj 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 | |
| 15 | dekho(getTemperatureMessage(22)); // "Pleasant" |