Search code examples
delphidelphi-7

Nested Looping for Delphi 7


I've just learn Delphi 7 and need to make a nested loop with the following output :

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

To do that I use this code

procedure TForm1.Button1Click(Sender: TObject);
var a, b : integer;
begin
  for a := 5 downto 1 do
  begin
    for b := 5 downto a do
    begin
      label1.caption := label1.caption + inttostr(b);
    end;
  end;
end;

The best I can got is 554543543254321. I've tried to include #13#10 in the label1.caption to make a new line but it turn into this :

5
5
4
5
4
3
5
4
3
2
5
4
3
2
1

Anyone can help me?


Solution

  • You should add the #13#10 in the outer loop, not the inner one, i.e.:

    procedure TForm1.Button1Click(Sender: TObject);
    var a, b : integer;
    begin
      for a := 5 downto 1 do
      begin
        for b := 5 downto a do
        begin
          label1.caption := label1.caption + inttostr(b);
        end;
        label1.caption := label1.caption + '#13#10';
      end;
    end;