In the following example, the do-group executes 5 times and at the end of the loop i has the value 5:
do i = 1 upthru 5;
·
·
·
end;
When the UPTHRU option is used, the reference is compared to the terminating value before being updated; this can be very useful when there is no value after the terminating value. For instance, the FIXEDOVERFLOW condition would not be raised by the following loop:
do i = 2147483641 upthru 2147483647;
·
·
·
end;
Similarly, the following loop avoids the problem of decrementing an unsigned value equal to zero:
dcl U unsigned fixed bin; do U = 17 downthru 0;
·
·
·
end;
UPTHRU and DOWNTHRU are particularly useful with ordinals. Consider the following example:
define ordinal Color ( Red value (1), Orange, Yellow, Green, Blue, Indigo, Violet); dcl C ordinal Color; do C = Red upthru Violet;
·
·
·
end; do C = Violet downthru Red;
·
·
·
end;
In the first loop, c assumes each successive color in ascending order from red to violet. In the second loop, c assumes each successive color in descending order from violet to red.