Using Iteration (Repetition) in Smart Pascal

Introduction

There are three types of program construct:
  • sequence;
  • selection;
  • iteration (repetition).
A sequence is simply a group of statements executed in the order they appear in the source program. We have seen that if statements and case statements perform selection. Iteration can be achieved by a:
  • for loop;
  • repeat loop;
  • while loop.
Each type of loop has its own characteristics:
  • A for loop is used when the number of iterations is known in advance.
  • A repeat loop carries on repeating until some condition is satisfied, so must run at least once.
  • A while loop tests a condition at the start of a loop and only continues while the condition is true. If the condition is false at the outset then the rest of the code in the loop will not be executed at all.

The following demonstration shows an example of each type of loop. Iteration is often used with arrays, which we cover on the next page.

Demonstration

unit Unit1;

interface

uses 
  System.Types, SmartCL.System, SmartCL.Components, SmartCL.Application,
  SmartCL.Game, SmartCL.GameApp, SmartCL.Graphics;

type
  TCanvasProject = class(TW3CustomGameApplication)
  protected
    procedure ApplicationStarting; override;
    procedure ApplicationClosing; override;
    procedure PaintView(Canvas: TW3Canvas); override;
  end;

implementation

procedure TCanvasProject.ApplicationStarting;
begin
  inherited;
  GameView.Delay := 20;
  GameView.StartSession(True);
end;

procedure TCanvasProject.ApplicationClosing;
begin
  GameView.EndSession;
  inherited;
end;
 
procedure TCanvasProject.PaintView(Canvas: TW3Canvas);
begin
  // Clear background
  Canvas.FillStyle := 'rgb(0, 0, 99)';
  Canvas.FillRectF(0, 0, GameView.Width, GameView.Height);
  //Draw four horizontal red lines separated by 100 pixels
  Canvas.StrokeStyle := 'red';
  Canvas.BeginPath;
  for var i := 1 to 4 do
    begin
      Canvas.LineF(0, i * 100,  GameView.Width, i * 100);
    end;
  Canvas.Stroke;
  Canvas.ClosePath;
  // Draw 10 grey steps
  Canvas.StrokeStyle := 'gray';
  Canvas.BeginPath;
  for var i := 1 to 10 do
    begin
      Canvas.LineF((i - 1) * 50 , i * 50,  i * 50, i * 50);
      Canvas.LineF(i * 50 , i * 50,  i * 50, (i + 1) * 50);
    end;
  Canvas.Stroke;
  Canvas.ClosePath;
  // Draw green vertical lines separated by 100 pixels
  // stopping short of half the GameView width.
  Canvas.StrokeStyle := 'green';
  Canvas.BeginPath;
  var xPos = 100;
  while xPos < (GameView.Width / 2) do
    begin
      Canvas.LineF(xPos, 0, xPos, GameView.Height);
      xPos += 100;
    end;
  Canvas.Stroke;
  Canvas.ClosePath;
  // Draw white vertical lines separated by 100 pixels
  // on the rest of the GameView.
  Canvas.StrokeStyle := 'white';
  Canvas.BeginPath;
  repeat
    Canvas.LineF(xPos, 0, xPos, GameView.Height);
    xPos += 100;
  until xPos >= GameView.Width;
  Canvas.Stroke;
  Canvas.ClosePath;
end;

end.    
Programming - a skill for life!

How to learn the Pascal language the fun way by making games in Smart Mobile Studio