[ Pobierz całość w formacie PDF ]
when the execution stops. Delphi has three kinds of control loop: repeat statements, while statements, and for
statements.
You can use the standard Break and Continue procedures to control the flow of a repeat, while, or for statement.
Break terminates the statement in which it occurs, while Continue begins executing the next iteration of the
sequence.
Repeat Statements
The syntax of a repeat statement is
repeatstatement1; ...; statementn;untilexpression
where expression returns a Boolean value. (The last semicolon before until is optional.) The repeat statement
executes its sequence of constituent statements continually, testing expression after each iteration. When
expression returns True, the repeat statement terminates. The sequence is always executed at least once because
expression is not evaluated until after the first iteration.
Examples of repeat statements include
repeat
K := I mod J;
I := J;
J := K;
until J = 0;
repeat
Write('Enter a value (0..9): ');
Readln(I);
until (I >= 0) and (I
While Statements
A while statement is similar to a repeat statement, except that the control condition is evaluated before the first
execution of the statement sequence. Hence, if the condition is false, the statement sequence is never executed.
The syntax of a while statement is
whileexpressiondostatement
39
where expression returns a Boolean value and statement can be a compound statement. The while statement
executes its constituent statement repeatedly, testing expression before each iteration. As long as expression returns
True, execution continues.
Examples of while statements include
while Data[I] X do I := I + 1;
while I > 0 do
begin
if Odd(I) then Z := Z * X;
I := I div 2;
X := Sqr(X);
end;
while not Eof(InputFile) do
begin
Readln(InputFile, Line);
Process(Line);
end;
For Statements
A for statement, unlike a repeat or while statement, requires you to specify explicitly the number of iterations you
want the loop to go through. The syntax of a for statement is
forcounter := initialValuetofinalValuedostatement
or
forcounter := initialValuedowntofinalValuedostatement
where
counter is a local variable (declared in the block containing the for statement) of ordinal type, without any
qualifiers.
initialValue and finalValue are expressions that are assignment-compatible with counter.
statement is a simple or structured statement that does not change the value of counter.
The for statement assigns the value of initialValue to counter, then executes statement repeatedly, incrementing or
decrementing counter after each iteration. (The for...to syntax increments counter, while the for...downto syntax
decrements it.) When counter returns the same value as finalValue, statement is executed once more and the for
statement terminates. In other words, statement is executed once for every value in the range from initialValue to
finalValue. If initialValue is equal to finalValue, statement is executed exactly once. If initialValue is greater than
finalValue in a for...to statement, or less than finalValue in a for...downto statement, then statement is never executed.
After the for statement terminates (provided this was not forced by a Break or an Exit procedure), the value of
counter is undefined.
For purposes of controlling execution of the loop, the expressions initialValue and finalValue are evaluated only
once, before the loop begins. Hence the for...to statement is almost, but not quite, equivalent to this while
construction:
begin
counter := initialValue;
while counter
begin
... {statement};
40
counter := Succ(counter);
end;
end
The difference between this construction and the for...to statement is that the while loop reevaluates finalValue before
each iteration. This can result in noticeably slower performance if finalValue is a complex expression, and it also
means that changes to the value of finalValue within statement can affect execution of the loop.
Examples of for statements:
for I := 2 to 63 do
if Data[I] > Max then
Max := Data[I];
for I := ListBox1.Items.Count - 1 downto 0 do
ListBox1.Items[I] := UpperCase(ListBox1.Items[I]);
for I := 1 to 10 do
for J := 1 to 10 do
begin
X := 0;
for K := 1 to 10 do
X := X + Mat1[I,K] * Mat2[K,J];
Mat[I,J] := X;
end;
for C := Red to Blue do Check(C);
Iteration Over Containers Using For statements
Both Delphi for .NET and for Win32 support for-element-in-collection style iteration over containers. The
following container iteration patterns are recognized by the compiler:
for Element in ArrayExpr do Stmt;
for Element in StringExpr do Stmt;
for Element in SetExpr do Stmt;
for Element in CollectionExpr do Stmt;
The type of the iteration variable Element must match the type held in the container. With each iteration of the loop,
the iteration variable holds the current collection member. As with regular for-loops, the iteration variable must be
declared within the same block as the for statement. The iteration variable cannot be modified within the loop. This
includes assignment, and passing the variable to a var parameter of a procedure. Doing so will result in a compile-
time error.
Array expressions may be single or multidimensional, fixed length, or dynamic arrays. The array is traversed in
increasing order, starting at the lowest array bound and ending at the array size minus one. The code below shows
an example of traversing single, multi-dimensional, and dynamic arrays:
type
TIntArray = array[0..9] of Integer;
TGenericIntArray = array of Integer;
var
IArray1: array[0..9] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IArray2: array[1..10] of Integer = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
41
IArray3: array[1..2] of TIntArray = ((11, 12, 13, 14, 15, 16, 17, 18, 19, 20),
(21, 22, 23, 24, 25, 26, 27, 28, 29, 30));
MultiDimTemp: TIntArray;
IDynArray: TGenericIntArray;
I: Integer;
begin
for I in IArray1 do
begin
// Do something with I...
end;
// Indexing begins at lower array bound of 1.
for I in IArray2 do
begin
// Do something with I...
end;
// Iterating a multi-dimensional array
for MultiDimTemp in IArray3 do // Indexing from 1..2
for I in MultiDimTemp do // Indexing from 0..9
begin
// Do something with I...
end;
// Iterating over a dynamic array
IDynArray := IArray1;
for I in IDynArray do
begin
// Do something with I...
end;
The following code example demonstrates iteration over string expressions:
var
C: Char;
S1, S2: String;
Counter: Integer;
OS1, OS2: ShortString;
AC: AnsiChar;
begin
S1 := 'Now is the time for all good men to come to the aid of their country.';
S2 := '';
for C in S1 do
S2 := S2 + C;
if S1 = S2 then
WriteLn('SUCCESS #1');
else
WriteLn('FAIL #1');
OS1 := 'When in the course of human events it becomes necessary to dissolve...';
OS2 := '';
42
for AC in OS1 do
OS2 := OS2 + AC;
if OS1 = OS2 then
WriteLn('SUCCESS #2');
else
WriteLn('FAIL #2');
end.
The following code example demonstrates iteration over set expressions:
type
TMyThing = (one, two, three);
TMySet = set of TMyThing;
TCharSet = set of Char;
var
MySet: TMySet;
MyThing: TMyThing;
CharSet: TCharSet;
{$IF DEFINED(CLR)}
C: AnsiChar;
{$ELSE}
C: Char;
{$IFEND}
begin
MySet := [one, two, three];
for MyThing in MySet do
begin
// Do something with MyThing...
end;
CharSet := [#0..#255];
for C in CharSet do
begin
// Do something with C...
end;
end.
To use the for-in loop construct on a class, the class must implement a prescribed collection pattern. A type that
implements the collection pattern must have the following attributes:
The class must contain a public instance method called GetEnumerator(). The GetEnumerator() method
must return a class, interface, or record type.
The class, interface, or record returned by GetEnumerator() must contain a public instance method called
MoveNext(). The MoveNext() method must return a Boolean.
The class, interface, or record returned by GetEnumerator() must contain a public instance, read-only
[ Pobierz całość w formacie PDF ]