Loops/Nested: Difference between revisions

1,276 bytes added ,  11 months ago
add RPL
(added Arturo)
(add RPL)
Line 3,509:
row 5 col 5 value : 6
</pre>
 
As there is no <code>BREAK</code> instruction in RPL, premature loop exit is usually made by forcing the loop variable to its end value. Depending on the way exit is required and on the need for code optimization within the loop, there are several ways to implement such a break feature. The following one is based on two <code>FOR..NEXT</code> loops:
{ 10 10 } 0 CON
1 10 '''FOR''' j
1 10 '''FOR''' k
j k 2 →LIST RAND 20 * CEIL PUT
'''NEXT'''
''' NEXT'''
DROP 1 CF
1 10 '''FOR''' j
1 10 '''FOR''' k
DUP j k 2 →LIST GET
DUP 1 DISP
'''IF''' 20 == '''THEN''' 1 SF '''END'''
1 FC? 1 10 IFTE '''STEP'''
1 FC? 1 10 IFTE '''STEP'''
We could also only use a <code>WHILE..REPEAT</code> loop, scanning the matrix line by line, but there is no nested loop anymore:
≪ 46 → done
≪ { 10 10 } 0 CON
{ 1 1 }
'''DO'''
RAND 20 * CEIL PUTI
'''UNTIL''' done FS? '''END'''
'''DO'''
GETI DUP 1 DISP
'''UNTIL''' 20 == done FS? OR '''END'''
≫ ≫
The <code>done</code> constant is the number of the system flag that becomes set when the last element of the matrix is written. The above value is for HP-28 versions; HP-48 users must change it to -64.
 
=={{header|Ruby}}==
1,150

edits