while
Evaluates an expression as long as a different expression evaluates to true
.
Syntax
-
while(cond, expr)
-
cond
is an expression -
expr
is an expression
-
Description
while(cond, expr)
evaluates expr
repeatedly as long as cond
evaluates to true
. More precisely, while(cond, expr)
first evaluates cond
. If the result is false
, then while
returns. Otherwise, expr
is evaluated. Then the process repeats, until cond
returns false
. while
always returns null
; hence, it is used only for its side effects.
Examples
f ≔ n ↦ if(even(n), n/2, 3⋅n + 1); st ≔ n ↦ (c ≔ 0; k ≔ n; while(k ≠ 1, (k ≔ f(k); inc(c))); c); WordWrap(join(SequenceList(100) @ st))
0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 112, 112, 19, 32, 19, 32, 19, 19, 107, 107, 6, 27, 27, 27, 14, 14, 14, 102, 22, 115, 22, 14, 22, 22, 35, 35, 9, 22, 110, 110, 9, 9, 30, 30, 17, 30, 17, 92, 17, 17, 105, 105, 12, 118, 25, 25, 25