[<< wikibooks] Rexx Programming/How to Rexx/loops
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 ==
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 count = 1 to 10
  say count
end

In the above example, the say block runs ten times and the iterator variable count, increments on each consecutive loop.
As an iterative loop cycles through its code step after step, it is possible to increase the iterator variable by amounts other than one. The by keyword precedes the amount to add on each iteration.

/* Count by 2's */
do count = 2 to 10 by 2
  say count
end

Alternatively, you can tell Rexx how many times to iterate the loop using the for keyword. We could list the first 20 positive odd numbers as follows:

do count = 1 by 2 for 20
  say count
end


== Conditional Loops ==
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 and until loops.

count = 0
do while count <= 10
 say count
 count = count + 1
end

A while loop will not even start unless the condition is true.

/* Nothing happens! */
count = 100
do while count <= 10
 say "Whatever, you're not going to see this message anyway."
end

Until loops are used to start a block of code and continue until some condition is met.

/* Ask the user to enter a number until they actually do. */
do until entry_is_valid
  say "Please enter a number below."
  pull entry
  entry_is_valid = datatype(entry, "NUM")
end
/* The user will always be asked at least once. */


== Loop Controls ==
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.
The LEAVE instruction stops a loop.

/* This function will keep adding up numbers as long the user keeps entering them. */
say "Enter as many numbers as you want!"
say "Put each one on a line by itself."
total = 0
do forever
  entry = LineIn()
  if DataType(entry) = "NUM" then
    say "Cool, you entered another number!"
  else
    leave
  total = total + entry
  say "The new total is" total"."
end
say "The final total is" total"."

The ITERATE instruction skips to the next step without finishing the current one.

/* Let's pretend 3 and 4 are boring. */
do number = 1 to 5
  if number = 3 then iterate
  if number = 4 then iterate
  say "I found an interesting number. It is" number"."
  say "Do you agree that it is interesting?"
  answer = LineIn()
  if answer = "yes" then do
    say "I'm glad you agree."
    say "Good thing it wasn't 3 or 4."
  end
end


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