Department of Mathematics

Math 300: Mathematical Computing

Maple Flow Control

We know what tools we need from the Matlab page. The difference between Matlab and Maple flow control is generally that Maple is a bit more verbose than Matlab.

If

The basic syntax is

if conditional then statement sequence;
elif conditional then statement sequence;
else statement sequence;
end if;
As always, the elif and else are optional.

The conditionals have a similar form to those in other languages. There are a few differences. First, conditionals in Maple do not have a truth value outside of the "if" statement context. In other words, the expression "5>2" has no truth value in Maple, but if 5>2 then c:=3; end if; has meaning. If you want to find the truth value of a conditional outside of an "if" statement, you must use the evalb function. Here is an example.

5=3+2; 5 = 5 evalb(5=3+2); true if 5>2 then x:=3; elif 5=2 then x:=103; end if; x := 3

It is worth a special note that Maple uses := for the assignment operator, which frees up = to be used to test equality. This differs from many other languages, which typically use == for that purpose. A table summarizing logical operators follows.

SymbolMeaning
=Is Equal To
<>Is Not Equal To
>Is Greater Than
>=Is Greater Than Or Equal To
<Is Less Than
<=Is Less Than Or Equal To
andLogical And
orLogical Or

For

Maple has a for loop construction, just like every other language. The syntax is a bit different, however.
for variable from expression to expression do
statement sequence
end do;
You will find this to be fairly intuitive.

for i from 1 to 10 x[i] := i: end do; y := 0: for j from x[3] to x[3]^2 do y := y+j: end do; the_answer_to_life_the_universe_and_everything:=y; the_answer_to_life_the_universe_and_everything:=42

Note that the task of the first loop could have been done more easily using
x:=[seq(i,i=1..10)];
















Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2020 Kevin Cooper