Home   Manual   Type Index   Symbol Index   Topics Index

std::from_to

a loop iterating over a sequence of values

Parameters

from

the value to use for the first invocation of body

to

the value to use for the last invocation of body

body

the statement sequence to execute for each value

return (optional; default value: pass)

a function to return selected variables to the caller

Description

The starting value is incremented by one after each invocation of body.

The type of the first and last value need not be integers but can be of arbitrary types that support adding the integer value 1.

The loop terminates as soon as the current value is greater than last. (The body will only be called for values that are less than or equal to last.)

If first is greater than last then the loop body will not be called at all!

The body is called with a single argument (the current value).

To start the next iteration of the loop a tail call to the next-function has to be performed.

To exit from the loop a tail call to the break-function has to be performed.

See also

std::loop   a simple loop

Topics

Loops

Example

from_to 1 10: (i)
  writeln i " x " i " = " i*i
  next

Output

1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36
7 x 7 = 49
8 x 8 = 64
9 x 9 = 81
10 x 10 = 100

Example

$sum 0
from_to 1 10
  : (i)
    plus &sum i*i
    next
  -> !sum
writeln "sum: " sum

Output

sum: 385

Example

$sum 0
from_to 1 10
  : (i)
    plus &sum i*i
    if sum < 100 next break
  -> !sum
writeln "sum: " sum

Output

sum: 140