For Loop
Learn how to repeat a block a fixed number of times with the for loop in BornomalaScript.
For Loop
A for loop is useful when you already know how many times you want to repeat something.
It is one of the cleanest ways to count through a range of values.
Basic Example
Count from 0 to 4:
/*
etar (initialization; condition; logic) jonno {
// body
}
*/
etar (dhoro x = 5; x >= 0; x--) jonno {
lekho(x)
}
This loop starts at 5, keeps running while x >= 0, and decreases x by one each time.
How It Works
The for loop usually has three parts:
- initialization: set the starting value
- condition: check whether the loop should continue
- logic/update: change the counter each time the loop runs
In the example above:
dhoro x = 5starts the counterx >= 0keeps the loop runningx--lowers the value after each iteration
Step-by-Step Breakdown
- The counter starts at 5
- The loop checks whether the counter is still valid
- The body prints the current value
- The counter decreases by one
- The loop repeats until the condition fails
Why Use a For Loop?
For loops are helpful when:
- you want to count a known range
- you want to repeat a task a fixed number of times
- you want cleaner counter-based code
Try These Variations
Practice with other ranges:
etar (dhoro i = 0; i < 5; i++) jonno {
lekho(i)
}
etar (dhoro i = 10; i > 0; i--) jonno {
lekho(i)
}
etar (dhoro i = 2; i <= 20; i = i + 2) jonno {
lekho(i)
}
These examples show how the counter can go up, down, or skip by steps.
Common Mistakes
Beginners often make these mistakes:
- forgetting to update the counter
- using the wrong comparison operator
- starting from the wrong number
- making the loop run forever by accident
Practice Task
Try writing for loops that:
- print numbers from 1 to 10
- count backward from 5 to 1
- print only even numbers
Quick Checklist
Before moving on, make sure you can:
- identify the three parts of a for loop
- count upward and downward
- change the step size
- stop the loop at the right time
If yes, you understand the core idea of the for loop.