Documentation

while, end_while, _while

"while" loop

For the while loop in MATLAB®, see while.

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

while condition do
  body
end_while

_while(condition, body)

Description

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

In a while loop, condition is evaluated before the body is executed for the first time. If condition evaluates to TRUE, the loop is entered and body and condition are evaluated until condition evaluates to FALSE.

The body can consist of any number of statements which must be separated either by a colon : or a semicolon ;. Only the last evaluated result inside the body (the return value of the loop) is printed on the screen. Use print to see intermediate results.

The Boolean expression condition must be reducible to either TRUE or FALSE. Internally, the condition is evaluated in the lazy evaluation context of the functions _lazy_and and _lazy_or.

The statements next and break can be used in while loops in the same way as in for loops.

The keyword end_while can be replaced by the keyword end.

The imperative form while - end_while is equivalent to corresponding call of the function _while. In most cases, the imperative form leads to simpler code.

The $ operator is often a more elegant notation for loops.

_while is a function of the system kernel.

Examples

Example 1

while loops do not show intermediate results of statements within a loop:

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

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

delete i, s:

Example 2

Express the same statements as a repeat loop and as an equivalent while loop. In more complicated cases, you might need additional initializations of variables:

i := 1:
repeat 
  print(i);
  i := i + 1;
until i = 3 end:

i := 1:
while i < 3 do
  print(i);
  i := i + 1;
end:

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: The Boolean 'TRUE' or 'FALSE' is expected. [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:

delete condition:

Example 4

You also can create a while loop by using the functional form _while:

hold(_while(condition, (statement1; statement2)))
while condition do
  statement1;
  statement2
end_while

Parameters

body

The body of the loop: an arbitrary sequence of statements

condition

A Boolean expression

Return Values

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.

See Also

MuPAD Functions

More About

Was this topic helpful?