L/loop keyword



There are 5 forms of loops in 11l.


1. The infinite loop.


L
   print("\a")
   sleep(1)


2. A "while" loop.


L <condition>
   ...

Keep executing a loop body as long the given condition evaluates to true (1B).

Note: if
<condition>
is just a single identifier, like in
L ok {...}
, then please write this:
L ok == 1B {...}
.


3. Loop a specified number of times.


L <number>
   ...

Example:
L 3
   print("ABC")
will print
"ABC"
3 times.

Note: only integer literals/constants are considered so far as
<number>
{}.


4. A "for in" loop.


L(<variable>) <iterable>
   ...

Examples:
L(x) 1..10 {print(x)}
will print
1
through
10
.
L(c) "str" {print(c)}
will print
"s"
,
"t"
and
"r"
.
L(n) [9,7] {print(n)}
will print
9
and
7
.
L(k, v) [‘k’ = 1] {print(k‘=’v)}
will print
k=1
.

If you want to modify the items/elements of the iterating container, then prefix
<variable>
with
&
:
V arr = [1, 2, 3]
L(&el) arr
   el++
print(arr) // prints `[2, 3, 4]`

Rarely useful stuff


5. A range loop.


L <range>
   ...

Examples:
L 1..n {print("ABC")}
will print
"ABC"
n times.
L 0.<n {print("ABC")}
will print
"ABC"
n times.


L/loop subkeywords



The
L.break
/
loop.break
statement terminates the current loop.
By default
L.break
/
loop.break
will apply to innermost loop. In a situation where you would like to a break for one of the outer loops, you can specify which loop the break statement applies to:
L(y) 0..10
   L(x) 0..10
      I a[y][x] == 1
         L(y).break // breaks out of outer loop over `y`
Or you can use
^L.break
,
^^L.break
, etc.


The
L.continue
/
loop.continue
statement terminates execution of the statements in the current iteration of the current loop, and continues execution of the loop with the next iteration.


The
L.remove_current_element_and_continue
statement removes current element of iterating collection. For example, this code removes all even numbers in
array
:
L(i) array
   I i % 2 == 0
      L.remove_current_element_and_continue


Statements under
L.was_no_break
/
loop.was_no_break
are executed only when loop was successfully finished without
L.break
/
loop.break
(just like else in Python):
L(test) tests
   I !test.ok()
      L.break

L.was_no_break
   print(‘All tests are ok!’)


L.index
/
loop.index
can be used to get the index of the current iteration of loop.


L.last_iteration
/
loop.last_iteration
evaluates to true (1B) if current element of the iterating container is the last one.


L.prev
/
loop.prev
and
L.next
/
loop.next
are not supported now.