Task

Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive).

If a number is 10, stop the loop after printing it, and do not generate any further numbers.

Otherwise, generate and print a second random number before restarting the loop.

If the number 10 is never generated as the first number in a loop, loop forever.



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


*        Loops Break               15/02/2017
LOOPBREA CSECT
         USING  LOOPBREA,R13       base register
         B      72(R15)            skip savearea
         DC     17F'0'             savearea
         STM    R14,R12,12(R13)    prolog
         ST     R13,4(R15)         " <-
         ST     R15,8(R13)         " ->
         LR     R13,R15            " addressability
LOOP     MVC    PG,=CL80' '        clean buffer
         LA     R8,PG              ipg=0
         BAL    R14,RANDINT        call randint
         C      R6,=F'10'          if k=10 then leave
         BE     ENDLOOP             <-- loop break
         BAL    R14,RANDINT        call randint
         XPRNT  PG,L'PG            print buffer
         B      LOOP               loop forever
ENDLOOP  XPRNT  PG,L'PG            print buffer
         L      R13,4(0,R13)       epilog 
         LM     R14,R12,12(R13)    " restore
         XR     R15,R15            " rc=0
         BR     R14                exit
RANDINT  L      R5,RANDSEED        randint
         M      R4,=F'397204091'   "
         D      R4,=X'7FFFFFFF'    "
         ST     R4,RANDSEED        "
         LR     R5,R4              "
         SR     R4,R4              "
         D      R4,=F'20'          "
         LR     R6,R4              k=randint(20)
         XDECO  R6,XDEC            edit k
         MVC    0(4,R8),XDEC+8     output k
         LA     R8,4(R8)           ipg=ipg+4
         BR     R14                return
RANDSEED DC     F'39710831'        seed
PG       DS     CL80               buffer
XDEC     DS     CL12
         YREGS
         END    LOOPBREA