next up previous contents
Next: Using if-then statements Up: Lists, sequences, via for Previous: Lists, sequences, via for   Contents

Using for loops

Another way to define a list is to use a for loop:

> L:=[];
> for i in [1..10] do
for> L[i]:=i^2;
for> end for;
> L;
[ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]
The first line L:=[]; is to initialize L. If omitted, MAGMA will give an error after you enter the line for> L[i]:=i^2;. The append command also works:
> L:=[];
> for i in [1..10] do
for> L:=Append(L,i^2);
for> end for;
Using the ``tilde'' notation, a similar command produces the same result:
> L:=[];
> for i in [1..10] do
for> Append(~L,i^2);
for> end for;

Exercise 4   Create the list of all integers of the form $n^2+1$, $1\leq n\leq 20$.



David Joyner 2001-08-22