Loops/Break: Difference between revisions

13,488 bytes added ,  2 months ago
No edit summary
 
(33 intermediate revisions by 21 users not shown)
Line 30:
=={{header|11l}}==
{{trans|Python}}
<langsyntaxhighlight lang="11l">L
V a = random:(20)
print(a)
Line 36:
L.break
V b = random:(20)
print(b)</langsyntaxhighlight>
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Loops Break 15/02/2017
LOOPBREA CSECT
USING LOOPBREA,R13 base register
Line 77:
XDEC DS CL12
YREGS
END LOOPBREA</langsyntaxhighlight>
{{out}}
<pre>
Line 94:
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR LoopBreakSub). Specific OS/hardware routines for generating random numbers and printing are left unimplemented.
<langsyntaxhighlight lang="6502asm">LoopBreakSub: PHA ;push accumulator onto stack
 
 
Line 110:
 
Break: PLA ;restore accumulator from stack
RTS ;return from subroutine</langsyntaxhighlight>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopbreak64.s */
Line 210:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 227:
</pre>
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE v
 
Line 239:
OD
PrintE("After loop")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer]
Line 257:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
 
Line 274:
Put_Line (Value_Type'Image (B));
end loop;
end Test_Loop_Break;</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer
main(void)
{
Line 296:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
<langsyntaxhighlight lang="algol60">'BEGIN' 'COMMENT' Loops/Break - ALGOL60 - 18/06/2018;
'INTEGER' SEED;
'INTEGER' 'PROCEDURE' RANDOM(N);
Line 324:
LAB:
SYSACT(1,14,1); 'COMMENT' skip line;
'END'</langsyntaxhighlight>
{{out}}
<pre>
Line 336:
{{wont work with|ALGOL 68G|Any - in a68G next random takes no seed argument}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}}
<langsyntaxhighlight lang="algol68">main: (
INT a, b;
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ #
Line 348:
OD;
print(new line)
)</langsyntaxhighlight>
{{out}}
<pre style="height:25ex;overflow:scroll">
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>
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">repeat
set a to random number from 0 to 19
if a is 10 then
Line 391 ⟶ 455:
set b to random number from 0 to 19
log a & b
end repeat</langsyntaxhighlight>
 
 
Line 439 ⟶ 503:
 
=={{header|Arc}}==
<langsyntaxhighlight Arclang="arc">(point break
(while t
(let x (rand 20)
Line 445 ⟶ 509:
(if (is x 10)
(break)))
(prn "b: " (rand 20))))</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 635 ⟶ 699:
 
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">while ø [
a: random 0 19
prints [a ""]
Line 647 ⟶ 711:
print b
]
print ""</langsyntaxhighlight>
 
{{out}}
Line 664 ⟶ 728:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop
<lang AutoHotkey>Loop
{
Random, var, 0, 19
Line 673 ⟶ 737:
output = %output%`n%var%
}
MsgBox % output</langsyntaxhighlight>
 
=={{header|Avail}}==
<langsyntaxhighlight Availlang="avail">rng ::= a pRNG;
checked : [0..19];
Do [
Line 683 ⟶ 747:
] while checked ≠ 10 alternate with [
Print: " " ++ “rng's next [0..19]” ++ "\n";
];</langsyntaxhighlight>
 
This demonstrates two interesting Avail features: the ''alternate with'' loop structures, which provide two separate code blocks that are run with a check in between, and the random number generator's ability to pick an item from the ranger of a given number type (<code>[0..19]</code> is an expression generating a ''type'' whose values are integers in the range 0-19 inclusive).
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">BEGIN {
srand() # randomize the RNG
for (;;) {
print n = int(rand() * 20)
Line 695 ⟶ 760:
print int(rand() * 20)
}
}</langsyntaxhighlight>
 
=={{header|Axe}}==
Because Axe only supports breaking out of loops as end conditions, the behavior must be simulated using a return statement. Note, however, that this will exit the current call context, not the necessarily just the current loop.
 
<langsyntaxhighlight lang="axe">While 1
rand^20→A
Disp A▶Dec
Line 706 ⟶ 771:
rand^20→B
Disp B▶Dec,i
End</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<langsyntaxhighlight lang="gwbasic">FOR I = 0 TO 1 STEP 0 : N = INT(RND(1) * 20) : PRINT " "N; : IF N <> 10 THEN ? ","INT(RND(1) * 20); : NEXT</langsyntaxhighlight>
==={{header|BaCon}}===
<langsyntaxhighlight lang="freebasic">
REPEAT
number = RANDOM(20)
Line 720 ⟶ 785:
ENDIF
PRINT "second ",RANDOM(20)
UNTIL FALSE</langsyntaxhighlight>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">do
<lang BASIC256>do
i = int(rand * 19)
print i; " ";
Line 731 ⟶ 796:
until false
print
end</langsyntaxhighlight>
 
==={{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}}===
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive).
<langsyntaxhighlight lang="commodorebasic">10 X = RND(-TI) : REM SEED RN GENERATOR
20 A = INT(RND(1)*20)
30 PRINT A
Line 742 ⟶ 839:
60 PRINT B
70 GOTO 20
80 END</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 RANDOMIZE
110 DO
120 LET A=RND(20)+1
Line 751 ⟶ 848:
140 IF A=10 THEN EXIT DO
150 PRINT RND(20)+1
160 LOOP</langsyntaxhighlight>
 
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">do
a = int(rnd * 20)
print a
Line 761 ⟶ 858:
b = int(rnd * 20)
print b
loop</langsyntaxhighlight>
 
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="basic">RANDOMIZE
 
DO
Line 777 ⟶ 874:
LOOP
PRINT
END</langsyntaxhighlight>
 
 
==={{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:
 
<langsyntaxhighlight lang="zxbasic">10 FOR l = 1 TO 20
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
30 PRINT l
40 NEXT l
50 STOP</langsyntaxhighlight>
 
The correct solution:
 
<langsyntaxhighlight lang="zxbasic">10 LET a = INT (RND * 20)
20 PRINT a
30 IF a = 10 THEN STOP
40 PRINT INT (RND * 20)
50 GO TO 10</langsyntaxhighlight>
 
=={{header|Batch File}}==
<langsyntaxhighlight lang="dos">@echo off
:loop
set /a N=%RANDOM% %% 20
Line 805 ⟶ 910:
set /a N=%RANDOM% %% 20
echo %N%
goto loop</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> REPEAT
num% = RND(20)-1
PRINT num%
IF num%=10 THEN EXIT REPEAT
PRINT RND(20)-1
UNTIL FALSE</langsyntaxhighlight>
 
=={{header|bc}}==
<langsyntaxhighlight lang="bc">s = 1 /* seed of the random number generator */
scale = 0
 
Line 839 ⟶ 944:
r() /* print 2nd number */
}
quit</langsyntaxhighlight>
 
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">
<lang Befunge>
>60v *2\<
>?>\1-:|
Line 854 ⟶ 959:
>^ 7
^ .%++67<
</syntaxhighlight>
</lang>
 
=={{header|C}}==
 
<syntaxhighlight lang="c">
<lang c>
int main(){
time_t t;
Line 872 ⟶ 977:
}
return 0;
}</langsyntaxhighlight>
Output (example):
<pre>
Line 888 ⟶ 993:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">class Program
{
static void Main(string[] args)
Line 905 ⟶ 1,010:
Console.ReadLine();
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <ctime>
#include <cstdlib>
Line 923 ⟶ 1,028:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|Chapel}}==
<langsyntaxhighlight lang="chapel">use Random;
 
var r = new RandomStream();
Line 936 ⟶ 1,041:
writeln(b);
}
delete r;</langsyntaxhighlight>
 
=={{header|Chef}}==
"Liquify" is now depreciated in favor of "Liquefy", but my interpreter/compiler ([http://search.cpan.org/~smueller/Acme-Chef/ Acme::Chef]) works only with "Liquify" so that's how I'm leaving it. At least it'll work no matter which version you use.
<div style='width:full;overflow:scroll'>
<langsyntaxhighlight Cheflang="chef">Healthy Vita-Sauce Loop - Broken.
 
Makes a whole lot of sauce for two people.
Line 998 ⟶ 1,103:
Pour contents of the 1st mixing bowl into the 1st baking dish.
 
Serves 2.</langsyntaxhighlight>
</div>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(loop [[a b & more] (repeatedly #(rand-int 20))]
(println a)
(when-not (= 10 a)
(println b)
(recur more)))</langsyntaxhighlight>
 
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Random-Nums.
 
Line 1,035 ⟶ 1,140:
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20)
DISPLAY Num
.</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this:
<langsyntaxhighlight lang="coffeescript">
loop
print a = Math.random() * 20 // 1
break if a == 10
print Math.random() * 20 // 1
</syntaxhighlight>
</lang>
 
=={{header|ColdFusion}}==
<langsyntaxhighlight lang="cfm">
<Cfset randNum = 0>
<cfloop condition="randNum neq 10">
Line 1,056 ⟶ 1,161:
<Br>
</cfloop>
</syntaxhighlight>
</lang>
{{out}}
My first two test outputs (I swear this is true)
Line 1,124 ⟶ 1,229:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(loop for a = (random 20)
do (print a)
until (= a 10)
do (print (random 20)))</langsyntaxhighlight>
 
=== Using DO ===
<langsyntaxhighlight lang="lisp">
(do ((a (random 20) (random 20))) ; Initialize to rand and set new rand on every loop
((= a 10) (write a)) ; Break condition and last step
(format t "~a~3T~a~%" a (random 20))) ; On every loop print formated `a' and rand `b'
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,157 ⟶ 1,262:
10
</pre>
 
=={{header|Emacs Lisp}}==
<lang lisp>(defun get-result (param)
;; According to the emacs lisp maual, the definition of ``catch'' is:
;; (catch 'symbol ..BODY...)
;; it returns either the last expression evaluated in BODY or the value given by ``throw''
(catch 'loop-break
(if (string= param "break")
(throw 'loop-break "breaked")
"pass") ) )</lang>
 
Example:
 
<lang lisp>(defun get-result (param)
(catch 'loop-break
(if (string= param "break")
(throw 'loop-break "breaked")
"pass") ) )
 
(get-result "break")
;; returns "breaked"
 
(get-result "break")
;; returns "pass"
 
</lang>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.random;
 
void main() {
Line 1,195 ⟶ 1,274:
write(uniform(0, 20), " ");
}
}</langsyntaxhighlight>
{{out}}
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre>
Line 1,201 ⟶ 1,280:
=={{header|dc}}==
{{trans|bc}}
<langsyntaxhighlight lang="dc">1 ss [s = seed of the random number generator]sz
0k [scale = 0]sz
 
Line 1,225 ⟶ 1,304:
0 0 =r p sz [Print 2nd number.]sz
0 0 =B [Continue loop.]sz
]sB 0 0 =B</langsyntaxhighlight>
 
=={{header|Delphi}}==
 
<langsyntaxhighlight Delphilang="delphi">program Project5;
 
{$APPTYPE CONSOLE}
Line 1,245 ⟶ 1,324:
end.
 
</syntaxhighlight>
</lang>
 
=={{header|DWScript}}==
 
<langsyntaxhighlight lang="delphi">
while True do begin
var num := RandomInt(20);
PrintLn(num);
if num=10 then Break;
end;</langsyntaxhighlight>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">while (true) {
def a := entropy.nextInt(20)
print(a)
Line 1,265 ⟶ 1,344:
}
println(" ", entropy.nextInt(20))
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
<lang>repeat
repeat
a = random 20
a = randint 20
print a
until print a = 10
until a = 10
print random 20
print randint 20
.</lang>
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
example
-- Eiffel example code
Line 1,292 ⟶ 1,373:
end
end
</syntaxhighlight>
</lang>
{{out}}
The output is superfluous and unneeded to read and understand what the Eiffel code is doing.
Line 1,304 ⟶ 1,385:
As a result, an output looks almost truly random:
 
<langsyntaxhighlight lang="ela">open datetime random monad io
loop = loop' 1
Line 1,315 ⟶ 1,396:
 
 
loop 10 ::: IO</langsyntaxhighlight>
 
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<langsyntaxhighlight lang="elixir">defmodule Loops do
def break, do: break(random)
Line 1,331 ⟶ 1,412:
end
 
Loops.break</langsyntaxhighlight>
 
{{out}}
Line 1,349 ⟶ 1,430:
1, 17
13, 5
10
</pre>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">(defun wait_10 ()
(catch 'loop-break
(while 't
(let ((math (random 19)))
(if (= math 10)
(progn (message "Found value: %d" math)
(throw 'loop-break math))
(message "current number is: %d" math) ) ) ) ) )
 
(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}}==
<langsyntaxhighlight lang="erlang">%% Implemented by Arjun Sunel
-module(forever).
-export([main/0, for/0]).
Line 1,370 ⟶ 1,479:
for()
end.
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
LOOP
A=INT(RND(1)*20)
Line 1,380 ⟶ 1,489:
PRINT(INT(RND(1)*20))
END LOOP
</syntaxhighlight>
</lang>
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument.
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">integer i
while 1 do
i = rand(20) - 1
Line 1,392 ⟶ 1,501:
end if
printf(1, "%g ", {rand(20)-1})
end while</langsyntaxhighlight>
The <code>rand()</code> function returns a random integer from 1 to the integer provided.
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Loops/Break. Nigel Galloway: February 21st., 2022
let n=System.Random()
let rec fN g=printf "%d " g; if g <> 10 then fN(n.Next(20))
fN(n.Next(20))
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
Using <code>with-return</code>:
<langsyntaxhighlight lang="factor">[
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop
] with-return</langsyntaxhighlight>
 
Idiomatic Factor:
<langsyntaxhighlight lang="factor">[ 20 random [ . ] [ 10 = not ] bi dup [ 20 random . ] when ] loop</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class ForBreak
{
Line 1,428 ⟶ 1,537:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">include random.fs
 
: main
Line 1,444 ⟶ 1,553:
10 = if leave then
i random .
loop ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program Example
implicit none
 
Line 1,464 ⟶ 1,573:
end do
 
end program Example</langsyntaxhighlight>
 
{{works with|Fortran|77 and later}}
<langsyntaxhighlight lang="fortran"> PROGRAM LOOPBREAK
INTEGER I, RNDINT
 
Line 1,535 ⟶ 1,644:
ENDIF
RETURN
END</langsyntaxhighlight>
 
{{works with|Fortran|66 and earlier}}
Line 1,543 ⟶ 1,652:
 
The RANDU routine is so notorious that latter-day compilers can supply their own RANDU (using a better method), and further, disregard a user-supplied RANDU routine so it may have to be called RANDUU or some other name!
<syntaxhighlight lang="fortran">
<lang Fortran>
SUBROUTINE RANDU(IX,IY,YFL)
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60.
Line 1,575 ⟶ 1,684:
20 CONTINUE
END
</syntaxhighlight>
</lang>
Output, converted to along the line:
0 13 4 19 1 7 2 12 4 7 14 11 6 4 0 9 5 12 16 19 18 2 0 13 2 7 10
Line 1,581 ⟶ 1,690:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim i As Integer
Line 1,596 ⟶ 1,705:
 
Print
Sleep</langsyntaxhighlight>
 
Sample output
Line 1,604 ⟶ 1,713:
6 12 2 16 5 19 9 6 16 1 16 10 1 4 18 3 2 9 19 0
19 13 0 0 12 17 13 12 18 10 8 13 9 5 14 7 10
</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">while true
{
a = random[0,19]
print["$a "]
if a == 10
break
 
b = random[0,19]
print["$b "]
}</syntaxhighlight>
{{out}}
<pre>
3 8 8 8 11 6 3 2 10
</pre>
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">include "NSLog.incl"
 
long num
Line 1,615 ⟶ 1,740:
until ( num == 10 )
 
HandleEvents</langsyntaxhighlight>
 
=={{header|Gambas}}==
<langsyntaxhighlight lang="gambas">Public Sub Form_Open()
Dim iRand As Integer
 
Line 1,626 ⟶ 1,751:
Until iRand = 10
 
End</langsyntaxhighlight> =={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim byNo As Byte
 
Line 1,639 ⟶ 1,764:
Loop
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,646 ⟶ 1,771:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">while true do
a := Random(0, 19);
Print(a);
Line 1,662 ⟶ 1,787:
# 5 10
# 1 16
# 10</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight GMLlang="gml">while(1)
{
a = floor(random(19))
Line 1,674 ⟶ 1,821:
show_message(string(a))
}
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,694 ⟶ 1,841:
fmt.Println(b)
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">final random = new Random()
 
while (true) {
Line 1,705 ⟶ 1,852:
print ' '
println random.nextInt(20)
}</langsyntaxhighlight>
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="qbasic">10 NUM = 0
20 WHILE NUM <> 10
30 NUM = INT(RND * 20)
40 PRINT NUM
50 WEND</langsyntaxhighlight>
 
=={{header|Harbour}}==
<langsyntaxhighlight lang="visualfoxpro">PROCEDURE Loop()
 
LOCAL n
Line 1,727 ⟶ 1,874:
ENDDO
 
RETURN</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad
import System.Random
 
Line 1,738 ⟶ 1,885:
unless (r==k) $ do
print =<< randomRIO (0,n)
loopBreak n k</langsyntaxhighlight>
Use:
<syntaxhighlight lang ="haskell">loopBreak 19 10</langsyntaxhighlight>
 
=={{header|Haxe}}==
<langsyntaxhighlight lang="haxe">class Program {
static public function main():Void {
while(true) {
Line 1,754 ⟶ 1,901:
}
}
}</langsyntaxhighlight>
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">while true
let r rand 20
println r
Line 1,764 ⟶ 1,911:
endif
println rand 20
endwhile</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">1 DO i = 1, 1E20 ! "forever"
a = INT( RAN(10, 10) )
WRITE(name) a
Line 1,775 ⟶ 1,922:
ENDDO
10
END</langsyntaxhighlight>
 
=={{header|HolyC}}==
 
<langsyntaxhighlight lang="holyc">U16 a, b;
while (1) {
a = RandU16 % 20;
Line 1,789 ⟶ 1,936:
Print("%d\n", b);
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main()
while 10 ~= writes(?20-1) do write(", ",?20-1)
end</langsyntaxhighlight>
Notes:
* For any positive integer i, ?i produces a value j where 1 <= j <= i
* Although this can be written with a break (e.g. repeat expression & break), there is no need to actually use one. (And it's ugly).
* 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 <langsyntaxhighlight Iconlang="icon">&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))</langsyntaxhighlight> 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}}==
<langsyntaxhighlight lang="io">loop(
a := Random value(0,20) floor
write(a)
Line 1,808 ⟶ 1,963:
b := Random value(0,20) floor
writeln(" ",b)
)</langsyntaxhighlight>
 
=={{header|J}}==
In recent versions of J, [[j:Vocabulary/zcapco|Z:]] can be used to provide early termination from a [[j:Vocabulary/fcap|fold]]. For example:
<lang j>loopexample=: verb define
 
while. 1 do.
<syntaxhighlight lang=J> ]F.(( _2 Z: 10&= [ echo)@(?@20))''
smoutput n=. ?20
15
if. 10=n do. return. end.
6
smoutput ?20
5
10
 
]F.(( _2 Z: 10&= [ echo)@(?@20))''
14
9
3
8
19
14
5
13
8
1
19
2
10
 
</syntaxhighlight>
 
But other mechanisms are also supported:
 
<syntaxhighlight lang="j">loopexample=: {{
while. do.
echo k=. ?20
if. 10=k do. return. end.
echo ?20
end.
}}</syntaxhighlight>
)</lang>
 
Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>.:
 
<syntaxhighlight lang="j">loopexample2=: verb define
while. do.
echo k=. ?20
if. 10=k do. break. end.
echo ?20
end.
)</syntaxhighlight>
 
<syntaxhighlight lang="j">loopexample3=: {{
while. do.
echo k=. ?20
if. 10=k do. goto_done. end.
echo ?20
end.
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}}==
<langsyntaxhighlight lang="java">import java.util.Random;
 
Random rand = new Random();
Line 1,831 ⟶ 2,058:
int b = rand.nextInt(20);
System.out.println(b);
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">for (;;) {
var a = Math.floor(Math.random() * 20);
print(a);
Line 1,841 ⟶ 2,068:
a = Math.floor(Math.random() * 20);
print(a);
}</langsyntaxhighlight>
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell.
 
Line 1,849 ⟶ 2,076:
In a functional idiom of JavaScript, we might instead write something like:
 
<langsyntaxhighlight JavaScriptlang="javascript">(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
Line 1,861 ⟶ 2,088:
return streamTillInitialTen();
})();</langsyntaxhighlight>
 
Obtaining runs like:
Line 1,902 ⟶ 2,129:
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread:
 
<langsyntaxhighlight JavaScriptlang="javascript">console.log(
(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
Line 1,915 ⟶ 2,142:
);
})().join('\n')
);</langsyntaxhighlight>
 
Sample result:
Line 1,943 ⟶ 2,170:
 
Currently, jq does not have a built-in random-number generator, so here we borrow one of the linear congruential generators defined at https://rosettacode.org/wiki/Linear_congruential_generator -
<langsyntaxhighlight lang="jq"># 15-bit integers generated using the same formula as rand()
# from the Microsoft C Runtime.
# Input: [ count, state, rand ]
Line 1,958 ⟶ 2,185:
 
# Generate random integers from 0 to (n-1):
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</langsyntaxhighlight>
 
'''"take"'''
 
<langsyntaxhighlight lang="jq">def take(s; cond):
label $done
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</langsyntaxhighlight>
 
'''"count"'''
 
Since the PRNG used here is deterministic, we'll just count the number of integers generated:
<langsyntaxhighlight lang="jq">def count(s): reduce s as $i (0; . + 1);</langsyntaxhighlight>
 
'''Example'''
Line 1,977 ⟶ 2,204:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
while true
n = rand(0:19)
Line 1,988 ⟶ 2,215:
@printf "%4d\n" n
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,007 ⟶ 2,234:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">import kotlin.random.Random
{{trans|Java}}
<lang scala>import java.util.Random
 
fun main(args: Array<String>) {
val rand = Random()
while (true) {
val a = randRandom.nextInt(20)
println(a)
if (a == 10) break
println(randRandom.nextInt(20))
}
}</langsyntaxhighlight>
 
A more compact version:
 
<syntaxhighlight lang="kotlin">fun main() {
while ((0..19).random().also { println(it) } != 10)
println((0..19).random())
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
<Langsyntaxhighlight lang="scheme">
{def loops_break
{lambda {:n}
Line 2,031 ⟶ 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>
</lang>
 
=={{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>
 
=={{header|Lang5}}==
<langsyntaxhighlight lang="lang5">do 20 ? int dup . 10 == if break then 20 ? int . loop</langsyntaxhighlight>
 
=={{header|langur}}==
<langsyntaxhighlight lang="langur">for {
val .i = random 0..19
write .i, " "
if .i == 10 { writeln(); break }
}</syntaxhighlight>
write random(0..19), " "
}</lang>
 
{{out}}
Line 2,048 ⟶ 2,296:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(x = 0)
while(#x != 10) => {^
#x = integer_random(19,0)
Line 2,054 ⟶ 2,302:
#x == 10 ? loop_abort
', '+integer_random(19,0)+'\r'
^}</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
The task specifies a "number".
<langsyntaxhighlight lang="lb">while num<>10
num=rnd(1)*20
print num
Line 2,064 ⟶ 2,312:
print rnd(1)*20
wend
</langsyntaxhighlight>If "integer" was meant, this code fulfils that requirement.
<langsyntaxhighlight lang="lb">while num<>10
num=int(rnd(1)*20)
print num
Line 2,071 ⟶ 2,319:
print int(rnd(1)*20)
wend
</syntaxhighlight>
</lang>
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">repeat while TRUE
n = random(20)-1
put n
if n = 10 then exit repeat
put random(20)-1
end repeat</langsyntaxhighlight>
 
=={{header|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">Section Header
 
+ name := TEST_LOOP_BREAK;
Line 2,102 ⟶ 2,350:
'\n'.print;
}
);</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">command loopForeverRandom
repeat forever
put random(20) - 1 into tRand
Line 2,113 ⟶ 2,361:
end repeat
end loopForeverRandom
</syntaxhighlight>
</lang>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">repeat
k = math.random(19)
print(k)
if k == 10 then break end
print(math.random(19)
until false</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
We use block of module to loop. Break also can be used, but breaks nested blocks (without crossing modules/functions). Using break in second Checkit module we break three blocks.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
M=Random(0, 19)
Line 2,152 ⟶ 2,400:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`randSeed',141592653)dnl
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
Line 2,166 ⟶ 2,414:
loopbreak')')dnl
dnl
loopbreak</langsyntaxhighlight>
 
{{out}}
Line 2,178 ⟶ 2,426:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">r := rand( 0 .. 19 ):
do
n := r();
Line 2,186 ⟶ 2,434:
end if;
printf( "%d\n", r() );
end do:</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">While[(Print[#];#!=10)&[RandomIntger[{0,19}]],
Print[RandomInteger[{0,19}]
]</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* To exit the innermost block, use return(<value>) */
 
block([n],
Line 2,229 ⟶ 2,477:
> x: 2;
> exit;
2</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
while true do
(
Line 2,241 ⟶ 2,489:
format ("B: % \n") b
)
</syntaxhighlight>
</lang>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">randomize
(19 random puts 10 ==) (19 random puts!) () () linrec</langsyntaxhighlight>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">СЧ 2 0 * П0
1 0 - [x] x#0 18
СЧ 2 0 * П1
БП 00 ИП0 С/П</langsyntaxhighlight>
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE Break EXPORTS Main;
 
IMPORT IO, Fmt, Random;
Line 2,271 ⟶ 2,519:
END;
END;
END Break.</langsyntaxhighlight>
 
=={{header|MOO}}==
<langsyntaxhighlight lang="moo">while (1)
a = random(20) - 1;
player:tell(a);
Line 2,282 ⟶ 2,530:
b = random(20) - 1;
player:tell(b);
endwhile</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">BREAKLOOP
NEW A,B
SET A=""
Line 2,299 ⟶ 2,547:
NEW A,B
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B
KILL A,B QUIT</langsyntaxhighlight>
{{out}}
<pre>USER>D BREAKLOOP^ROSETTA
Line 2,324 ⟶ 2,572:
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Loops/Break in Neko
Tectonics:
Line 2,345 ⟶ 2,593:
$print(r, " ");
}
$print("\n");</langsyntaxhighlight>
 
{{out}}
Line 2,354 ⟶ 2,602:
=={{header|Nemerle}}==
{{trans|C#}}
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
using Nemerle.Imperative;
Line 2,372 ⟶ 2,620:
}
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
Line 2,391 ⟶ 2,639:
end lb
say
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(until (= 10 (println (rand 20)))
(println (rand 20)))</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import random
 
while true:
Line 2,407 ⟶ 2,655:
break
let b = random(19)
echo b</langsyntaxhighlight>
 
=={{header|NS-HUBASIC}}==
<langsyntaxhighlight NSlang="ns-HUBASIChubasic">10 I=RND(20)
20 PRINT I
30 IF I=10 THEN STOP
40 PRINT RND(20)
50 GOTO 10</langsyntaxhighlight>
 
=={{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}}==
Works with oo2c Version 2
<langsyntaxhighlight lang="oberon2">
MODULE LoopBreak;
IMPORT
Line 2,440 ⟶ 2,698:
Do
END LoopBreak.
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
while(true) {
a := (Float->Random() * 20.0)->As(Int);
Line 2,453 ⟶ 2,711:
a->PrintLine();
}
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml"># Random.self_init();;
- : unit = ()
 
Line 2,473 ⟶ 2,731:
13
10
Exception: Pervasives.Exit.</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">while(1)
a = floor(unifrnd(0,20, 1));
disp(a)
Line 2,484 ⟶ 2,742:
b = floor(unifrnd(0,20, 1));
disp(b)
endwhile</langsyntaxhighlight>
 
=={{header|Oforth}}==
<langsyntaxhighlight Oforthlang="oforth">while(true) [
19 rand dup print ":" print
10 == ifTrue: [ break ]
19 rand print " " print
]</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(import (otus random!))
 
Line 2,503 ⟶ 2,761:
(print (rand! 20))
(loop))))
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">/*REXX ****************************************************************
* Three Ways to leave a Loop
* ooRexx added the possibility to leave an outer loop
Line 2,550 ⟶ 2,808:
End
Else
Say 'Leave label-name is probably not supported in' v</langsyntaxhighlight>
{{out}}
<pre>i1=1
Line 2,577 ⟶ 2,835:
=={{header|Oz}}==
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic.
<langsyntaxhighlight lang="oz">for break:Break do
R = {OS.rand} mod 20
in
Line 2,584 ⟶ 2,842:
else {Show {OS.rand} mod 20}
end
end</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">while(1,
t=random(20);
print(t);
if(t==10, break);
print(random(20))
)</langsyntaxhighlight>
 
=={{header|Pascal}}==
Line 2,598 ⟶ 2,856:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">while (1) {
my $a = int(rand(20));
print "$a\n";
Line 2,606 ⟶ 2,864:
my $b = int(rand(20));
print "$b\n";
}</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 2,613 ⟶ 2,871:
The rand() function returns a random integer from 1 to the integer provided.
 
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
Line 2,621 ⟶ 2,879:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 2,630 ⟶ 2,888:
10
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/Break
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
def ran rand * int enddef
 
true while
20 ran
dup print "\t" print
10 == if false else 20 ran ? true endif
endwhile</syntaxhighlight>
{{out}}
<pre>12 8
12 10
5 7
10
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">while (true) {
$a = rand(0,19);
echo "$a\n";
Line 2,639 ⟶ 2,917:
$b = rand(0,19);
echo "$b\n";
}</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Literally:
<langsyntaxhighlight PicoLisplang="picolisp">(use R
(loop
(println (setq R (rand 1 19)))
(T (= 10 R))
(println (rand 1 19)) ) )</langsyntaxhighlight>
Shorter:
<langsyntaxhighlight PicoLisplang="picolisp">(until (= 10 (println (rand 1 19)))
(println (rand 1 19)) )</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">int main(){
while(1){
int a = random(20);
Line 2,663 ⟶ 2,941:
write(b + "\n");
}
}</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
do forever;
k = trunc(random()*20);
Line 2,674 ⟶ 2,952:
put skip list (k);
end;
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Demonstrate breaking.
Line 2,693 ⟶ 2,971:
To write a number to the console:
Convert the number to a string.
Write the string to the console.</langsyntaxhighlight>
 
=={{header|PostScript}}==
<langsyntaxhighlight lang="postscript">realtime srand % init RNG
{
rand 20 mod % generate number between 0 and 19
dup = % print it
10 eq { exit } if % exit if 10
} loop</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">$r = New-Object Random
for () {
$n = $r.Next(20)
Line 2,712 ⟶ 2,990:
}
Write-Host $r.Next(20)
}</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">If OpenConsole()
 
Repeat
Line 2,731 ⟶ 3,009:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from random import randrange
 
while True:
Line 2,742 ⟶ 3,020:
break
b = randrange(20)
print(b)</langsyntaxhighlight>
 
=={{header|QB64}}==
''CBTJD'': 2020/03/14
<langsyntaxhighlight lang="qbasic">RANDOMIZE TIMER
DO
n = INT(RND * 20)
Line 2,756 ⟶ 3,034:
PRINT n
END IF
LOOP UNTIL 0</langsyntaxhighlight>
 
=={{header|Qi}}==
<syntaxhighlight lang="qi">
<lang qi>
(define loop -> (if (= 10 (PRINT (random 20)))
true
Line 2,765 ⟶ 3,043:
(loop))))
(loop)
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery">[ 20 random
dup echo sp
10 = if done
20 random echo cr
again ]</langsyntaxhighlight>
 
{{Out}}
Line 2,789 ⟶ 3,067:
=={{header|R}}==
{{works with|R|2.8.1}}
<langsyntaxhighlight Rlang="r">sample0to19 <- function() sample(0L:19L, 1,replace=TRUE)
repeat
{
Line 2,800 ⟶ 3,078:
result2 <- sample0to19()
cat(result1, result2, "\n")
}</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(let loop ()
Line 2,812 ⟶ 3,090:
(displayln (random 20))
(loop)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#21 "Seattle"}}
<syntaxhighlight lang="raku" perl6line>loop {
say my $n = (0..19).pick;
last if $n == 10;
say (0..19).pick;
}</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">REBOL [
Title: "Loop/Break"
URL: http://rosettacode.org/wiki/Loop/Break
Line 2,839 ⟶ 3,117:
print rejoin [" " r20]
]
print ""</langsyntaxhighlight>
 
{{out}}
Line 2,851 ⟶ 3,129:
=={{header|Red}}==
{{trans|REBOL}}
<langsyntaxhighlight lang="rebol">Red [
Title: "Loops/Break"
URL: http://rosettacode.org/wiki/Loops/Break
Line 2,865 ⟶ 3,143:
print rejoin [" " r20]
]
print ""</langsyntaxhighlight>
{{out}}
<pre>
Line 2,877 ⟶ 3,155:
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">doc{
A couple of helper functions to make the rest of the
code more readable.
Line 2,899 ⟶ 3,177:
 
[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while
</syntaxhighlight>
</lang>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates a FOREVER DO loop with a test to LEAVE (break). */
/*REXX's RANDOM BIF returns an integer.*/
do forever /*perform loop until da cows come home.*/
Line 2,911 ⟶ 3,189:
say right(b, 5) /*show B right─justified, column 2.*/
end /*forever*/ /* [↑] CHAROUT , xxx writes to term.*/
/*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output}}
 
Line 2,970 ⟶ 3,248:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
while true
a = random(20)
Line 2,976 ⟶ 3,254:
if a = 10 exit ok
end
</syntaxhighlight>
</lang>
 
=={{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}}==
<langsyntaxhighlight lang="ruby">loop do
a = rand(20)
print a
Line 2,988 ⟶ 3,287:
b = rand(20)
puts "\t#{b}"
end</langsyntaxhighlight>
or
<langsyntaxhighlight lang="ruby">loop do
print a = rand(20)
puts or break if a == 10
puts "\t#{rand(20)}"
end</langsyntaxhighlight>
 
{{out}}
Line 3,013 ⟶ 3,312:
=={{header|Rust}}==
{{libheader|rand}}
<langsyntaxhighlight lang="rust">// cargo-deps: rand
 
extern crate rand;
Line 3,028 ⟶ 3,327:
println!("{}", rng.gen_range(0, 20));
}
}</langsyntaxhighlight>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">data _null_;
do while(1);
n=floor(uniform(0)*20);
Line 3,037 ⟶ 3,336:
if n=10 then leave; /* 'leave' to break a loop */
end;
run;</langsyntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">-- help class for random number sequence
class RANDOM is
attr seed:INT;
Line 3,068 ⟶ 3,367:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">scala> import util.control.Breaks.{breakable, break}
import util.control.Breaks.{breakable, break}
 
Line 3,090 ⟶ 3,389:
4
10
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">
(let loop ((first (random 20)))
(print first)
Line 3,100 ⟶ 3,399:
(print (random 20))
(loop (random 20)))))
</syntaxhighlight>
</lang>
 
Or by using call/cc to break out:
 
<langsyntaxhighlight lang="scheme">
(call/cc
(lambda (break)
Line 3,113 ⟶ 3,412:
(print (random 20))
(loop (random 20)))))
</syntaxhighlight>
</lang>
 
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">while %T
a=int(rand()*20) // [0..19]
printf("%2d ",a)
Line 3,124 ⟶ 3,423:
printf("%2d\n",b)
end
printf("\n")</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">
Line 3,144 ⟶ 3,443:
is 10. The second random number does never terminate the loop.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 3,157 ⟶ 3,456:
end if;
until number = 10;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var lim = 20;
loop {
say (var n = lim.rand.int);
n == 10 && break;
say lim.rand.int;
}</langsyntaxhighlight>
 
=={{header|Simula}}==
{{works with|SIMULA-67}}
<langsyntaxhighlight lang="simula">! Loops/Break - simula67 - 08/03/2017;
begin
integer num,seed;
Line 3,180 ⟶ 3,479:
end;
lab:
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,191 ⟶ 3,490:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">[
|first second done|
 
Line 3,201 ⟶ 3,500:
].
done
] whileFalse</langsyntaxhighlight>
alternative:
<langsyntaxhighlight lang="smalltalk">[:exit |
|first|
 
Line 3,209 ⟶ 3,508:
first == 10 ifTrue:[ exit value:nil ].
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</langsyntaxhighlight>
or shorter (because <tt>ifTrue:</tt> sends #value to its arg):
<langsyntaxhighlight lang="smalltalk">[:exit |
|first|
 
Line 3,217 ⟶ 3,516:
first == 10 ifTrue:exit.
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</langsyntaxhighlight>
 
=={{header|Snabel}}==
Uses a ranged random generator as iterator.
<langsyntaxhighlight lang="snabel">
let: rnd 19 random;
 
Line 3,229 ⟶ 3,528:
@rnd pop str say
} for
</syntaxhighlight>
</lang>
 
=={{header|SNOBOL4}}==
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation:
<langsyntaxhighlight lang="snobol"> input(.random,io_findunit(),1,"/dev/urandom")
while &ALPHABET random @rand
output = rand = rand - (rand / 20) * 20
eq(rand,10) :f(while)
end</langsyntaxhighlight>
 
Or using a library function:
 
<langsyntaxhighlight SNOBOL4lang="snobol4">* rand(n) -> real x | 0 <= x < n
-include 'random.sno'
 
loop ne(output = convert(rand(20)'integer'),10) :s(loop)
end</langsyntaxhighlight>
 
=={{header|Spin}}==
Line 3,252 ⟶ 3,551:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<langsyntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 3,276 ⟶ 3,575:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</langsyntaxhighlight>
{{out}}
<pre>
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}}==
Direct approach:
<langsyntaxhighlight lang="spl">>
n = #.rnd(20)
#.output(n)
Line 3,290 ⟶ 3,620:
n = #.rnd(20)
#.output(n)
<</langsyntaxhighlight>
With reusable code:
<langsyntaxhighlight lang="spl">>
:1
n = #.rnd(20)
Line 3,299 ⟶ 3,629:
<< n=10
1 <->
<</langsyntaxhighlight>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<langsyntaxhighlight lang="sql pl">
--#SET TERMINATOR @
 
Line 3,321 ⟶ 3,651:
END WHILE LOOP;
END @
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,339 ⟶ 3,669:
</pre>
Since V11.1, the builtin module can be used instead of RAND, like this:
<langsyntaxhighlight lang="sql pl">
SET VAL = CALL DBMS_RANDOM.VALUE(0,20);
</syntaxhighlight>
</lang>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">while 1 {
local n=runiformint(0,19)
display `n'
if `n'==10 continue, break
display runiformint(0,19)
}</langsyntaxhighlight>
 
=== Mata ===
<langsyntaxhighlight lang="stata">for (; 1; ) {
printf("%f\n",n=runiformint(1,1,0,19))
if (n==10) break
printf("%f\n",runiformint(1,1,0,19))
}</langsyntaxhighlight>
 
=={{header|Suneido}}==
<langsyntaxhighlight Suneidolang="suneido">forever
{
Print(i = Random(20))
Line 3,366 ⟶ 3,696:
Print(i = Random(20))
}
</syntaxhighlight>
</lang>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">while true
{
let a = Int(arc4random()) % (20)
Line 3,380 ⟶ 3,710:
print("b: \(b)")
}
</langsyntaxhighlight>{{out}}
<pre>
a: 2 b: 7
Line 3,389 ⟶ 3,719:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">while true {
set a [expr int(20*rand())]
puts $a
Line 3,397 ⟶ 3,727:
set b [expr int(20*rand())]
puts $b
}</langsyntaxhighlight>
 
=={{header|TI-89 BASIC}}==
 
<langsyntaxhighlight lang="ti89b">Local x
Loop
rand(20)-1 → x
Line 3,409 ⟶ 3,739:
EndIf
Output 64, 50, rand(20)-1 © paint text to the right on same line
EndLoop</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
 
<langsyntaxhighlight Torquelang="torque">for(%a = 0; %a > -1; %a++)
{
%number = getRandom(0, 19);
if(%number == 10)
break;
}</langsyntaxhighlight>
 
=={{header|Transact-SQL}}==
 
<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
DECLARE @i INT;
WHILE 1=1
Line 3,431 ⟶ 3,761:
PRINT ABS(CHECKSUM(NewId())) % 20;
END;
</syntaxhighlight>
</lang>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
LOOP
Line 3,447 ⟶ 3,777:
IF (10==a,b) STOP
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,461 ⟶ 3,791:
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">Do
n = RND(20)
Print n
Until n = 10
Print RND(20)
Loop</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
Line 3,474 ⟶ 3,804:
{{works with|Bourne Shell}}
{{libheader|jot}}
<langsyntaxhighlight lang="bash">while true; do
a=`jot -w %d -r 1 0 20` || exit $?
echo $a
Line 3,480 ⟶ 3,810:
b=`jot -w %d -r 1 0 20` || exit $?
echo $b
done</langsyntaxhighlight>
 
Korn Shells have a RANDOM parameter.
Line 3,486 ⟶ 3,816:
{{works with|Bash}}
{{works with|pdksh|5.2.14}}
<langsyntaxhighlight lang="bash">while true; do
echo $((a=RANDOM%20))
[ $a -eq 10 ] && break
echo $((b=RANDOM%20))
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
{{trans|Python}}
<langsyntaxhighlight lang="ursa">decl ursa.util.random r
decl int a b
while true
Line 3,504 ⟶ 3,834:
set b (r.getint 19)
out b endl console
end while</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight VBlang="vb">Public Sub LoopsBreak()
Dim value As Integer
Randomize
Line 3,516 ⟶ 3,846:
Debug.Print Int(20 * Rnd)
Loop
End Sub</langsyntaxhighlight>
 
=={{header|VBScript}}==
Based on BASIC version. Demonstrates breaking out of Do/Loop and For/Next (Exit is good for getting out of functions and subs as well).
 
<langsyntaxhighlight lang="vb">Dim a, b, i
 
Do
Line 3,537 ⟶ 3,867:
b = Int(Rnd * 20)
WScript.Echo vbNullString, b
Next</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Program
Sub Main()
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though).
Line 3,556 ⟶ 3,886:
Loop
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>14 16 15 11 4 11 18 8 19 5 5 9 12 9 19 0 17 19 13 6 16 16 19 0 13 10 18 13 10 </pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import rand
import rand.seed
 
Line 3,575 ⟶ 3,905:
println(b)
}
}</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
 
var r = Random.new()
Line 3,586 ⟶ 3,916:
if (n == 10) break
System.print(r.int(20))
} </langsyntaxhighlight>
 
{{out}}
Line 3,602 ⟶ 3,932:
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<langsyntaxhighlight lang="xbasic">
PROGRAM "loopbreak"
 
Line 3,639 ⟶ 3,969:
 
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,656 ⟶ 3,986:
 
=={{header|XBS}}==
<langsyntaxhighlight lang="xbs">while(true){
set n:number = math.random(0,19);
log(`first: {n}`);
Line 3,662 ⟶ 3,992:
n = math.random(0,19);
log(`second: {n}`);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,679 ⟶ 4,009:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int N;
loop [N:= Ran(20);
Line 3,687 ⟶ 4,017:
IntOut(0, Ran(20));
CrLf(0);
]</langsyntaxhighlight>
 
{{out}}
Line 3,703 ⟶ 4,033:
 
=={{header|Yabasic}}==
<langsyntaxhighlight lang="yabasic">do
i = int(ran(19))
print i using "##";
Line 3,712 ⟶ 4,042:
loop
print
end</langsyntaxhighlight>
 
=={{header|Zig}}==
<langsyntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() !void {
Line 3,731 ⟶ 4,061:
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2});
}
}</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">while(1){n:=(0).random(20); n.print(" ");
if (n==10){ println(); break; } (0).random().println();
}</langsyntaxhighlight>
{{out}}
<pre>
885

edits