Loops/With multiple ranges: Difference between revisions
(→{{header|zkl}}: added code) |
m (→{{header|zkl}}: fancify output) |
||
Line 116: | Line 116: | ||
} |
} |
||
/* SUM and PROD are used for verification of J incrementation */ |
/* SUM and PROD are used for verification of J incrementation */ |
||
println("sum = |
println("sum = %,d\nprod = %,d".fmt(sum,prod));</lang> |
||
println("prod = ",prod);</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
sum = |
sum = 348,173 |
||
prod = - |
prod = -793,618,560 |
||
</pre> |
</pre> |
Revision as of 03:52, 14 September 2018
Some languages allow multiple loop ranges, such as the PL/I example (snippet) below.
<lang pli> /* all variables are DECLARED as integers. */
prod= 1; /*start with a product of unity. */ sum= 0; /* " " " sum " zero. */ x= +5; y= -5; z= -2; one= 1; three= 3; seven= 7; /*(below) ** is exponentiation: 4**3=64 */ do j= -three to 3**3 by three , -seven to +seven by x , 555 to 550 - y , 22 to -28 by -three , 1927 to 1939 , x to y by z , 11**x to 11**x + one; /* ABS(n) = absolute value*/ sum= sum + abs(j); /*add absolute value of J.*/ if abs(prod)<2**27 & j¬=0 then prod=prod*j; /*PROD is small enough & J*/ end; /*not 0, then multiply it.*/ /*SUM and PROD are used for verification of J incrementation.*/ display (' sum= ' || sum); /*display strings to term.*/ display ('prod= ' || prod); /* " " " " */</lang>
- Task
Simulate/translate the above PL/I program snippet as best as possible in your language, with particular emphasis on the do loop construct.
The do index must be incremented/decremented in the same order shown.
If possible, add commas to the two output numbers.
Show all output here.
- Related tasks
- Loop over multiple arrays simultaneously
- Loops/Break
- Loops/Continue
- Loops/Do-while
- Loops/Downward for
- Loops/For
- Loops/For with a specified step
- Loops/Foreach
- Loops/Increment loop index within loop body
- Loops/Infinite
- Loops/N plus one half
- Loops/Nested
- Loops/While
- Loops/with multiple ranges
REXX
Programming note: the (sympathetic) trailing semicolons (;) after each REXX statement are optional, they are only there to mimic what the PL/I language requires after each statement.
The technique used by this REXX version is to "break up" the various do iterating clauses (ranges) into separate do loops, and have them invoke a subroutine to perform the actual computations. <lang rexx>/*REXX program emulates a multiple─range DO loop (all variables can be any numbers). */
prod= 1; sum= 0; x= +5; y= -5; z= -2; one= 1;
three= 3; seven= 7;
do j= -three to 3**3 by three ; call meat; end; do j= -seven to seven by x ; call meat; end; do j= 555 to 550 - y ; call meat; end; do j= 22 to -28 by -three ; call meat; end; do j= 1927 to 1939 ; call meat; end; do j= x to y by z ; call meat; end; do j= 11**x to 11**x + one ; call meat; end;
say ' sum= ' || commas( sum); /*display SUM with commas. */ say 'prod= ' || commas(prod); /* " PROD " " */ exit; /*stick a fork in it, we're done.*/ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: procedure; parse arg _; n= _'.9'; #= 123456789; b= verify(n, #, "M")
e= verify(n, #'0', , verify(n, #"0.", 'M') ) - 4 do j=e to b by -3; _= insert(',', _, j); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/ meat: sum= sum + abs(j);
if abs(prod)<2**27 & j\==0 then prod= prod * j; return;</lang>
- output when using the same variable values:
sum= 348,173 prod= -793,618,560
zkl
<lang zkl>prod,sum := 1,0; /* start with a product of unity, sum of 0 */ x,y,z := 5, -5, -2; one,three,seven := 1,3,7; foreach j in (Walker.chain([-three..(3).pow(3),three],
[-seven..seven,x], [555..550 - y], [22..-28,-three], [1927..1939], [x..y,z], [(11).pow(x)..(11).pow(x) + one])){ sum+=j.abs(); /* add absolute value of J */ if(prod.abs()<(2).pow(27) and j!=0) prod*=j; /* PROD is small enough & J */
} /* SUM and PROD are used for verification of J incrementation */ println("sum = %,d\nprod = %,d".fmt(sum,prod));</lang>
- Output:
sum = 348,173 prod = -793,618,560