Next: workspace
Up: MacAnova Help File
Previous: vtx()
Contents
Usage:
while(Logical){statement1;statement2;...;}
|
Keywords:
syntax, control
while(Logical){statement1;statement2;....;} repeatedly executes the
statements enclosed in '{' and '}' as long as Logical has value True.
Logical should be a LOGICAL variable or expression, but not a constant
expression. Unless the last statement in {...} is empty (';;' just
before '}'), its value may be printed on every repetition.
The opening '{' must be on the same line as 'while' unless that line
terminates with '\'.
To avoid "infinite" loops, a 'while' loop will automatically terminate
after 1000 repetitions. This limit can be changed by option
'maxwhile'. After
Cmd> setoptions(maxwhile:10000)
a 'while' loop will terminate after 10000 repetitions.
It is essential that one of the statements modify the variable(s) used
in the LOGICAL expression, or that a break or breakall statement is
used. Otherwise the loop will repeated until 1000 (or value reset by
option 'maxwhile') repetitions are completed.
A 'while' statement does not have a value. Hence such constructs as yyy
<- while(n > 0){...} or zzz + while(n > 0){...} are illegal.
You can terminate a "while loop" prematurely using syntax elements
'break' and 'breakall' or pre-defined macro breakif().
You can skip to the end of a "while loop" using syntax element 'next'.
Be careful to modify the variable(s) used in the LOGICAL expression
before 'next' so as to avoid an "infinite" loop.
Examples:
Cmd> @s <- 0;@n <- length(x);while(@n>0){\
@s <- @s+x[@n];@n <- @n-1;;};@s
This would print the sum of all the elements in x.
Cmd> ex <- macro("@x <- argvalue($1,\"x\",\"real nonmissing\")
@dims <- dim(@x);@x <- vector(@x)
@neg <- 1-2*(@x<0);@x <- abs(@x);@eps <- 1e-12
@s <- @t <- @k <- 1
while(max(@t) > @eps*max(@s)){
@t <- (@x/@k)*@t
@s <- @s+@t;@k <- @k+1
}
array(@s^@neg,@dims)",dollars:T)
will create a macro ex() such that ex(x) computes exp(x) using a power
series, when x is a vector, matrix, or array. See topics
'macro_syntax', argvalue(), macro() for information about writing
macros.
See also topics 'for', 'if', 'break', 'breakall'.
Gary Oehlert
2003-01-15