Chapter 3 - More Program StructureContents:More LoopsThe For Loop and Conditional LoopsThe for loop executes a predetermined number of iterations which is defined by the values in <first> and <last>. Most loops do not have that predetermined nature. Their number of iterations may depend upon other things, such as the number of entries in a file or the number of times a player wishes to play a computer game. These are called conditional loops, where a test is performed during each iteration to determine whether to carry on, or jump out of the loop to the next part of the program.
The Repeat loopThe simplest of the conditional loops is the repeat.until loop. This syntax will literally repeat it's instructions until a certain condition is met:
<statement>;
repeat
Write( 'Do you want to stop yet? (Type "y" or "n")> ');
ReadLn( Response )
until (Response = "n") or (Response = "N");
<statement>;
The above loop will continue to execute until the user types upper or lower case n. SyntaxThe repeat loop has a very simple syntax. Because the repeat and until statements form their own bounds to the structure, there is no need to use begin and end as is usually the case. The <condition> at the end must follow the usual conditions for Boolean evaluation in just the same way as the conditions in the if statement seen previously.
{ Single statement Repeat...Until }
<statement>;
repeat
<statement>
until <condition>;
<statement>;
{ Multiple statement Repeat...Until }
<statement>;
repeat
<statement 1>;
<.
.>;
<statement n>
until <condition>;
<statement>;
Because the condition is at the end of the loop, this construct is used when the loop will always need to be executed at least once. The program must execute the lines of the repeat loop at least once in order to reach the condition at the end.
The While LoopThe while loop is also conditional, but this time the condition is at the beginning. The while loop is used in conditions where the loop may never need to be executed. If the condition is not met, the loop is skipped entirely.
<statement>;
while not EOF(DataFile) do
begin
ReadLn( DataFile, Number );
WriteLn( Number );
Total := Total + Number
end; { while }
<statement>;
The code above checks whether it is at the end of the input file DataFile before reading the next line. When the file is empty the loop is skipped and the next statement is executed. If the file was already empty, or already at the end, the loop would be skipped entirely. Syntax
{ Single statement while loop }
<statement>;
while <condition> do
<statement>;
<statement>;
{ Multiple statement while loop }
<statement>;
while <condition> do
begin
<statement 1>;
<.
.>;
<statement n>
end; { while }
<statement>;
Multiple statements are bounded by the usual begin and end, unlike the repeat loop above.
Chapter 3 ExercisesValidation RoutineValidation is a consideration in almost all programs. Design a program which prompts for and reads an integer number. To be valid, the number must be between 0 and 999 inclusive. If the number is outside the range, the program must respond to this and prompt for the number again (as many times as necessary). The validated number must then be echoed back to the user before termination. Code and test your design for the above problem. Which numbers did you use for testing and why? Solution
|