expression1; while (expression2) { statement; expression3; }
Again, termination of the loop can be caused by the execution of a break statement.
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> |