Sometimes, one may need   (or want)   a loop which
its   iterator   (the index
variable)   is modified within the

loop body    in addition to the normal incrementation by the   (do)   loop structure index.



Goal

Demonstrate the best way to accomplish this.



Task

Write a loop which:

   starts the index (variable) at   42
   (at iteration time)   increments the index by unity
   if the index is prime:
   displays the count of primes found (so far) and the prime   (to the terminal)
   increments the index such that the new index is now the (old) index plus that prime
   terminates the loop when   42   primes are shown


Extra credit:   because of the primes get rather large, use commas
within the displayed primes to ease comprehension.



Show all output here.



Note

Not all programming languages allow the modification of a
loop's index.   If that is the case, then use whatever method that
is appropriate or idiomatic for that language.   Please add a note
if the loop's index isn't modifiable.



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/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


*        Loops/Increment loop index within loop body - 16/07/2018
LOOPILWB PROLOG
         SR     R6,R6              i=0
         ZAP    N,=P'42'           n=42
       DO WHILE=(C,R6,LT,IMAX)     do while(i<imax)
         BAL    R14,ISPRIME          call isprime(n)
       IF C,R0,EQ,=F'1' THEN         if n is prime then
         LA     R6,1(R6)               i=i+1
         XDECO  R6,XDEC                edit i
         MVC    PG+2(2),XDEC+10        output i
         MVC    ZN,EM                  load edit mask
         ED     ZN,N                   edit n
         MVC    PG+7(L'ZN),ZN          output n
         XPRNT  PG,L'PG                print buffer
         ZAP    WP,N                   n
         AP     WP,N                   +n
         SP     WP,=P'1'               +1
         ZAP    N,WP                   n=n+n-1
       ENDIF    ,                    endif                  
         ZAP    WP,N                 n
         AP     WP,=P'1'             +1
         ZAP    N,WP                 n=n+1
       ENDDO    ,                  enddo
         EPILOG
ISPRIME  EQU    *                  isprime(n) -----------------------
         CP     N,=P'2'            if n=2
         BE     RETURN1            then return(1)
         CP     N,=P'3'            if n=3
         BE     RETURN1            then return(1)
         ZAP    WDP,N              n
         DP     WDP,=PL8'2'        /2
         CP     WDP+8(8),=P'0'     if mod(n,2)=0
         BE     RETURN0            then return(0)
         ZAP    WDP,N              n
         DP     WDP,=PL8'3'        /3
         CP     WDP+8(8),=P'0'     if mod(n,3)=0
         BE     RETURN0            then return(0)
         ZAP    J,=P'5'            j=5
LWHILE   ZAP    WP,J               j
         MP     WP,J               *j
         CP     WP,N               while(j*j<=n)
         BH     EWHILE             ~
         ZAP    WDP,N                n
         DP     WDP,J                /j
         CP     WDP+8(8),=P'0'       if mod(n,j)=0
         BE     RETURN0              then return(0)
         ZAP    WP,J                 j
         AP     WP,=P'2'             +2
         ZAP    WDP,N                n
         DP     WDP,WP               n/(j+2)
         CP     WDP+8(8),=P'0'       if mod(n,j+2)=0
         BE     RETURN0              then return(0)
         ZAP    WP,J                 j
         AP     WP,=P'6'             +6
         ZAP    J,WP                 j=j+6
         B      LWHILE             loopwhile
EWHILE   B      RETURN1            return(1)
RETURN0  LA     R0,0               rc=0
         B      RETURNX
RETURN1  LA     R0,1               rc=1
RETURNX  BR     R14                return to caller -----------------
IMAX     DC     F'42'              limit
EM       DC     XL20'402020206B2020206B2020206B2020206B202120'  mask
N        DS     PL8                n
J        DS     PL8                j
PG       DC     CL80'i=00 :  000,000,000,000,000'   buffer
XDEC     DS     CL12               temp for XDECO
WP       DS     PL8                temp for AP,SP,MP
WDP      DS     PL16               temp for DP
CW       DS     CL16               temp for UNPK
ZN       DS     CL20
         REGEQU
         END    LOOPILWB