Rexx Programming/How to Rexx/loops

From Wikibooks, open books for an open world
Jump to: navigation, search

Loops are control structures that allow sections of code to be executed repeatedly according to the controlling conditions of the loop. The rexx scripting language supports both iterative loops and conditional loops.

Iterative Loops[edit]

An iterative loop repeatedly executes a set of instructions as the iterator steps through a series of values. Types of iterative loops include forloops and foreach loops. The following is a traditional example of an iterative for loop:

do l = 1 to 10
  say l

In the above example, the say block runs ten times and the iterator variable $l, increments on each consecutive loop.

Conditional Loops[edit]

A conditionalloop 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 and until loops.

l = 0 do while l <= 10

 say l
 l = l + 1


Loop Controls[edit]

Both iterative loops and conditional loops can be controlled by loop modifier statements, such as leave, iterate and signal (Do we also have next, last and redo?). These allow normal flow of execution within a loop to be restarted or terminated.

Nested Loops[edit]

The rexx scripting language allows nested loop structures to be used. These consist of one or more loops that are nested within other loops.