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
chalo (start; condition; update) {
// body
}Runnable example
Same idea in JavaScript and Python
const mithai = ["Ladoo", "Barfi", "Jalebi"];
for (let i = 0; i < mithai.length; i = i + 1) {
console.log(i + 1, mithai[i]);
}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 ,.
chalo (rakho i = 0, i < 3, i = i + 1) { }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).
chalo (rakho i = 0; i < 3; i++) { }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.
chalo (rakho i = 0; i <= lambai(list); i = i + 1) { }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.