Loops/With multiple ranges: Difference between revisions

Content added Content deleted
(Added Perl example)
(added additional notes explaining how a PL/I DO loop works.)
Line 4: Line 4:


{{draft task|Iteration}}
{{draft task|Iteration}}
{{Category:Clarify task}}


Some languages allow multiple '''loop''' ranges, such as the '''PL/I''' example (snippet) below.
Some languages allow multiple '''loop''' ranges, such as the '''PL/I''' example (snippet) below.
Line 40: Line 39:
The   '''do'''   index must be incremented/decremented in the same order shown.
The   '''do'''   index must be incremented/decremented in the same order shown.


If possible, add commas to the two output numbers.
If feasible, add commas to the two output numbers (being displayed).


Show all output here.
Show all output here.
<lang> A simple PL/I DO loop (incrementing or decrementing) has the construct of:

DO variable = start_expression {TO ending_expression] {BY increment_expression} ;
---or---
DO variable = start_expression {BY increment_expression} {TO ending_expression] ;

where it is understood that all expressions will have a value. The variable is normally a
scaler variable, but need not be (but for this task, all variables and expressions are declared
to be scaler integers). If the BY expression is omitted, a BY value of unity is used.
All expressions are evaluated before the DO loop is executed, and those values are used
throughout the DO loop execution (even though, for instance, the value of Z may be
changed within the DO loop. This isn't the case here for this task.

A multiple-range DO loop can be constructed by using a comma (,) to separate additional ranges
(the use of multiple TO and/or BY keywords). This is the construct used in this task.
There are other forms of DO loops in PL/I involving the WHILE clause, but those won't be
needed here. DO loops without a TO clause might need a WHILE clause or some other
means of exiting the loop (such as LEAVE, RETURN, SIGNAL, GOTO, or STOP), or some other
(possible error) condition that causes transfer of control outside the DO loop.
Also, in PL/I, the check if the DO loop index value is outside the range is made at the
"head" (start) of the DO loop, so it's possible that the DO loop isn't executed, but
that isn't the case for any of the ranges used in this task.

In the example above, the clause: x to y by z
will cause the variable J to have to following values (in this order): 5 3 1 -1 -3 -5

In the example above, the clause: -seven to +seven by x
will cause the variable J to have to following values (in this order): -7 -2 3 </lang>