control statements - axkr/symja_android_library GitHub Wiki
Like most programming languages, Symja has common control statements for conditions, loops, etc.:
If(cond, pos, neg)
returns
pos
ifcond
evaluates toTrue
, and neg if it evaluates toFalse
.
Which(cond1, expr1, cond2, expr2, ...)
yields
expr1
ifcond1
evaluates toTrue
,expr2
ifcond2
evaluates toTrue
, etc.
Do(expr, {i, max})
evaluates
expr
max
times, substitutingi
inexpr
with values from1
tomax
.
For(start, test, incr, body)
evaluates
start
, and then iterativelybody
andincr
as long astest
evaluates toTrue
.
While(test, body)
evaluates
body
as long astest
evaluates toTrue
.
Nest(f, expr, n)
returns an expression with
f
appliedn
times toexpr
.
NestWhile(f, expr, test)
applies a function
f
repeatedly on an expressionexpr
, until applyingtest
on the result no longer yieldsTrue
.
FixedPoint(f, expr)
starting with
expr
, repeatedly appliesf
until the result no longer changes.
>> If(2 < 3, a, b)
a
>> x = 3; Which(x < 2, a, x > 4, b, x < 5, c)
c
Compound statements can be entered with ;
.
The result of a compound expression is its last part or Null
if it ends with a ;
.
>> 1; 2; 3
3
>> 1; 2; 3;
Inside For, While, and Do loops, Break() exits the loop and Continue() continues to the next iteration.
>> For(i = 1, i <= 5, i++, If(i == 4, Break()); Print(i))
1
2
3