langur

for/while loops

There are 6 types of loops in langur.

1 part while test { ... }
3 part for init; test; increment { ... }
unlimited for { ... }
for in for x in expr { ... }
for of for x of expr { ... }
variable-free for of for of expr { ... }
1 part
while test { ... }
3 part
for init; test;
increment { ... }
unlimited
for { ... }
for in
for x in expr { ... }
for of
for x of expr { ... }
variable-free for of
for of expr { ... }

general rules

No parentheses are used.

Curly braces are required on the loop body.

Langur uses 1-based indexing.

3 part for loop

You can omit any section from a 3 part loop, but not the 2 semicolons.

while loop

A while loop (test only) uses no semicolons.

The use of in and of operators on a where loop test do not created automated loops. This means while x of ... is different than for x of....

for in/of

A for in loop iterates over the values of an list, string, or hash, or over a range or number (implicit 1-based range).

A for of loop iterates over the index itself (or keys of a hash). These will loop the number of times specified by the number (or range), or the length of the list or string.

Given a descending range or number less than 1, a for of loop will not run, but not throw an exception either.

A for in loop will run with an ascending or descending range, or negative number, but not 0.

Loop control variables of for in and for of are protected.

The set of values to loop over is evaluated once for efficiency.

A for in/of loop does not accept floating point numbers or ranges.

You can use for k of h... for the keys or for v in h... for the values. To loop over hash keys in a specific order, you would use something like for k in sort(keys(h))....

break / next

Use break to break out of a loop, and next to try the next iteration.

These keywords apply only to loops (never to switch expressions).

loop value

The implicit default value of a loop expression is null. You can use an assignment expression after a break keyword to break with a specific value.

Using square brackets on the for or while token and an equals sign, you can specify a different default value for a loop. You can set the loop value at any point during the loop. The default for loop value variable is _for and while loop value variable is _while. You can also specify a different loop value variable inside the square brackets.

for[=0] i of 7 { ... } # default value 0 instead of null for[f] i of 7 { ... } # loop value variable f instead of _for for[f=0] i of 7 { ... } # custom variable and default value for[=0] i of 7 { _for += i } # evaluates to 28

Concatenating to null returns the right-hand operand if it is a string, list, hash, or null, making for loop value assignment easy.

break with value

Assign a value to the break keyword to break with a specified value.

for i of 7 { ... break = 21 ... }