Turbo Pascal 7.0 Guide
Iteration
So far, the code that you have written runs only once. There are obviously going to be occasions when you want sections of code to be repeated. There are several ways to achieve this in Pascal.
The While Statement
WHILE Boolean expression DO statement
The while statement executes a block of code providing that the expression evaluates to true.
Example - Adding Numbers
PROGRAM addup;
USES crt;
VAR x, total: integer;
BEGIN
clrscr;
total:=0;
write('Input next number ');
readln(x);
WHILE x<999 DO
BEGIN
total:= total+x;
write('Input next number ');
readln(x);
END;
writeln;
writeln('The total is ', total);
readln;
END.
This program instructs the user to enter numbers until they enter the number 999. At this point, the total of the numbers entered is output to the screen.
The while statement is a compound statement. Like with block if statements, the statements BEGIN and END; mark the start and end of the code to repeat.
The Repeat - Until Statement
REPEAT
statement 1;
statement 2;
UNTIL Boolean expression;
The repeat statement, executes a block of code until the expression evaluates to true.
Example Adding Numbers 2
PROGRAM addup2;
USES crt;
VAR x, total: integer;
BEGIN
clrscr;
total:=0;
write('Input next number ');
readln(x);
REPEAT
total:= total+x;
write('Input next number ');
readln(x);
UNTIL x=999;
writeln;
writeln('The total is ', total);
readln;
END.
The FOR Statement
FOR control variable := initial value TO final value DO
statement;
FOR control variable := initial value DOWNTO final value DO
statement;
The control variable, initial value and final value must be of the same type. For statements or loops are useful when you know exactly how many times you want to repeat a section of code.
The value of the control variable is incremented (or decremented in the second example) until it reaches the final value. The block of code is executed each time.
Example - 5 times table
PROGRAM tables;
USES crt;
VAR a,b: integer;
BEGIN
clrscr;
FOR a:=1 TO 10 DO
BEGIN
b:= a*5;
writeln(a:3, ' x 5 = ', b);
END;
readln;
END.
Example - Adding Numbers 3
PROGRAM addup3;
USES crt;
VAR x, i, k, total: integer;
BEGIN
clrscr;
total:=0;
write('How many numbers do you want to enter? ');
readln(k);
FOR i:= 1 TO k DO
BEGIN
write('Input next number ');
readln(x);
total:=total +x;
END;
writeln;
writeln('The total is ', total);
readln;
END.
Example - Factorials
PROGRAM factorial;
VAR i, n: integer;
VAR f: longint;
BEGIN
write('Input a number between 1 and 15 ');
readln(n);
f:=n;
FOR i:= n-1 DOWNTO 1 DO
f:= f*i;
writeln(n, '! is ', f);
readln;
END.
The definition of 3! is 3x2x1. The longint data type is used to store larger integer values. Factorials get very large very quickly - 15! is about the maximum value that can be stored.
There are no begin and end statements with this loop because only one line of code is executed.
Exercises
- Write a program which allows the user to input a series of numbers, entering 0 when they want to stop. Output the total and mean average of the numbers entered.
- Write a program to output the 8 times table.
- Write a program that outputs 6 random lottery numbers for the national lottery. Modify the program to produce three sets of numbers.
- Write a program that generates a random number between 1 and 100. Allow the user to keep guessing the number until they have guessed the random number. If the enter a number lower than the number generated, tell them that their number is too low. Do the same if they enter a number higher than the number generated. Once they have guessed the number, tell them how many guesses they entered.
- Write a program that lists the numbers 1 to 20 along with their squares, cubes and square roots.
- Write a program that allows the user to input 10 scores. At the end of the program, output the total, mean average, highest and lowest numbers.