Last modified on 3 October 2011, at 18:42

Rexx Programming/How to Rexx/conditional loop

A conditional loop tests for a condition around the loop, and repeatedly executes a block of instructions whilst the condition is true. Types of conditional loops include while loops, until loops and repeat loops.

While loopsEdit

A while loop is a conditional loop that repeatedly executes a block of instructions whilst the condition is true. The while statement is used as a syntactical cocomponent of do and end constructs to produce a while loop:

l = 0 do while l <= 10

 say l
 l = l + 1

end

Until loopsEdit

An until loop is a conditionalloop that repeatedly executes a block of instructions until the condition is true. In Rexx, the until statement is used together with do and end constructs to produce an until loop:

l = 1; do until l > 10

 say l
 l = l + 1

end

Repeat loopsEdit

A *repeat loop* is a [conditionalloop] that repeatedly executes a [block] of instructions whilst the condition is true. The [do] statement is used together with an [until] syntactical cocomponent to produce a repeat loop:

attempts = 1
do until attempts==10
  say "Knock! Knock!"
  attempts = attempts + 1
end

The block of code within a repeat loop will always run at least onceEdit

Note than unlike [while] loops, with a repeat loop, the [condition] is evaluated after the [block] of code within the [loop]. even though it is written at the beginning. This means that the [block] of code within a repeat loop will always run at least once.

The cocomponents do and while are evaluated at the start of the loopEdit

If a [do] and [while] cocomponents are used instead of [do] and [until], the loop will behave as a [while] loop with the condition being evaulated at the beginning of the loop. This may cause the loop to not execute at all, if the expression evaluates to a boolean [false] value: