[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

13.2.10 Loops

(This message will disappear, once this node revised.)

Two looping constructs are provided: while and do...while.

While loop

The syntax of a while loop is:

 
while cond
  stmt

Newline after cond is obligatory.

Do...while loop

 
do
  stmt
while cond

As usual do...while loop differs from its while counterpart in that its stmt is executed at least once.

The looping constructs can be nested to any depth.

Two special statements are provided for branching within loop constructs. These are break and continue.

Break statement stops the execution of the current loop statement and passes control to the statement immediately following it

 
while $x < 10
begin
  if $x < $y
     break
  ...
  x = $x + 1
end
print "OK\n"

In the example above, execution of break statement passes control to print statement.

Break may also take an argument: a literal number representing the number of nested loop statements to break from. For example, the break statement in the sample code below will exit from the outermost while:

 
while $y < 10
begin
  while $x < 10
  begin
    if $x < $y
       break 2
    ...
    x = $x + 1 
  end
  ...
  y = $y + 1 
end  
print "OK\n"

Continue statement passes control to the condition of the current looping construct. When used with a numeric argument, the latter specifies the number of the nesting looping construct to pass control to (as with break, the innermost loop is considered to have number 1, so continue is equivalent to continue 1).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Sergey Poznyakoff on November, 20 2004 using texi2html