%mathpiper,def="For"

/* Defining a For function */
TemplateFunction("For",{start,predicate,increment,body})
[
  Eval(start);
  While (IsEqual(Eval(predicate),True))
  [
    Eval(body);
    Eval(increment);
  ];
];
UnFence("For",4);
HoldArgumentNumber("For",4,1);
HoldArgumentNumber("For",4,2);
HoldArgumentNumber("For",4,3);
HoldArgumentNumber("For",4,4);

%/mathpiper



%mathpiper_docs,name="For",categories="User Functions;Control Flow"
*CMD For --- C-style {for} loop
*STD
*CALL
	For(init, pred, incr) body

*PARMS

{init} -- expression for performing the initialization

{pred} -- predicate deciding whether to continue the loop

{incr} -- expression to increment the counter

{body} -- expression to loop over

*DESC

This commands implements a C style {for} loop. First
of all, the expression "init" is evaluated. Then the predicate
"pred" is evaluated, which should return {True} or
{False}. Next the loop is executed as long as the
predicate yields {True}. One traversal of the loop
consists of the subsequent evaluations of "body", "incr", and
"pred". Finally, the value {True} is returned.

This command is most often used in a form such as {For(i=1, i<=10, i++) body}, which evaluates {body} with
{i} subsequently set to 1, 2, 3, 4, 5, 6, 7, 8, 9,
and 10.

The expression {For(init, pred, incr) body} is
equivalent to {init; While(pred) [body; incr;]}.

*E.G. notest

In> For (i:=1, i<=10, i++) Echo({i, i!});
	 1  1
	 2  2
	 3  6
	 4  24
	 5  120
	 6  720
	 7  5040
	 8  40320
	 9  362880
	 10  3628800
Result: True;

*SEE While, Until, ForEach, Break, Continue
%/mathpiper_docs