next up previous contents
Next: Breaking execution Up: The language Previous: Redirecting execution

Loops

The following example prints the squares of the numbers 1 to 10 in reverse order. It is implemented in three different ways to illustrate the use of the different kinds of loops:

LC Session

lc> i = 10; 
$44 = 10
lc> while (i > 0) { print(i^2, " "); i--; } print("\n");
100 81 64 49 36 25 16 9 4 1 
lc> for (i = 10; i > 0; i--) print(i^2, " "); print("\n");
100 81 64 49 36 25 16 9 4 1 
lc> i = 10;
$45 = 10
lc> do { print(i^2, " "); i--; } while (i > 0); print("\n");
100 81 64 49 36 25 16 9 4 1 
lc>



Thomas Papanikolaou
Thu Aug 28 10:25:48 MET DST 1997