COMP203: Lecture 22

For loops and ifelse in Maple and Logo

For loops in Maple and Logo

In Logo, the instruction:

for [i 0 10 2] [print :i]

displays the even integers from 0 to 10 on the computer screen.

In Maple, the instruction: for i from 0 to 10 by 2 do i; od;

has the same effect.

Instead of Logo's list of controls [i 0 10 2], Maple uses keywords that are much more descriptive of what the for loop will do.

Where Logo uses square brackets [] to indicate the start and end of the block of instructions to repeat, Maple uses do and od.

Logo only displays values if they're input to print, show or type, while Maple evaluates expressions and displays the results by default.

Still, there are more similiarities than differences in the function of for loops in the two programming languages.

ifelse in Maple and Logo

We saw many examples of conditional evaluation like if and ifelse in Logo. For example:
ifelse equalp readlist [John Lennon] [print [You are right!]] [print [No silly, it's John Lennon.]]
Maple also has if and if/else statements, but again they look very different from the equivalent Logo commands. Some examples appear below:

In Maple, conditional evaluations always start with an if. Because we're using Maple for math, the true/false statement (predicate) will almost always be an equation or inequality. The thing that happens if the equation or inequality is true follows the word then. If you wish something different to happen only if the equation or inequality is false, that follows the word then.

The entire command ends with the words "end if". The command may be typed all on one line, or you may use shift-enter to use multiple lines.

Every instruction you give to Maple has an output; conditional evaluations like these are no exception. Try now to write an if/else statement in Maple whose output is x if x>0 and -x if x < 0.