const list = [ "a", "b", "c" ] repeat for each string in list write_line ( string ) .
repeat
instructions
repeat
instructions (also called loops) are used to execute a block of code a specific number of times.
Summary:
repeat for each
instruction
The repeat for each
instruction is used to iterate over an iterable object, such as a list, map, iterator etc.
Here is an example of iterating over a list:
Output:
a b c
If you prefer the functional style of iterating, you can convert a list to a stream
and use the stream’s for_each
function with a closure:
const list = [ "a", "b", "c" ] list.stream.for_each ( { string -> write_line ( string ) } )
When iterating over a map
, you can iterate only the keys, only the values, or the keys and values, as shown below:
const map = [ "a": 1, "b": 2, "c": 3 ] // iterate over keys repeat for each key k in map write_line ( k ) . // iterate over values repeat for each value v in map write_line ( v.to_string ) . // iterate over keys and values repeat for each k, v in map write_line ( """{{k}}: {{v.to_string}}""" ) .
Output:
a b c 1 2 3 a: 1 b: 2 c: 3
repeat while
instruction
The repeat while
instruction is used to re-execute a block of code as long as a given condition is fulfilled.
Example:
// remove trailing digits from string variable string = "abc123" repeat while string.last.is_digit and string.size >= 1 string = string.remove_last . assert string =v "abc"
repeat times
instruction
Used to execute a block of code a predefined number of times.
Example:
repeat 3 times write_line ( "Hello" ) .
Ouput:
Hello Hello Hello
repeat from to
instruction
Used to loop with an integer variable whose value is incremented or decremented at each iteration.
Example:
// display even numbers from 2 to 6 repeat from i = 2 to 6 step 2 write ( i.to_string ) .
Ouput:
246
repeat forever
instruction
repeat forever
is used to continuously re-execute a block of code until the loop is explicitly cancelled with the exit repeat
instruction.
Example:
// check for a new message once a second // exit if message is "end" repeat forever const message = get_new_message_in_inbox if message =v "end" then exit repeat . write_line ( message ) se_wait.wait_seconds ( 1L ) .
exit repeat
instruction
The exit repeat
instruction is used to exit any kind of loop, as shown in the previous source code example.
When exit repeat
is encountered, execution continues at the first instruction after the repeat
block. If there is no instruction after the 'repeat' block then the current function simply returns.
Note: exit repeat
in PPL is similar to the break
statement in C# and Java.
next repeat
instruction
The next repeat
instruction is used to stop the current iteration, and immediately start with the next iteration.
Note: next repeat
in PPL is similar to the continue
statement in C# and Java.
Example:
// count the number of digits contained in a string const string = "ab123" variable num_digits = 0 repeat for each character in string if not character.is_digit then next repeat . num_digits = num_digits + 1 . assert num_digits =v 3
Note: An easier way to achieve the same result is to use a stream:
const string = "ab123" const num_digits = string.stream.filter ( { char => char.is_digit } ).count assert num_digits =v 3L
Predefined Constants
There are three predefined labeled values which you can optionally use within each repeat
instruction:
-
is_first
: ayes_no
(boolean) value set toyes
during the loop’s first iteration. -
is_last
: ayes_no
value set toyes
during the loop’s last iteration. -
counter
: an integer value (of typepos_64
) set to the number of times the loop has been iterated. During the first iteration the value is 1. It is then incremented by 1 in each subsequent iteration.
For each value to be used, a constant identifier must be defined for the corresponding label with the syntax label:constant_id
, as shown in the following example:
const list = [ "a", "b", "c", "d" ] repeat for each string in list counter:index is_first:first_iteration is_last:last_iteration if first_iteration then write ( "first element" ) else if last_iteration write ( "last element" ) else write ( "element at index " & index.to_string ) . write_line ( ": " & string ) .
Output:
first element: a element at index 2: b element at index 3: c last element: d