HomeManualTopicsTypesSymbols

std::loop

a simple "loop"

Parameters

body
the function to execute for each "iteration" of the "loop"
finally (default: pass)
the function to execute after the last "iteration"

Results

None

Description

The "loop" construct replaces loops like while, do_while and forever.

To start the next iteration of the "loop" use the std::next-function.

std::next and std::break must be called in tail position!

A "loop" is exited via std::break or through returning one or more results.

Example

$buf "" $a 1 loop !buf : # "while" style if a <= 3: $b 1 loop : # "do while" style append &buf " @(a) * @(b) = @(a*b) inc &b if b <= 2 next break : inc &a next -> buf print! buf

Output

1*1 = 1 1*2 = 2 2*1 = 2 2*2 = 4 3*1 = 3 3*2 = 6

Example

$sum 0 $i 0 println! "sum = " loop : inc &i plus &sum i if i < 5 next break -> sum

Output

sum = 15

Topic

Loops

See also

std::repeat invoke a statement sequence for a specific number of times
std::from_to a loop iterating over a sequence of values
std::from_to_by a loop iterating over a sequence of values
std::next starts the next "iteration" of a "loop"
std::break cancels the current "loop"