DODO groups instructions - IBM-REXX

« Previous Topics
Call In Rexx
Arg
Address
Next Topics »
Drop
Exit
If

DODO groups instructions

DO groups instructions together and optionally processes them repetitively. During repetitive execution, a control variable (name) can be stepped through some range ofvalues.

Syntax Notes:

  • The exprr,expri,exprb,exprt, and exprf options (if present) are any expressions that evaluate to a number. The exprr and exprf options are further restricted to result in a positive whole number or zero.If necessary, the numbers are rounded according to the setting of NUMERIC DIGITS.
  • The exprw or expru options (if present) can be any expression that evaluates to 1 or 0.
  • The TO, BY, and FOR phrases can be in any order, if used, and are evaluated in the order in which they are written.
  • The instruction can be any instruction, including assignments, commands, and keyword instructions (including any of the more complex constructs such as IF, SELECT, and the DO instruction itself).
  • The subkeywords WHILE and UNTIL are reserved within a DO instruction, in that they cannot be used as symbols in any of the expressions. Similarly, TO, BY, and FOR cannot be used in expri, exprt, exprb, or exprf.FOREVER is also reserved, but only if it immediately follows the keyword DO and an equal sign does not follow it.
  • The exprb option defaults to 1, if relevant.

Simple DO Group
If you specify neither repetitor nor conditional, the construct merely groups a number of instructions together.These are processed one time.

In the following example, the instructions are processed one time.

Example:

/* The two instructions between DO and END are both */
/* processed if A has the value "3". */
If a=3 then Do
a =a+2
Say ’Smile!’
End

Repetitive DO Loops
If a DO instruction has a repetitor phrase or a conditional phrase or both, the group of instructions forms a repetitive DO loop.The instructions are processed according to the repetitor phrase, optionally modified by the conditional phrase.

Simple Repetitive Loops
A simple repetitive loop is a repetitive DO loop in which the repetitor phrase is an expression that evaluates to a count of the iterations.

If repetitor is omitted but there is a conditional or if the repetitor is FOREVER, the group of instructions is nominally processed “forever”, that is, until the condition is satisfied or a REXX instruction is processed that ends the loop (for example, LEAVE).

In the simple form of a repetitive loop,exprr is evaluated immediately (and must result in a positive whole number or zero), and the loop is then processed that many times.

Example:

/* This displays "Hello" five times */
Do 5
say ’
Hello’
end

Note that, similar to the distinction between a command and an assignment, if the first token of exprr is a symbol and the second token is (or starts with) =, the controlled form of repetitor is expected.

Controlled Repetitive Loops

The controlled form specifies name, a control variable that is assigned an initial value (the result of expri, formatted as though 0 had been added) before the first execution of the instruction list.The variable is then stepped (by adding the result of exprb) before the second and subsequent times that the instruction list is processed.

The instruction list is processed repeatedly while the end condition (determined by the result of exprt) is not met.If exprb is positive or 0, the loop is ended when name is greater than exprt.If negative, the loop is ended when name is less than exprt.

The expri, exprt,and exprb options must result in numbers.They are evaluated only one time, before the loop begins and before the control variable is set to its initial value.The default value for exprb is 1.If exprt is omitted, the loop runs indefinitely unless some other condition stops it.

Example:

Example

The numbers do not have to be whole numbers:

Example:

Example

The control variable can be altered within the loop, and this may affect the iteration of the loop. Altering the value of the control variable is not usually considered good programming practice, though it may be appropriate in certain circumstances.

Note that the end condition is tested at the start of each iteration (and after the control variable is stepped, on the second and subsequent iterations).Therefore, if the end condition is met immediately, the group of instructions can be skipped entirely.Note also that the control variable is referred to by name.If (for example) the compound name A.I is used for the control variable, altering I within the loop causes a change in the control variable.

The execution of a controlled loop can be bounded further by a FOR phrase.In this case, you must specify exprf, and it must evaluate to a positive whole number or zero.This acts just like the repetition count in a simple repetitive loop, and sets a limit to the number of iterations around the loop if no other condition stops it.Like the TO and BY expressions, it is evaluated only one time—when the DO instruction is first processed and before the control variable receives its initial value.Like the TO condition, the FOR condition is checked at the start of each iteration.

Example:

Do Y=0.3 to 4.3 by 0.7 for 3 /* Displays: */
say Y /* 0.3 */
end /* 1.0 */
/* 1.7 */

In a controlled loop, the name describing the control variable can be specified on the END clause. This name must match name in the DO clause in all respects except case (note that no substitution for compound variables is carried out); a syntax error results if it does not.This enables the nesting of loops to be checked automatically, with minimal overhead.

Example:

Do K=1 to 10
... ...
End k /* Checks that this is the END for K loop */

The NUMERIC settings may affect the successive values of the control variable, because REXX arithmetic rules apply to the computation of stepping the control variable.

Conditional Phrases (WHILE and UNTIL)
A conditional phrase can modify the iteration of a repetitive DO loop.It may cause the termination of a loop.It can follow any of the forms of repetitor (none, FOREVER, simple, or controlled). If you specify WHILE or UNTIL, exprw or expru, respectively, is evaluated each time around the loop using the latest values of all variables (and must evaluate to either 0 or 1), and the loop is ended if exprw evaluates to 0 or expru evaluates to 1.

For a WHILE loop, the condition is evaluated at the top of the group of instructions.For an UNTIL loop, the condition is evaluated at the bottom—before the control variable has been stepped.

Example:

Do I=1 to 10 by 2 until i>6
say i
end
/* Displays: "1" "3" "5" "7" */

Tip:Using the LEAVE or ITERATE instructions can also modify the execution of repetitive loops.

Concept of a DO Loop

Concept of a DO LoopConcept of a DO Loop

« Previous Topics
Call In Rexx
Arg
Address
Next Topics »
Drop
Exit
If

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM-REXX Topics