Previous Page Next Page Contents

repeat, while -- repeat and while loop

Introduction

repeat - end_repeat is a loop that evaluates its body until a specified stopping criterion is satisfied.

while - end_while represents a loop that evaluates its body while a specified condition holds true.

Call(s)


repeat
  body
until condition end_repeat _repeat(body, condition)


while condition do
  body
end_while _while(condition, body)

Parameters

body - the body of the loop: an arbitrary sequence of statements
condition - a Boolean expression

Returns

the value of the last command executed in the body of the loop. If no command was executed, the value NIL is returned. If the body of a while loop is not evaluated due to a false condition, the void object of type DOM_NULL is returned.

Further Documentation

Chapter 16 of the MuPAD Tutorial.

Related Functions

break, for, next, _lazy_and, _lazy_or

Details

Example 1

Intermediate results of statements within a repeat and while loop are not printed to the screen:

>> i := 1:
   s := 0:
   while i < 3 do
     s := s + i;
     i := i + 1;
   end_while
                                     3

Above, only the return value of the loop is displayed. Use print to see intermediate results:

>> i := 1:
   s := 0:
   while i < 3 do
     print("intermediate sum" = s);
     s := s + i;
     i := i + 1;
     s
   end_while
                          "intermediate sum" = 0
      
                          "intermediate sum" = 1
      
                                     3
>> delete i, s:

Example 2

A simple example is given, how a repeat loop can be expressed via an equivalent while loop. For other examples, this may be more complicated and additional initializations of variables may be needed:

>> i := 1:
   repeat 
     print(i);
     i := i + 1;
   until i = 3 end:
                                     1
      
                                     2
>> i := 1:
   while i < 3 do
     print(i);
     i := i + 1;
   end:
                                     1
      
                                     2
>> delete i:

Example 3

The Boolean expression condition must evaluate to TRUE or FALSE:

>> condition := UNKNOWN:
   while not condition do
     print(Condition = condition);
     condition := TRUE;
   end_while:
      Error: Unexpected boolean UNKNOWN [while]

To avoid this error, change the stopping criterion to condition <> TRUE:

>> condition := UNKNOWN:
   while condition <> TRUE do
     print(Condition = condition);
     condition := TRUE;
   end_while:
                            Condition = UNKNOWN
>> delete condition:

Example 4

We demonstrate the correspondence between the functional and the imperative form of the repeat and while loop, respectively:

>> hold(_repeat((statement1; statement2), condition))
                        repeat
                          statement1;
                          statement2
                        until condition end_repeat
>> hold(_while(condition, (statement1; statement2)))
                            while condition do
                              statement1;
                              statement2
                            end_while

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000