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


/*REXX program loops through a two-dimensional array to search for a  '20'    (twenty). */
parse arg rows cols targ .                       /*obtain optional arguments from the CL*/
if rows=='' | rows==","  then rows=60            /*Rows not specified?  Then use default*/
if cols=='' | cols==","  then cols=10            /*Cols  "      "         "   "     "   */
if targ=='' | targ==","  then targ=20            /*Targ  "      "         "   "     "   */
w=max(length(rows), length(cols), length(targ))  /*W:  used for formatting the output.  */
not= 'not'                                       /* [↓]  construct the 2─dimension array*/
          do     row=1  for rows                 /*ROW  is the 1st dimension of array.  */
              do col=1  for cols                 /*COL   "  "  2nd     "      "   "     */
              @.row.col=random(1, targ)          /*create some positive random integers.*/
              end   /*row*/
          end       /*col*/
 
          do     r=1  for rows    /* ◄───────────────── now, search for the target {20}.*/
              do c=1  for cols
              say left('@.'r"."c, 3+w+w) '=' right(@.r.c, w)    /*show an array element.*/
              if @.r.c==targ  then do; not=; leave r; end       /*found the targ number?*/
              end   /*c*/
          end       /*r*/
 
say right( space( 'Target'  not  "found:" )    targ, 33, '─')
                                                 /*stick a fork in it,  we're all done. */