CodeRunner MPL Documentation

For In

Demonstrate how For IN loops work.
program TestForIn;
{$assertions ON}

var
  item:Int32;
  resStr:String;

//=================================================\\
const staticArr: Array [3..7] of Int32 = [0,1,2,3,4];
begin
  resStr := '';
  for item in staticArr do
    resStr := resStr + ToString(item);
  Assert(resstr = '01234');
  Writeln('Static Array Passed.');
end;


//=================================================\\
var dynArr:Array of Int32 = [0..4];
begin
  resStr := '';
  for item in dynArr do
    resStr := resStr + ToString(item);
  Assert(resstr = '01234');
  Writeln('Dynamic Array Passed.');
end;


//=================================================\\
begin
  resStr := '';
  for item in [0,1,2,3,4] do
    resStr := resStr + ToString(item);
  Assert(resStr = '01234');
  Writeln('Static Set Passed.');
end;


//=================================================\\
var
  c:Char;
  sstr:ShortString = 'shortstr';
begin
  resStr := '';
  for c in sstr do
    resStr := resStr + c;
  Assert(resStr = sstr);
  Writeln('Short String Passed.');
end;


//=================================================\\
var
  ch:Char;
  _str:String = 'string';
begin
  resStr := '';
  for ch in _str do
    resStr := resStr + ch;
  Assert(resStr = _str);
  Writeln('Huge String Passed.');
end;


//===| Special tests |============================\\
var evalCount:Int32=0;
function SideEffect(): Array of Int32;
begin
  Inc(evalCount);
  Result := [5,4,3,2,1,0];
end;

begin
  for item in SideEffect() do
    continue;
  Assert(evalCount = 1);
  Writeln('Function Result Passed.');
end;

//---| make sure locks are working |---\
var
  c1,c2,c3:Char;
  txt:string = 'abcdefg';
begin
  for c1 in txt do
    for c2 in txt do
      for c3 in txt do ;
  Assert((c1 = 'g') and (c2 = 'g') and (c3 = 'g'));
  Writeln('Nested Passed.');
end;
Output:
Static Array Passed.
Dynamic Array Passed.
Static Set Passed.
Short String Passed.
Huge String Passed.
Function Result Passed.
Nested Passed.