# CS 121 Lab 2 Summary for Fall 2006

Key TopicsSkills to MasterMaple CommandsExamples of Use
Conditional StatementsUnderstand and write Maple expressions which evaluate to true or false.(see ?boolean help page)i = 1;
j <= 2;
(0 < x) and (x <= 1);
x := Pi;
type(x, realcons);
Understand and write "if" statements, including those with "elif" and "else" clauses.ify := -1;
if y = 0 then
print("zero")
elif y > 0 then
print("positive")
else
print("negative")
end if;
LoopsWrite simple "for" loops with a fixed number of iterations.forfor i from 1 to 10 do
printf("%d^2 = %d\n", i, i^2);
end do;
Write nested "for" loops, including those with a variable number of iterations.forfor i from 1 to 5 do
for j from i+1 to 5 do
print(i < j);
end do;
end do;
ListsCreate a list data structure in Maple and extract a single element from the list.(see ?list help page)L := [a,b,c];
L[2];  # second element
Determine the number of elements in a list.nopsnops(L);
Extract all the elements of a list.opop(L);
Append a new element to the end of a list.(see ?list help page)L := [op(L), d];  # append d
Generate the elements of a list automatically using a formula.seqsquares := [seq(i^2, i=1..10)];
Random NumbersCreate a random number generator in Maple.randcoin := rand(0..1);
Generate a list of random numbers using a sequence.seqtosses := [seq(coin(), i=1..100)];
Generate a list of random numbers using a loop.fortosses := [];
for i from 1 to 100 do
tosses := [op(tosses), coin()];
end do:  # suppress loop output
tosses;

Created on 11/8/2006 by Jeremy Johnson () and Frederick W. Chapman ().