Download Maple Introductory Programming Guide

Transcript
5.2 Repetition
•
177
> L := [7,2,5,8,7,9];
L := [7, 2, 5, 8, 7, 9]
> for i in L do
> if i <= 7 then
>
print(i)
> end if;
> end do;
7
2
5
7
This code cycles through the operands of object L, in this case, a list.
The object can be, for example, a set, sum of terms, product of factors,
or string of characters.
The while Loop
The for loop is used to repeatedly execute a sequence of statements until
a condition is satisfied. The while loop is a for loop with all of its clauses
omitted except the while clause.
while expr do
statseq
end do
The expression expr is called the while condition. It must be a
Boolean-valued expression. That is, it must evaluate to true, false, or
FAIL.
> x := 256;
x := 256
> while x>1 do
>
x := x/4
> end do;