Program-Flow Control Statements

Like most programming languages, Mathics3 has common program-flow control statements for conditions, loops, etc.:


If[cond, pos, neg]

returns pos if cond evaluates to True, and neg if it evaluates to False.

Which[$cond_1$, $expr_1$, $cond_2$, $expr_2$, ...]

yields $expr_1$ if $cond_1$ evaluates to True, $expr_2$ if $cond_2$ evaluates to True, etc.

Do[expr, {i, max}]

evaluates expr max times, substituting i in expr with values from 1 to max.

For[start, test, incr, body]

evaluates start, and then iteratively body and incr as long as test evaluates to True.

While[test, body]

evaluates body as long as test evaluates to True.

Nest[f, expr, n]

returns an expression with f applied n times to expr.

NestWhile[f, expr, test]

applies a function f repeatedly on an expression expr, until
applying test on the result no longer yields True.

FixedPoint[f, expr]

starting with expr, repeatedly applies f until the result no longer changes.

Compound statements can be entered with ;. The result of a compound expression is its last part or Null if it ends with a ;.

Inside For, While, and Do loops, Break[] exits the loop, and Continue[] continues to the next iteration.

Precision and Accuracy
Scoping