Show a nested loop which searches a two-dimensional array filled with random numbers uniformly distributed over 



[
1
,
…
,
20
]


{\displaystyle [1,\ldots ,20]}

.

The loops iterate rows and columns of the array printing the elements until the value 



20


{\displaystyle 20}

 is met.

Specifically, this task also shows how to break out of nested loops.



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/Wrong ranges


*        Loop nested               12/08/2015 
LOOPNEST CSECT
         USING  LOOPNEST,R12
         LR     R12,R15
BEGIN    LA     R6,0               i
         LA     R8,1
         LA     R9,20
LOOPI1   BXH    R6,R8,ELOOPI1      do i=1 to hbound(x,1)
         LA     R7,0               j
         LA     R10,1
         LA     R11,20
LOOPJ1   BXH    R7,R10,ELOOPJ1     do j=1 to hbound(x,2)
         L      R5,RANDSEED        n
         M      R4,=F'397204094'   r4r5=n*const
         D      R4,=X'7FFFFFFF'    r5=r5 div (2^31-1)
         ST     R4,RANDSEED        r4=r5 mod (2^31-1) ; n=r4
         LR     R5,R4              r5=n
         LA     R4,0
         D      R4,=F'20'          r5=n div nn; r4=n mod nn
         LR     R2,R4              r2=randint(nn) [0:nn-1]
         LA     R2,1(R2)           randint(nn)+1
         LR     R1,R6              i
         BCTR   R1,0
         MH     R1,=H'20'
         LR     R5,R7              j
         BCTR   R5,0
         AR     R1,R5
         SLA    R1,2
         ST     R2,X(R1)           x(i,j)=randint(20)+1
         B      LOOPJ1
ELOOPJ1  B      LOOPI1
ELOOPI1  MVC    MVCZ,=CL80' '
         LA     R6,0               i
         LA     R8,1
         LA     R9,20
LOOPI2   BXH    R6,R8,ELOOPI2      do i=1 to hbound(x,1)
         LA     R7,0               j
         LA     R10,1
         LA     R11,20
LOOPJ2   BXH    R7,R10,ELOOPJ2     do j=1 to hbound(x,2)
         LR     R1,R6
         BCTR   R1,0
         MH     R1,=H'20'
         LR     R5,R7
         BCTR   R5,0
         AR     R1,R5
         SLA    R1,2
         L      R5,X(R1)           x(i,j)
         LR     R2,R5
         LA     R3,MVCZ
         AH     R3,MVCI
         XDECO  R2,XDEC
         MVC    0(4,R3),XDEC+8
         LH     R3,MVCI
         LA     R3,4(R3)
         STH    R3,MVCI
         L      R5,X(R1)
         C      R5,=F'20'          if x(i,j)=20 
         BE     ELOOPI2            then exit
         B      LOOPJ2
ELOOPJ2  XPRNT  MVCZ,80
         MVC    MVCI,=H'0'
         MVC    MVCZ,=CL80' '
         B      LOOPI2
ELOOPI2  XPRNT  MVCZ,80
RETURN   XR     R15,R15
         BR     R14
X        DS     400F
MVCZ     DS     CL80
MVCI     DC     H'0'
XDEC     DS     CL16
RANDSEED DC     F'16807'           running n
         YREGS  
         END    LOOPNEST