Loops/Nested: Difference between revisions

m
(add RPL)
m (→‎{{header|Wren}}: Minor tidy)
(10 intermediate revisions by 5 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 1,250 ⟶ 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,663 ⟶ 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,273 ⟶ 2,358:
$j
repeat($[j], 10) {
fn.arraySet(&array, [$j,] $= parser.op(fn.randRange(20) + 1))
}
fn.arraySet(&values, [$i,] ::= &array)
}
 
Line 2,286 ⟶ 2,371:
if($ele === 20) {
con.break(2) //=# numberNumber of loops we want to break out of
}
}
Line 3,510 ⟶ 3,595:
</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:
Line 4,523 ⟶ 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,537 ⟶ 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
9,485

edits