For Loop Control (Stepper) Variables

Smart Pascal

You may have experienced already the syntax error "For loop control variable must be simple local variable" and then rectified the error by declaring the variable in the procedure containing the for loop. This page is prompted by the inclusion in DWScript (on which Smart Mobile Studio is based) around November 2013 of a handy syntax for the program where you declare in-line the stepper variable required by a for loop. See the simple example below.

unit StepperDemo;

interface

uses 
  W3System, W3Scroll, W3Console, W3Components, W3Application, W3ConsoleApp, W3Lists;

type
  TApplication = class(TW3CustomConsoleApplication)
  protected
    procedure PopulateConsole; override;
  end;

implementation

procedure TApplication.PopulateConsole;
begin
  for var a := 1 to 3 do
    Console.WriteLn(IntToStr(a));
end;    

Oxygene for Java

The syntax is slightly different in Oxygene for Java and you can control the step size. Here is an extract from our loops page:
for i : Int32 := 0 to 50 step 5 do 
  begin
    System.out.print(i);
    System.out.print(' ');
  end;    

Lazarus and Delphi

As far as we are aware, Lazarus and Delphi do not support the in-line declaration of for-loop control variables.

Programming - a skill for life!

Developments in Pascal, Oxygen for Java and Smart Mobile Studio