langur

for/while loops

There are 6 types of loops in langur.

1 part while test { ... }
Note that prior to 0.8, a 1 part loop used the for token.
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 { ... }
Note that prior to 0.8, a 1 part loop used the for token.
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.

The init and increment sections are each a comma-delimited list (may contain multiple expressions).

while loop

A while loop (test only) uses no semicolons.

0.8 changes the keyword for a test only loop from for to while, with implicit loop value variable _while.

for in/of

A for in loop iterates over the values of an array, 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 array 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.

To loop over hash keys prior to 0.7.2, you would have to use something like for .k in keys(.h).... Now, 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 given 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

As of 0.9.6, concatenating to null returns the right-hand operand if it is a string, array, hash, or null, making for loop value assignment easier.

break with value

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

Note that previously, you would not use the assignment operator for break with value. 0.9.6 adds use of the operator and deprecates not using the operator (still works).

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