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 loops

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 loops

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 loops

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 once

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.

```/* This snippet asks the user their age and tries again if their answer is negative. */
/* The question will always be asked at least once. */
do until age >= 0
say "How old are you?"
age = LineIn()
if age < 0 then say "Your age cannot be negative."
end
```

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

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:

```/* This snippet lets the user enter a number to start a countdown to lift off. */
/* If the user enters a negative number, there won't be a countdown at all. */
say "Where should I start the countdown?"
number = LineIn()
do while number > 0
say number
number = number - 1
end
say "Lift off!"
```