next up previous contents
Next: MAGMA functions and procedures Up: Simple exercises in MAGMA Previous: for loops   Contents

if then statements

There are basically two ``flavors'' of if then statements, similar to the MAPLE:

if {\it Boolean expression} then {\it statements} else {\it more statements} end if;

and

if {\it Boolean expression} then {\it statements} end if;

What is a ``Boolean expression''? It is usually a statement using the Boolean operators x and y, x or y, x xor y, not x, x in S, x eq y (for $x=y$), x ne y (for $x\not= y$), x ge y (for $x\geq y$), x le y (for $x\leq y$), and a few others.

For example,

> if 2^5 eq 32  then
if> print "they're equal";
if> else print "no they're not";
if> end if;
they're equal

and

> if 2^5 in [1..31]  then k:=0; else k:=1; end if;
> k;
1

Exercise 13   First, initialize a sequence $L$ to be empty by typing L:=[];. Next, construct a for loop over a variable x in the set $S$ of integers given by $S=\{-20,...,20\}$ which appends $x$ if it is odd.

Remark 2   In some cases, it appears that MAGMA wants you to type the ``if then'' statent on one line, the ``statements'' on the next line, and the ``end if'' on the last line. In other cases, having them all on one line is fine.



David Joyner 2001-08-22