Making Loops:

For-Do, While-Do, Repeat-Until


Writing computer code is a slow and painful process. One way that we
get more good out of the code that we write is by repeating the same
code over and over. In fact, repetative processes are one of the things
that computers are very good at. The FOR-DO is one of three ways of
writing a loop.


The next program is very similar to the first one except that I have
added an IF-THEN to make the output look nicer. You will also see that
in order to put more statements inside of the loop I had to add a BEGIN-
END block. It is so common to want to do several things inside of a FOR
loop that some programmers always write a BEGIN-END after the FOR-
DO. Can you figure out what the output will look like before you run the
program? This also illustrates that the programmer has to work at it if
she wants the computer to generate output that looks like real English


Here is a new trick -- a FOR loop inside of a FOR loop. This can generate
a lot of program activity since the inner loop runs completely for each
time through the outer loop. In this example the inner loop "for times
:=" runs three times each of the times through the outer "for verses :="
loop. Since the outer loop runs three times, by the time the program is
done the inner loop has run nine times.
Imagine what would happen if you put a third loop inside the second
loop! Can you do it? The words to put in are "fa la."


Here is the first program again except that I have replaced the FOR-DO
with a WHILE-DO. You can see that now I have to write separate
statements to set "times" equal to one and to add one to "times" each
time through the loop.

The first program doesn't need a BEGIN-END block but this one does --
can you figure out why?


Just in case you aren't sick of this song yet I've done it again. This time
I used the REPEAT-UNTIL. I had to change the condition that is tested
from what I used in the program with the WHILE. Can you see what I
changed and can you tell why? And look -- the BEGIN-END has
disappeared! Why?

Rules for writing Pascal statements

General Index to Sample Programs

[ Home ][ Vita ][ Copyright  ][ Copyright  ]