Loops/With multiple ranges: Difference between revisions

From Rosetta Code
Content added Content deleted
(added a new Rosetta Code task.)
 
m (elided a duplicate link (related tasks).)
Line 55: Line 55:
*   [[Loops/Foreach]]
*   [[Loops/Foreach]]
*   [[Loops/Increment loop index within loop body]]
*   [[Loops/Increment loop index within loop body]]
*   [[Loops/Infinite]]
*   [[Loops/Infinite]]
*   [[Loops/Infinite]]
*   [[Loops/N plus one half]]
*   [[Loops/N plus one half]]

Revision as of 02:46, 14 September 2018


Loops/With multiple ranges is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


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



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