Home Up Contents Search

For loop

The FOR loop is for situations in which a statement or series of statements must be repeated 0 or more times--the exact number of times being known before the loop begins. In contrast, loops based on the WHILE and REPEAT/UNTIL statements are better suited to circumstances in which the terminating condition must be calculated in the loop (that is, in which the number of times to repeat isn't known in advance).

Syntax:

FOR <ordinalVar> := <ordinalExpr> {TO|DOWNTO} <ordinalExpr> DO
<statement>


For example, this loop outputs 'Hello' ten times (N is an integer variable):

for N := 1 to 10 do Writeln('Hello');


The DOWNTO keyword causes the counter variable to be decremented with each pass, as in this program:

program ForDemo;

uses Win32Crt;

var N : integer;

begin

Write('Ignition sequence has commenced ...');

for N := 10 downto 1 do

begin

Write(N, '...');
Delay(1000);

end;

Writeln('Blastoff!');

end. { ForDemo }

 
 
Previous Next
Send mail to ljschwerin@hotmail.com with questions or comments about this web site.

Copyright © 1999-2002 Leon Schwerin
Last modified: 26 March 2000
1