Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə57/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   53   54   55   56   57   58   59   60   ...   294
DelphiLanguageGuide

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 <= 9);
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 finalValuestatement is executed once more and the for
statement terminates. In other wordsstatement is executed once for every value in the range from initialValue to
finalValue. If initialValue is equal to finalValuestatement 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 <= finalValue do
    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);

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   53   54   55   56   57   58   59   60   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin