Next: fprint() Up: MacAnova Help File Previous: floor()   Contents

for

Usage:
for(i,vec)){command1;command2; ...}, vec a REAL vector
for(i,start,end [, incr]){command1;command2;...}, start, end and incr
  REAL scalars
for(i,NULL){...}



Keywords: syntax, control
for(Index,Range){statement1;statement2;... } where Range is a REAL
vector of length N repeats the statements in {...} N times with Index
successively taking the values Range[1], Range[2], ..., Range[N].

Unless the last statement in {...} is NULL (';;') its value may be
printed on each loop.  The most common form for Range is 'run(n)' which
results in the statements in {...} being repeated n times, with variable
Index taking values 1, 2, ..., n.

for(Index,NULL){...} skips the compound statement {...} entirely.  An
example might be
  Cmd> for(i, run(length(x))[x>10]){print(paste("x[",i,"] > 10"))}
when max(x) < 10.  In this case run(length(x))[x>10] is NULL.  See
topics 'subscripts' and 'NULL'.

for(Index,i1,i2){...} is equivalent to for(Index,run(i1,i2)){...}.

for(Index,i1,i2,incr){...} is equivalent to for(Index,
run(i1,i2,incr)){...}.

A 'for' statement does not have a value.  Hence such constructs as
  yyy <- for(i,run(3)){i+2} or 4 + for(i,run(3)){i+2}
are illegal.

You can terminate a "for loop" prematurely using syntax elements 'break'
and 'breakall' or pre-defined macro breakif().

You can skip to the end of a "for loop" using syntax element 'next'.

Example:
  Cmd> @n <- length(a);@s <- 0;for(i,run(@n)){@s <- @s+(a[i]-1)^2;;}; @s
is essentially equivalent to sum((vector(a)-1)^2).  'for(i,run(@n))'
could be replaced by 'for(i,1,@n)' or 'for(i,1,@n,1)'.

The following might be better:
  Cmd> @s <- 0; for(@ai,vector(a)){@s <- @s + (@ai - 1)^2;;}; @s

The opening '{' after 'for(...)' must be on the same line as 'for'.

See also topics 'while', 'break', 'breakall', breakif(), 'next'.


Gary Oehlert 2003-01-15