Looping means repeating a statement or compound statement over and over until some condition is met.
There are three types of loops:
In Pascal, the fixed repetition loop is the for loop. The general
form is:
     for index :=
StartingLow to EndingHigh do
        statement;
The index variable must be of an ordinal data type. You can use
the index in calculations within the body of the loop, but you
should not change the value of the index. An example of using the
index is:
sum := 0; for count := 1 to 100 do sum := sum + count;The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (1+100 = 101. 2+99 = 101. See a pattern? There are 100 numbers, so the pattern repeats 50 times. 101*50 = 5050).
In the for-to-do loop, the starting value MUST be lower than the
ending value, or the loop will never execute! If you want to count
down, you should use the for-downto-do loop:
     for index :=
StartingHigh downto EndingLow do
        statement;
In Pascal, the for loop can only count in increments (steps) of 1.
![]() Previous lesson |
![]() Next lesson |
![]() Contents |
![]() Index |
![]() e-mail me |