Loops/Nested: Difference between revisions

m
(Add lang example)
m (→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 7 users not shown)
Line 254:
8 9 1 15 3 1 10 19 6 7
12 20
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <jambo.h>
 
#define DIMS 10
Main
Unset decimal
Dim (DIMS,DIMS) as ceil rand (20,t)
 
Set decimal '0'
Printnl ("ORIGINAL MATRIX:\n", Just right (3, Str(t)), "\n")
 
aux=0
Loop for ( i=1, #(i<=DIMS && aux<>20 ), ++i)
Loop for ( j=1, #(j<=DIMS), ++j)
When ( Equals ( 20, [i,j] Get 't' ---Copy to 'aux'---) ) { Break }
/*
Also: When( #( ((aux:= (t[i,j])) == 20) ) ) { Break }
*/
Just right (3, Str(aux)), Print only if ( #(DIMS-j), "," )
Next
Prnl
Next
Printnl ("\nFOUNDED: ", i,",",j," = ",aux)
End
</syntaxhighlight>
{{out}}
<pre>
ORIGINAL MATRIX:
16, 5, 15, 19, 14, 15, 12, 15, 10, 19
6, 8, 17, 1, 5, 13, 14, 4, 15, 5
10, 17, 8, 4, 9, 19, 14, 17, 7, 4
7, 2, 8, 1, 20, 1, 15, 12, 16, 4
10, 2, 12, 7, 3, 16, 19, 16, 19, 14
1, 9, 11, 9, 12, 19, 7, 6, 16, 13
9, 2, 15, 16, 2, 15, 17, 17, 7, 13
20, 17, 15, 12, 3, 17, 8, 2, 13, 7
15, 13, 15, 6, 2, 7, 5, 8, 12, 20
1, 20, 1, 16, 16, 2, 10, 12, 19, 17
 
 
16, 5, 15, 19, 14, 15, 12, 15, 10, 19
6, 8, 17, 1, 5, 13, 14, 4, 15, 5
10, 17, 8, 4, 9, 19, 14, 17, 7, 4
7, 2, 8, 1,
 
FOUNDED: 4,5 = 20
 
</pre>
 
Line 559 ⟶ 609:
bx lr
</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">printTable: function [tbl][
; wrapping the nested loop in a function
; allows us to use return to exit all of the loops
; since `break` only exits the inner loop
loop 0..dec size tbl 'x [
loop 0..dec size tbl\[x] 'y [
prints pad to :string tbl\[x]\[y] 2
if tbl\[x]\[y] = 20 -> return ø
prints ", "
]
print ""
]
]
 
a: []
loop 1..10 'x [
row: []
loop 1..10 'y [
'row ++ random 1 20
]
'a ++ @[row]
]
 
printTable a</syntaxhighlight>
 
{{out}}
 
<pre> 4, 12, 12, 17, 7, 13, 14, 10, 14, 9,
17, 12, 16, 10, 11, 13, 8, 13, 17, 3,
6, 17, 3, 18, 2, 16, 7, 9, 19, 9,
19, 11, 11, 14, 5, 14, 18, 17, 19, 15,
17, 16, 8, 3, 14, 17, 5, 6, 8, 1,
8, 4, 9, 10, 16, 16, 4, 15, 10, 18,
5, 8, 15, 19, 7, 8, 2, 6, 10, 8,
4, 17, 15, 18, 14, 2, 20</pre>
 
=={{header|AutoHotkey}}==
Line 1,213 ⟶ 1,300:
}
println("done.")</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
arr[][] = [ [ 2 12 10 4 ] [ 18 11 20 2 ] ]
for i to len arr[][]
for j to len arr[i][]
if arr[i][j] = 20
print "20 at " & i & "," & j
break 2
.
.
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,626 ⟶ 1,726:
19 1 15 14 10 20
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
long a(9,9), i, j
BOOL done = NO
 
for i = 0 to 9
for j = 0 to 9
a(i,j) = rnd(20)
next
next
 
for i = 0 to 9
for j = 0 to 9
print a(i,j)
if ( a(i,j) == 20 ) then done = YES : break
next
if ( done ) then break
next
 
HandleEvents
</syntaxhighlight>
 
=={{header|Gambas}}==
Line 2,236 ⟶ 2,358:
$j
repeat($[j], 10) {
fn.arraySet(&array, [$j,] $= parser.op(fn.randRange(20) + 1))
}
fn.arraySet(&values, [$i,] ::= &array)
}
 
Line 2,249 ⟶ 2,371:
if($ele === 20) {
con.break(2) //=# numberNumber of loops we want to break out of
}
}
Line 3,472 ⟶ 3,594:
row 5 col 5 value : 6
</pre>
 
=={{header|RPL}}==
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}}==
Line 4,456 ⟶ 4,609:
{{libheader|Wren-fmt}}
Wren doesn't have ''goto'' or ''break label'' so to break out of nested loops you need to use a flag (''found'' in the code below).
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
Line 4,470 ⟶ 4,623:
for (i in 0..19) {
for (j in 0..19) {
SystemFmt.write(Fmt.d(4"$4d", a[i][j]))
if (a[i][j] == 20) {
found = true
Line 4,637 ⟶ 4,790:
 
{{omit from|GUISS|No loops or nesting, and we can't read our values}}
{{omit from|PL/0|No arrays}}
{{omit from|Tiny BASIC|No arrays}}
9,476

edits