Loops/Break: Difference between revisions

6,779 bytes added ,  2 months ago
(→‎{{header|J}}: show a fold single example)
 
(21 intermediate revisions by 13 users not shown)
Line 380:
+7 +4
+10
</pre>
 
=={{header|Amazing Hopper}}==
<p>Flavour "Jambo"</p>
<syntaxhighlight lang="c">
#include <jambo.h>
 
Main
Loop
Break if ' Int rand(20) ---show--- Is equal to (10) '
Printnl ( "--", Int rand(20) )
Back
Print '"\nEnd of Loop\n" '
End
 
</syntaxhighlight>
<p>Assembler Hopper version of this program:</p>
<syntaxhighlight lang="amazing hopper">
main:
____CODE_JUMP____997416047:,
;{20};rand;int;show;eqto(10);jt(____CODE_JUMP____803885359)
 
{"--"};{20};rand;int;{"\n"}print;
,jmp(____CODE_JUMP____997416047),____CODE_JUMP____803885359:,
{"\nEnd of Loop\n"};print;
emptystack?do{{0}};return
</syntaxhighlight>
{{out}}
<pre>
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
11--19
4--4
1--9
0--13
19--18
12--6
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
19--10
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
0--14
7--1
18--11
15--15
17--9
7--1
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
13--0
17--12
16--2
19--14
2--6
19--10
10
End of Loop
xxxx@debian:~/Proy$
</pre>
 
Line 733 ⟶ 797:
print
end</syntaxhighlight>
 
==={{header|bootBASIC}}===
In bootBASIC, the rnd statement returns an unsigned integer between 0 and 255. 255 divided by 19 gives us 13 without the fraction part, so 13 is the number to divide the random number by to get a range of 0 to 19. All division is integer division.
<syntaxhighlight lang="BASIC">
10 a=rnd/13
20 print a ;
30 if a-10 goto 50
40 goto 100
50 a=rnd/13
55 print ", ";
60 print a
70 goto 10
100 print
</syntaxhighlight>
{{out}}
<pre>
13, 19
11, 14
18, 4
17, 0
12, 15
0, 13
7, 19
2, 7
1, 3
6, 18
13, 6
9, 10
4, 7
15, 7
10
</pre>
 
==={{header|Commodore BASIC}}===
Line 781 ⟶ 877:
 
 
==={{header|uBasic/4tH}}===
In uBasic/4tH '''UNTIL''' ''<cond>'' is equivalent to '''IF''' ''<cond>'' '''THEN BREAK'''. You can add as many '''UNTIL''' and '''WHILE''' as required in '''FOR..NEXT''' or '''DO..LOOP''' loops.
<syntaxhighlight lang="qbasic">Do
n = Rnd(20)
Print n
Until n=10
Print Rnd(20)
Loop</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
On the ZX Spectrum, for loops must be terminated through the NEXT statement, otherwise a memory leak will occur. To terminate a loop prematurely, set the loop counter to the last iterative value and jump to the NEXT statement:
Line 1,243 ⟶ 1,347:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">repeat
repeat
a = random 20
a = randint 20
print a
until print a = 10
until a = 10
print random 20
print randint 20
.</syntaxhighlight>
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,338 ⟶ 1,444:
 
(wait_10)</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
for ever
int a = random(20)
write(a)
if a == 10 do break end
writeLine("," + random(20))
end
writeLine()
</syntaxhighlight>
{{out}}
<pre>
19,14
10
</pre>
 
=={{header|Erlang}}==
Line 1,666 ⟶ 1,788:
# 1 16
# 10</syntaxhighlight>
 
=={{header|GDScript}}==
{{works with|Godot|4.0.1}}
{{trans|11l}}
 
<syntaxhighlight lang="gdscript">
extends MainLoop
 
 
func _process(_delta: float) -> bool:
randomize()
 
while true:
var a: int = randi_range(0, 19)
print(a)
if a == 10:
break
var b: int = randi_range(0, 19)
print(b)
 
return true # Exit
</syntaxhighlight>
 
=={{header|GML}}==
Line 1,803 ⟶ 1,947:
* Programmers new to Icon/Unicon need to understand that just about everything returns values including comparison operators, I/O functions like write/writes.
* This program will perform similarly but not identically under Icon and Unicon because the random operator ?i behaves differently. While both produce pseudo-random numbers a different generator is used. Also, the sequence produced by Icon begins with the same seed value and is repeatable whereas the sequence produced by Unicon does not. One way to force Icon to use different random sequences on each call would be to add the line <syntaxhighlight lang="icon">&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))</syntaxhighlight> at the start of the <tt>main</tt> procedure to set the random number seed based on the time of day.
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(while true
(print (let x (rand-int 0 20)))
(when (= x 10) (break)))
</syntaxhighlight>
 
=={{header|Io}}==
Line 1,867 ⟶ 2,019:
label_done.
}}</syntaxhighlight>
 
=={{header|Jakt}}==
The random number generation is slightly biased, but negligible for the purpose of the task.
 
<syntaxhighlight lang="jakt">
fn random(mut random_source: File = File::open_for_reading("/dev/urandom")) throws -> u64 {
mut buffer = [0u8; 4]
random_source.read(buffer)
mut result = 0u64
for byte in buffer {
result <<= 8
result += byte as! u64
}
return result
}
 
fn main() {
while true {
let n = random() % 20
println("{}", n)
if n == 10 {
break
}
 
println("{}", random() % 20)
}
}
</syntaxhighlight>
 
=={{header|Java}}==
Line 2,063 ⟶ 2,243:
println(Random.nextInt(20))
}
}</syntaxhighlight>
 
A more compact version:
 
<syntaxhighlight lang="kotlin">fun main() {
while ((0..19).random().also { println(it) } != 10)
println((0..19).random())
}</syntaxhighlight>
 
Line 2,076 ⟶ 2,263:
{loops_break 0}
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
loop {
$a = fn.randRange(20)
fn.printf(%2d, $a)
if($a === 10) {
fn.println()
con.break
}
$b = fn.randRange(20)
fn.printf(\s- %2d%n, $b)
}
</syntaxhighlight>
 
Line 2,086 ⟶ 2,290:
write .i, " "
if .i == 10 { writeln(); break }
write random(0..19), " "
}</syntaxhighlight>
 
Line 2,460 ⟶ 2,663:
40 PRINT RND(20)
50 GOTO 10</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
while true {
let a = random int 0..19
print $a
if $a == 10 {break}
print (random int 0..19)
}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 3,042 ⟶ 3,255:
end
</syntaxhighlight>
 
=={{header|RPL}}==
RPL does not have any <code>BREAK</code> command. Flags are of great help to exit loops:
≪ 1 CF
'''WHILE''' 1 FC? '''REPEAT'''
RAND 20 * IP
DUP 1 DISP
'''IF''' 10 == '''THEN''' 1 SF '''ELSE'''
RAND 20 * IP
2 DISP '''END'''
'''END'''
The error handling mechanism provides another way to break a loop:
≪ '''IFERR'''
'''WHILE''' 1 '''REPEAT'''
RAND 20 * IP DUP 1 DISP
'''IF''' 10 == '''THEN''' 0 DUP / '''END'''
RAND 20 * IP 2 DISP
'''END'''
'''THEN''' DROP2 '''END'''
 
=={{header|Ruby}}==
Line 3,346 ⟶ 3,580:
8 13 1 7 19 1 15 16 9 6 5 9 1 15 5 0 6 3 9 19 8 9 10
</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "loopsbreak" )
@( description, "Show a loop which prints random numbers (each number newly" )
@( description, "generated each loop) from 0 to 19 (inclusive). If a number is" )
@( description, "10, stop the loop after printing it, and do not generate any" )
@( description, "further numbers. Otherwise, generate and print a second random" )
@( description, "number before restarting the loop. If the number 10 is never" )
@( description, "generated as the first number in a loop, loop forever. " )
@( category, "tutorials" )
@( author, "Ken O. Burtch" )
@( see_also, "http://rosettacode.org/wiki/Loops/Break" );
pragma license( unrestricted );
 
pragma software_model( nonstandard );
pragma restriction( no_external_commands );
 
procedure arraysloop is
a : positive;
b : positive;
begin
loop
a := numerics.rnd( 20 );
put_line( strings.image( a ) );
exit when a = 10;
b := numerics.rnd( 20 );
put_line( strings.image( b ) );
end loop;
end arraysloop;</syntaxhighlight>
 
=={{header|SPL}}==
Line 3,643 ⟶ 3,908:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var r = Random.new()
885

edits