10.8 Loop Constructs

10.8.1 for Loop

This construct is used for the formulation of loops, if the number of runs is defined before the entry in the loop.

Syntax

for <counter> := <start> to <end> { step <increment> } do
  <statements>
endfor

At the loop start, the value <start> is assigned to <counter>. After each loop run, <increment> is added to <counter>. If <increment> was not given, the default increment 1 is added. The loop runs until <counter> becomes larger than <end>. At each loop run, the <statements> are performed which can access the current value of <counter>. <start>, <end> and <increment> have to be integer values. <counter> has to be able to accept integer values. <start>, <end> and <increment> are calculated only once before the first loop run.

Example

for Index := 1 to Window.childcount do
  print Window.child[Index];
endfor

It is not possible to cancel a for loop before it is terminated.

If the cancel criteria cannot be reached at for loops, the loop will not be carried out.

Example

for I := 1 to 10 step -1 do

10.8.2 foreach Loop

This construct is used to create loops that iterate over all elements of a collection.

Syntax

foreach <item> in <expression> do
  <statements>
endfor

The loop terminates with an error if the <expression> is not a collection or the loop value cannot be assigned to the <item>. The <expression> is evaluated only once initially. The control variable <item> may be changed within the <statements>. A change of the control variable does not affect the number of cycles or the value of the control variable in the next cycle.

Example

dialog D
window Wi
{
  listbox Lb
  {
    .xauto 0;  .yauto 0;
  }
  on close { exit(); }
}

on dialog start
{
  variable list StationList:= ["ABC", "CBS", "NBC", "ESPN"];
  variable string Station;

  foreach Station in StationList do
    Lb.content[Lb.itemcount+1] := Station;
  endfor
}

10.8.3 while Loop

This construct is a repetition statement, a loop statement, at which the number of repetitions depends on a condition. The condition is checked at the beginning of a new run.

Syntax

while <condition> do
  <statements>
endwhile

The value <condition> is calculated before each run of a loop. It has to be a boolean value. If it is true, the <statements> will be carried out and the loops will be started again from the beginning. If the value is false, the loop will be canceled. There is no other way to end a while loop.

Example

while (Index < Window.childcount) do
  print Window.child;
  Index := Index + 1;
endwhile