2.07 Loops and Iteration

For Loops

Description:

Repeat a loop a defined number of times.

Syntax:

for counter = start to end [by incr] [do] … [end]
for [each] element in array [do] … [end]
for times [do] … [end]

where:

counter
control variable for the loop
start
the starting value for the control variable
end
the ending value for the control variable. The loop ends when the value of the control variable reaches or passes this value.
incr
the amount by which the control variable is changed on each iteration. The value may be postive or negative.
element
a variable which will hold the value of each element of the array in turn.
array
an array from which elements are extracted.
times
a value that is the number of times the loop will be executed.

Example:

for ix = 1 to 10 by 5 do
  :
  end //for ix = 1 to 10 by 5 

Implementation Status:

LynPlex Interpreter
Fully implemented.
LynPlexC Compiler
Does not yet support single line for.
Does not yet support for each or times.
LynPlexS Compiler
Does not yet support for each or times.
Supports fornext in basic mode.

While/Until Loops

Description:

Loops while a certain condition is true or until a certain condition is true.

Syntax:

while expr [do] … [end]
while expr single statement
until expr [do] … [end]
until expr single statement

where:

while
defines a while loop. The loop will continue while the expression is true. If the expression is not true at the start of the loop the loop will not be executed.
until
defines an until loop. The loop will continue until the expression is true. The loop will always be executed once.

Example:

while ix < 3 do
  :
  end //while ix < 3

Implementation Status:

LynPlex Interpreter
Fully implemented
LynPlexC Compiler
Supports:
While conditionWend.
Single statement is not yet supported.
LynPlexS Compiler
Not supported - see do loops

Do Loops

Description:

Where the do command is used to start a loop the above options may be combined in a single statement. It is possible, for example, to have a for loop combined with a while or until loop, or have both a while and until loop.

If this happens the options may be specified in any order but they will always be processed in a given order.

If a for command is specified this will be actioned and the result tested if necessary before anything else. If the test fails, the loop will finish at this point.

If the for test is successful, any while test that is specified will then be tested. Again if the test fails, the loop will finish at this point.

If both the tests are successful, the body of the loop will be executed.

If an until test is also specified, the test will be made at the end of the loop, after the body of the loop has been executed. If the test is successful, processing will continue with the next statement following the loop. If the test fails processing continues with the for statement or, if there is no for statement, with the while statement.

Syntax:

do [ [for] expression] [while expression] [until expression] do forever

Example:

do ix = 1 to 10 by 3 ..
  while jx/2 = 0 ..
  until kx > 30
  :
  end //do ix = 1 to 10 by 3 ..

do for each item in an_array ..
  until kx > 30
  :
  end //do for each item in an_array

do 10 ..
  while jx/2 = 0 
  :
  end //do 10 ..

Implementation Status:

LynPlex Interpreter
Supports for, while or until separately but not combined.
Does not yet support optional for following leading do.
Does not yet support forever option
LynPlexC Compiler
Supports:
do while/until conditionend
do while/until conditionloop
doloop while/until condition
LynPlexS Compiler
Supports:
do while/until conditionend
do while/until conditionloop
doloop while/until condition

Short circuit Loops

Description:

The loop statement may be used within a loop to force the next iteration.

Syntax:

loop

Example:

do while x 
  :
  loop
  :
  end //do while x 

Implementation Status:

LynPlex Interpreter
To be implemented
LynPlexC Compiler
Supports continue
LynPlexS Compiler
Supports do loop and do do

Exit from Loops

Description:

Exits a multi-line function definition, a loop, or a subprogram.

Syntax:

exit [for|do|loop|sub|func] …
exit [for|do|loop|sub|func] [level]

Example:

exit do
exit loop loop
exit loop 2

Implementation Status:

LynPlex Interpreter
Does not support do, repeats or levels
LynPlexC Compiler
Supports repeats but does not support levels.
LynPlexS Compiler
Supports levels but does not support repeats.

See also


lynplex/lp0207.txt · Last modified: 2012/08/22 09:03 (external edit)