Skip to main content
Skip to content

Loops keyword

chalo — for loop

chalo is a counting loop with three parts — start, condition and update — separated by semicolons.

Meaning: "let's go"

How chalo works

chalo (rakho i = 1; i <= 5; i = i + 1) { ... } starts i at 1, runs the body while i <= 5, and adds 1 after every round. The counter belongs to the loop and is gone once it finishes.

To visit every item of a list, count from 0 while i < lambai(list) and read list[i]. Counting down works too: start high and use i = i - 1.

AltFLang has no ++ operator. Write i = i + 1 or the shortcut i += 1.

Syntax

syntax
chalo (start; condition; update) {
  // body
}

Runnable example

Expected output

Press Run to try it

1 Ladoo2 Barfi3 Jalebi

Same idea in JavaScript and Python

JavaScript
const mithai = ["Ladoo", "Barfi", "Jalebi"];
for (let i = 0; i < mithai.length; i = i + 1) {
  console.log(i + 1, mithai[i]);
}
Python
mithai = ["Ladoo", "Barfi", "Jalebi"]
for i in range(len(mithai)):
    print(i + 1, mithai[i])

Common mistakes

Commas instead of semicolons

The three parts are separated by ;, not ,.

Galat
chalo (rakho i = 0, i < 3, i = i + 1) { }
Sahi
chalo (rakho i = 0; i < 3; i = i + 1) { bol(i); }

Using i++

++ is not part of AltFLang. Use i = i + 1 (or i += 1).

Galat
chalo (rakho i = 0; i < 3; i++) { }
Sahi
chalo (rakho i = 0; i < 3; i = i + 1) { bol(i); }

Off-by-one with <=

Indexes run from 0 to lambai(list) - 1. Looping while i <= lambai(list) reads one position past the end, which gives khali.

Galat
chalo (rakho i = 0; i <= lambai(list); i = i + 1) { }
Sahi
rakho list = ["a", "b"];
chalo (rakho i = 0; i < lambai(list); i = i + 1) { bol(list[i]); }

chalo: frequently asked questions

How do I write a for loop in Hindi programming?

In AltFLang use chalo: chalo (rakho i = 1; i <= 10; i = i + 1) { bol(i); } prints 1 to 10.

Is there a for-each loop?

There is no separate for-each keyword. Loop over indexes with chalo, or hand a function to har(), chuno() or ikattha() to process every item without writing the loop yourself.