for / of keywords
Bnlang has exactly one form of for loop: for (var <name> of <list>) { ... }. It binds <name> to each element of <list> in order. The English form is for ... of; the Bangla form is প্রতি ... এর.
There is no C-style for (init; test; update) loop. For counter-driven loops, use while.
Syntax
for (var <name> of <list>) {
// body
}
Examples
for (var n of [1, 2, 3, 4, 5]) {
print(n);
}
Bangla form:
প্রতি (চলক n এর [1, 2, 3, 4, 5]) {
print(n);
}
Iterating with an index
If you need the index too, pair for ... of with a counter:
var items = ["a", "b", "c"];
var i = 0;
for (var item of items) {
print(i, item);
i = i + 1;
}
What's not here
- No C-style
for.for (var i = 0; i < n; i++)is not valid syntax — usewhile. - No
for ... in. To iterate map keys, usefor (var k of m.keys()) { ... }.
Early exit and skip
Use break to exit the loop and continue to skip to the next item:
for (var n of [1, 2, 3, 4, 5]) {
if (n == 4) { break; }
if (n == 2) { continue; }
print(n);
}
// 1
// 3
See also
while— for counter-driven or condition-driven loops.