The repeat in a loop can be modified by adding one or more while clauses. Each clause contains a predicate immediately following the while keyword. The predicate is tested before the evaluation of the body of the loop. The loop body is evaluated whenever the predicate in a while clause is true.
The syntax for a simple loop using while is
while predicate repeat loopbody
The predicate is evaluated before loopbody is evaluated. A while loop terminates immediately when predicate evaluates to false or when a leave or return expression is evaluted. See )help repeat for more information on leave and return.
Here is a simple example of using while in a loop. We first initialize the counter.
i := 1
1
Type: PositiveInteger
while i < 1 repeat
output "hello"
i := i + 1
Type: Void
The steps involved in computing this example are
(1) set i to 1
(2) test the condition i < 1 and determine that it is not true
(3) do not evaluate the loop body and therefore do not display "hello"
(x, y) := (1, 1)
1
Type: PositiveInteger
If you have multiple predicates to be tested use the logical and operation to separate them. FriCAS evaluates these predicates from left to right.
while x < 4 and y < 10 repeat
output [x,y]
x := x + 1
y := y + 2
[1,1]
[2,3]
[3,5]
Type: Void
A leave expression can be included in a loop body to terminate a loop even if the predicate in any while clauses are not false.
(x, y) := (1, 1)
1
Type: PositiveInteger
while x < 4 and y < 10 repeat
if x + y > 7 then leave
output [x,y]
x := x + 1
y := y + 2
[1,1]
[2,3]
Type: Void