Using Variables & For Loops in Delphi

By: Borland Staff

Abstract: Using Variables & For Loops in Delphi to cause a piece of code to get executed a certain number of consecutive iterations based on certain criteria.

Problem:
Using Variables & For Loops in Delphi to cause a piece of code to get executed a certain number of consecutive iterations based on certain criteria.

Solution:
The information in this article applies to:
* All versions of Delphi

1. From the Standard tab of the Component Pallette drop a Button & a Memo Field on the Form.

2. Enlarge the Memo Field vertically by dragging the top & bottom edges.

3. Double click the Button to open the Event Handler.

4. Enter the code as shown in the procedure body:

procedure TForm1.Button1Click(Sender: TObject);  // this line is pre-coded in the event handler

var  {this declares a section of code where you can define variables}
I : Integer; {define 'I' as an Integer variable}

Begin // This line is pre-coded in the event handler

Memo1.Lines.Clear; {clears  characters previously displayed in memo field}

For I := 0 to 5 do  {incremental 'For Loop'}
  Memo1.Lines.Add('This is iteration ' + IntToStr(I));  {display string}
{'IntToStr' converts the increment variable from an integer to a string so that it can be displayed}

Memo1.Lines.Add(' ');  {display blank line}

For I := 5 downto 0 do  {decremental 'For Loop'}
  Memo1.Lines.Add('This is iteration ' + IntToStr(I));
  
end;

end.


5. Click the 'Run' button on the Toolbar (or press F9).


Server Response from: ETNASC02