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 = 5 starts the counter
  • x >= 0 keeps the loop running
  • x-- lowers the value after each iteration

Step-by-Step Breakdown

  1. The counter starts at 5
  2. The loop checks whether the counter is still valid
  3. The body prints the current value
  4. The counter decreases by one
  5. 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:

  1. print numbers from 1 to 10
  2. count backward from 5 to 1
  3. 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.