Loops/Break: Difference between revisions

Content added Content deleted
(→‎{{header|J}}: some alternatives)
m (syntax highlighting fixup automation)
Line 30: Line 30:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}
<lang 11l>L
<syntaxhighlight lang="11l">L
V a = random:(20)
V a = random:(20)
print(a)
print(a)
Line 36: Line 36:
L.break
L.break
V b = random:(20)
V b = random:(20)
print(b)</lang>
print(b)</syntaxhighlight>


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
<lang 360asm>* Loops Break 15/02/2017
<syntaxhighlight lang="360asm">* Loops Break 15/02/2017
LOOPBREA CSECT
LOOPBREA CSECT
USING LOOPBREA,R13 base register
USING LOOPBREA,R13 base register
Line 77: Line 77:
XDEC DS CL12
XDEC DS CL12
YREGS
YREGS
END LOOPBREA</lang>
END LOOPBREA</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 94: Line 94:
=={{header|6502 Assembly}}==
=={{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.
Code is called as a subroutine (i.e. JSR LoopBreakSub). Specific OS/hardware routines for generating random numbers and printing are left unimplemented.
<lang 6502asm>LoopBreakSub: PHA ;push accumulator onto stack
<syntaxhighlight lang="6502asm">LoopBreakSub: PHA ;push accumulator onto stack




Line 110: Line 110:


Break: PLA ;restore accumulator from stack
Break: PLA ;restore accumulator from stack
RTS ;return from subroutine</lang>
RTS ;return from subroutine</syntaxhighlight>
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopbreak64.s */
/* program loopbreak64.s */
Line 210: Line 210:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{output}}
{{output}}
<pre>
<pre>
Line 227: Line 227:
</pre>
</pre>
=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC Main()
<syntaxhighlight lang="action!">PROC Main()
BYTE v
BYTE v


Line 239: Line 239:
OD
OD
PrintE("After loop")
PrintE("After loop")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer]
Line 257: Line 257:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
with Ada.Numerics.Discrete_Random;


Line 274: Line 274:
Put_Line (Value_Type'Image (B));
Put_Line (Value_Type'Image (B));
end loop;
end loop;
end Test_Loop_Break;</lang>
end Test_Loop_Break;</syntaxhighlight>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>integer
<syntaxhighlight lang="aime">integer
main(void)
main(void)
{
{
Line 296: Line 296:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|ALGOL 60}}==
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
{{works with|ALGOL 60|OS/360}}
<lang algol60>'BEGIN' 'COMMENT' Loops/Break - ALGOL60 - 18/06/2018;
<syntaxhighlight lang="algol60">'BEGIN' 'COMMENT' Loops/Break - ALGOL60 - 18/06/2018;
'INTEGER' SEED;
'INTEGER' SEED;
'INTEGER' 'PROCEDURE' RANDOM(N);
'INTEGER' 'PROCEDURE' RANDOM(N);
Line 324: Line 324:
LAB:
LAB:
SYSACT(1,14,1); 'COMMENT' skip line;
SYSACT(1,14,1); 'COMMENT' skip line;
'END'</lang>
'END'</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 336: Line 336:
{{wont work with|ALGOL 68G|Any - in a68G next random takes no seed argument}}
{{wont work with|ALGOL 68G|Any - in a68G next random takes no seed argument}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}}
<lang algol68>main: (
<syntaxhighlight lang="algol68">main: (
INT a, b;
INT a, b;
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ #
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ #
Line 348: Line 348:
OD;
OD;
print(new line)
print(new line)
)</lang>
)</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:25ex;overflow:scroll">
<pre style="height:25ex;overflow:scroll">
Line 383: Line 383:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang AppleScript>repeat
<syntaxhighlight lang="applescript">repeat
set a to random number from 0 to 19
set a to random number from 0 to 19
if a is 10 then
if a is 10 then
Line 391: Line 391:
set b to random number from 0 to 19
set b to random number from 0 to 19
log a & b
log a & b
end repeat</lang>
end repeat</syntaxhighlight>




Line 439: Line 439:


=={{header|Arc}}==
=={{header|Arc}}==
<lang Arc>(point break
<syntaxhighlight lang="arc">(point break
(while t
(while t
(let x (rand 20)
(let x (rand 20)
Line 445: Line 445:
(if (is x 10)
(if (is x 10)
(break)))
(break)))
(prn "b: " (rand 20))))</lang>
(prn "b: " (rand 20))))</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>


/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
Line 635: Line 635:




</syntaxhighlight>
</lang>


=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>while ø [
<syntaxhighlight lang="rebol">while ø [
a: random 0 19
a: random 0 19
prints [a ""]
prints [a ""]
Line 647: Line 647:
print b
print b
]
]
print ""</lang>
print ""</syntaxhighlight>


{{out}}
{{out}}
Line 664: Line 664:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop
<lang AutoHotkey>Loop
{
{
Random, var, 0, 19
Random, var, 0, 19
Line 673: Line 673:
output = %output%`n%var%
output = %output%`n%var%
}
}
MsgBox % output</lang>
MsgBox % output</syntaxhighlight>


=={{header|Avail}}==
=={{header|Avail}}==
<lang Avail>rng ::= a pRNG;
<syntaxhighlight lang="avail">rng ::= a pRNG;
checked : [0..19];
checked : [0..19];
Do [
Do [
Line 683: Line 683:
] while checked ≠ 10 alternate with [
] while checked ≠ 10 alternate with [
Print: " " ++ “rng's next [0..19]” ++ "\n";
Print: " " ++ “rng's next [0..19]” ++ "\n";
];</lang>
];</syntaxhighlight>


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).
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}}==
=={{header|AWK}}==
<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
for (;;) {
for (;;) {
print n = int(rand() * 20)
print n = int(rand() * 20)
Line 695: Line 695:
print int(rand() * 20)
print int(rand() * 20)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Axe}}==
=={{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.
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.


<lang axe>While 1
<syntaxhighlight lang="axe">While 1
rand^20→A
rand^20→A
Disp A▶Dec
Disp A▶Dec
Line 706: Line 706:
rand^20→B
rand^20→B
Disp B▶Dec,i
Disp B▶Dec,i
End</lang>
End</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<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</lang>
<syntaxhighlight 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</syntaxhighlight>
==={{header|BaCon}}===
==={{header|BaCon}}===
<lang freebasic>
<syntaxhighlight lang="freebasic">
REPEAT
REPEAT
number = RANDOM(20)
number = RANDOM(20)
Line 720: Line 720:
ENDIF
ENDIF
PRINT "second ",RANDOM(20)
PRINT "second ",RANDOM(20)
UNTIL FALSE</lang>
UNTIL FALSE</syntaxhighlight>


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">do
<lang BASIC256>do
i = int(rand * 19)
i = int(rand * 19)
print i; " ";
print i; " ";
Line 731: Line 731:
until false
until false
print
print
end</lang>
end</syntaxhighlight>


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive).
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive).
<lang commodorebasic>10 X = RND(-TI) : REM SEED RN GENERATOR
<syntaxhighlight lang="commodorebasic">10 X = RND(-TI) : REM SEED RN GENERATOR
20 A = INT(RND(1)*20)
20 A = INT(RND(1)*20)
30 PRINT A
30 PRINT A
Line 742: Line 742:
60 PRINT B
60 PRINT B
70 GOTO 20
70 GOTO 20
80 END</lang>
80 END</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 RANDOMIZE
<syntaxhighlight lang="is-basic">100 RANDOMIZE
110 DO
110 DO
120 LET A=RND(20)+1
120 LET A=RND(20)+1
Line 751: Line 751:
140 IF A=10 THEN EXIT DO
140 IF A=10 THEN EXIT DO
150 PRINT RND(20)+1
150 PRINT RND(20)+1
160 LOOP</lang>
160 LOOP</syntaxhighlight>


==={{header|QuickBASIC}}===
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}
<lang qbasic>do
<syntaxhighlight lang="qbasic">do
a = int(rnd * 20)
a = int(rnd * 20)
print a
print a
Line 761: Line 761:
b = int(rnd * 20)
b = int(rnd * 20)
print b
print b
loop</lang>
loop</syntaxhighlight>




==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang basic>RANDOMIZE
<syntaxhighlight lang="basic">RANDOMIZE


DO
DO
Line 777: Line 777:
LOOP
LOOP
PRINT
PRINT
END</lang>
END</syntaxhighlight>




Line 783: Line 783:
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:
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:


<lang zxbasic>10 FOR l = 1 TO 20
<syntaxhighlight lang="zxbasic">10 FOR l = 1 TO 20
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
30 PRINT l
30 PRINT l
40 NEXT l
40 NEXT l
50 STOP</lang>
50 STOP</syntaxhighlight>


The correct solution:
The correct solution:


<lang zxbasic>10 LET a = INT (RND * 20)
<syntaxhighlight lang="zxbasic">10 LET a = INT (RND * 20)
20 PRINT a
20 PRINT a
30 IF a = 10 THEN STOP
30 IF a = 10 THEN STOP
40 PRINT INT (RND * 20)
40 PRINT INT (RND * 20)
50 GO TO 10</lang>
50 GO TO 10</syntaxhighlight>


=={{header|Batch File}}==
=={{header|Batch File}}==
<lang dos>@echo off
<syntaxhighlight lang="dos">@echo off
:loop
:loop
set /a N=%RANDOM% %% 20
set /a N=%RANDOM% %% 20
Line 805: Line 805:
set /a N=%RANDOM% %% 20
set /a N=%RANDOM% %% 20
echo %N%
echo %N%
goto loop</lang>
goto loop</syntaxhighlight>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> REPEAT
<syntaxhighlight lang="bbcbasic"> REPEAT
num% = RND(20)-1
num% = RND(20)-1
PRINT num%
PRINT num%
IF num%=10 THEN EXIT REPEAT
IF num%=10 THEN EXIT REPEAT
PRINT RND(20)-1
PRINT RND(20)-1
UNTIL FALSE</lang>
UNTIL FALSE</syntaxhighlight>


=={{header|bc}}==
=={{header|bc}}==
<lang bc>s = 1 /* seed of the random number generator */
<syntaxhighlight lang="bc">s = 1 /* seed of the random number generator */
scale = 0
scale = 0


Line 839: Line 839:
r() /* print 2nd number */
r() /* print 2nd number */
}
}
quit</lang>
quit</syntaxhighlight>


=={{header|Befunge}}==
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">
<lang Befunge>
>60v *2\<
>60v *2\<
>?>\1-:|
>?>\1-:|
Line 854: Line 854:
>^ 7
>^ 7
^ .%++67<
^ .%++67<
</syntaxhighlight>
</lang>


=={{header|C}}==
=={{header|C}}==


<syntaxhighlight lang="c">
<lang c>
int main(){
int main(){
time_t t;
time_t t;
Line 872: Line 872:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>
Output (example):
Output (example):
<pre>
<pre>
Line 888: Line 888:


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>class Program
<syntaxhighlight lang="csharp">class Program
{
{
static void Main(string[] args)
static void Main(string[] args)
Line 905: Line 905:
Console.ReadLine();
Console.ReadLine();
}
}
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <ctime>
#include <ctime>
#include <cstdlib>
#include <cstdlib>
Line 923: Line 923:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


=={{header|Chapel}}==
=={{header|Chapel}}==
<lang chapel>use Random;
<syntaxhighlight lang="chapel">use Random;


var r = new RandomStream();
var r = new RandomStream();
Line 936: Line 936:
writeln(b);
writeln(b);
}
}
delete r;</lang>
delete r;</syntaxhighlight>


=={{header|Chef}}==
=={{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.
"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'>
<div style='width:full;overflow:scroll'>
<lang Chef>Healthy Vita-Sauce Loop - Broken.
<syntaxhighlight lang="chef">Healthy Vita-Sauce Loop - Broken.


Makes a whole lot of sauce for two people.
Makes a whole lot of sauce for two people.
Line 998: Line 998:
Pour contents of the 1st mixing bowl into the 1st baking dish.
Pour contents of the 1st mixing bowl into the 1st baking dish.


Serves 2.</lang>
Serves 2.</syntaxhighlight>
</div>
</div>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(loop [[a b & more] (repeatedly #(rand-int 20))]
<syntaxhighlight lang="lisp">(loop [[a b & more] (repeatedly #(rand-int 20))]
(println a)
(println a)
(when-not (= 10 a)
(when-not (= 10 a)
(println b)
(println b)
(recur more)))</lang>
(recur more)))</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
{{works with|OpenCOBOL}}
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Random-Nums.
PROGRAM-ID. Random-Nums.


Line 1,035: Line 1,035:
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20)
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20)
DISPLAY Num
DISPLAY Num
.</lang>
.</syntaxhighlight>


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this:
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this:
<lang coffeescript>
<syntaxhighlight lang="coffeescript">
loop
loop
print a = Math.random() * 20 // 1
print a = Math.random() * 20 // 1
break if a == 10
break if a == 10
print Math.random() * 20 // 1
print Math.random() * 20 // 1
</syntaxhighlight>
</lang>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
<lang cfm>
<syntaxhighlight lang="cfm">
<Cfset randNum = 0>
<Cfset randNum = 0>
<cfloop condition="randNum neq 10">
<cfloop condition="randNum neq 10">
Line 1,056: Line 1,056:
<Br>
<Br>
</cfloop>
</cfloop>
</syntaxhighlight>
</lang>
{{out}}
{{out}}
My first two test outputs (I swear this is true)
My first two test outputs (I swear this is true)
Line 1,124: Line 1,124:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(loop for a = (random 20)
<syntaxhighlight lang="lisp">(loop for a = (random 20)
do (print a)
do (print a)
until (= a 10)
until (= a 10)
do (print (random 20)))</lang>
do (print (random 20)))</syntaxhighlight>


=== Using DO ===
=== Using DO ===
<lang lisp>
<syntaxhighlight lang="lisp">
(do ((a (random 20) (random 20))) ; Initialize to rand and set new rand on every loop
(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
((= 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'
(format t "~a~3T~a~%" a (random 20))) ; On every loop print formated `a' and rand `b'
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,159: Line 1,159:


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio, std.random;
<syntaxhighlight lang="d">import std.stdio, std.random;


void main() {
void main() {
Line 1,169: Line 1,169:
write(uniform(0, 20), " ");
write(uniform(0, 20), " ");
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre>
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre>
Line 1,175: Line 1,175:
=={{header|dc}}==
=={{header|dc}}==
{{trans|bc}}
{{trans|bc}}
<lang dc>1 ss [s = seed of the random number generator]sz
<syntaxhighlight lang="dc">1 ss [s = seed of the random number generator]sz
0k [scale = 0]sz
0k [scale = 0]sz


Line 1,199: Line 1,199:
0 0 =r p sz [Print 2nd number.]sz
0 0 =r p sz [Print 2nd number.]sz
0 0 =B [Continue loop.]sz
0 0 =B [Continue loop.]sz
]sB 0 0 =B</lang>
]sB 0 0 =B</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==


<lang Delphi>program Project5;
<syntaxhighlight lang="delphi">program Project5;


{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
Line 1,219: Line 1,219:
end.
end.


</syntaxhighlight>
</lang>


=={{header|DWScript}}==
=={{header|DWScript}}==


<lang delphi>
<syntaxhighlight lang="delphi">
while True do begin
while True do begin
var num := RandomInt(20);
var num := RandomInt(20);
PrintLn(num);
PrintLn(num);
if num=10 then Break;
if num=10 then Break;
end;</lang>
end;</syntaxhighlight>


=={{header|E}}==
=={{header|E}}==
<lang e>while (true) {
<syntaxhighlight lang="e">while (true) {
def a := entropy.nextInt(20)
def a := entropy.nextInt(20)
print(a)
print(a)
Line 1,239: Line 1,239:
}
}
println(" ", entropy.nextInt(20))
println(" ", entropy.nextInt(20))
}</lang>
}</syntaxhighlight>


=={{header|EasyLang}}==
=={{header|EasyLang}}==
<lang>repeat
<syntaxhighlight lang="text">repeat
a = random 20
a = random 20
print a
print a
until a = 10
until a = 10
print random 20
print random 20
.</lang>
.</syntaxhighlight>


=={{header|Eiffel}}==
=={{header|Eiffel}}==
<lang eiffel>
<syntaxhighlight lang="eiffel">
example
example
-- Eiffel example code
-- Eiffel example code
Line 1,266: Line 1,266:
end
end
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
The output is superfluous and unneeded to read and understand what the Eiffel code is doing.
The output is superfluous and unneeded to read and understand what the Eiffel code is doing.
Line 1,278: Line 1,278:
As a result, an output looks almost truly random:
As a result, an output looks almost truly random:


<lang ela>open datetime random monad io
<syntaxhighlight lang="ela">open datetime random monad io
loop = loop' 1
loop = loop' 1
Line 1,289: Line 1,289:




loop 10 ::: IO</lang>
loop 10 ::: IO</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
{{works with|Elixir|1.2}}
<lang elixir>defmodule Loops do
<syntaxhighlight lang="elixir">defmodule Loops do
def break, do: break(random)
def break, do: break(random)
Line 1,305: Line 1,305:
end
end


Loops.break</lang>
Loops.break</syntaxhighlight>


{{out}}
{{out}}
Line 1,327: Line 1,327:


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<lang lisp>(defun wait_10 ()
<syntaxhighlight lang="lisp">(defun wait_10 ()
(catch 'loop-break
(catch 'loop-break
(while 't
(while 't
Line 1,336: Line 1,336:
(message "current number is: %d" math) ) ) ) ) )
(message "current number is: %d" math) ) ) ) ) )


(wait_10)</lang>
(wait_10)</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>%% Implemented by Arjun Sunel
<syntaxhighlight lang="erlang">%% Implemented by Arjun Sunel
-module(forever).
-module(forever).
-export([main/0, for/0]).
-export([main/0, for/0]).
Line 1,356: Line 1,356:
for()
for()
end.
end.
</syntaxhighlight>
</lang>


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
LOOP
LOOP
A=INT(RND(1)*20)
A=INT(RND(1)*20)
Line 1,366: Line 1,366:
PRINT(INT(RND(1)*20))
PRINT(INT(RND(1)*20))
END LOOP
END LOOP
</syntaxhighlight>
</lang>
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument.
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument.


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang euphoria>integer i
<syntaxhighlight lang="euphoria">integer i
while 1 do
while 1 do
i = rand(20) - 1
i = rand(20) - 1
Line 1,378: Line 1,378:
end if
end if
printf(1, "%g ", {rand(20)-1})
printf(1, "%g ", {rand(20)-1})
end while</lang>
end while</syntaxhighlight>
The <code>rand()</code> function returns a random integer from 1 to the integer provided.
The <code>rand()</code> function returns a random integer from 1 to the integer provided.


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Loops/Break. Nigel Galloway: February 21st., 2022
// Loops/Break. Nigel Galloway: February 21st., 2022
let n=System.Random()
let n=System.Random()
let rec fN g=printf "%d " g; if g <> 10 then fN(n.Next(20))
let rec fN g=printf "%d " g; if g <> 10 then fN(n.Next(20))
fN(n.Next(20))
fN(n.Next(20))
</syntaxhighlight>
</lang>


=={{header|Factor}}==
=={{header|Factor}}==
Using <code>with-return</code>:
Using <code>with-return</code>:
<lang factor>[
<syntaxhighlight lang="factor">[
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop
] with-return</lang>
] with-return</syntaxhighlight>


Idiomatic Factor:
Idiomatic Factor:
<lang factor>[ 20 random [ . ] [ 10 = not ] bi dup [ 20 random . ] when ] loop</lang>
<syntaxhighlight lang="factor">[ 20 random [ . ] [ 10 = not ] bi dup [ 20 random . ] when ] loop</syntaxhighlight>


=={{header|Fantom}}==
=={{header|Fantom}}==


<lang fantom>
<syntaxhighlight lang="fantom">
class ForBreak
class ForBreak
{
{
Line 1,414: Line 1,414:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>include random.fs
<syntaxhighlight lang="forth">include random.fs


: main
: main
Line 1,430: Line 1,430:
10 = if leave then
10 = if leave then
i random .
i random .
loop ;</lang>
loop ;</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran>program Example
<syntaxhighlight lang="fortran">program Example
implicit none
implicit none


Line 1,450: Line 1,450:
end do
end do


end program Example</lang>
end program Example</syntaxhighlight>


{{works with|Fortran|77 and later}}
{{works with|Fortran|77 and later}}
<lang fortran> PROGRAM LOOPBREAK
<syntaxhighlight lang="fortran"> PROGRAM LOOPBREAK
INTEGER I, RNDINT
INTEGER I, RNDINT


Line 1,521: Line 1,521:
ENDIF
ENDIF
RETURN
RETURN
END</lang>
END</syntaxhighlight>


{{works with|Fortran|66 and earlier}}
{{works with|Fortran|66 and earlier}}
Line 1,529: Line 1,529:


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!
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)
SUBROUTINE RANDU(IX,IY,YFL)
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60.
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60.
Line 1,561: Line 1,561:
20 CONTINUE
20 CONTINUE
END
END
</syntaxhighlight>
</lang>
Output, converted to along the line:
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
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,567: Line 1,567:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64


Dim i As Integer
Dim i As Integer
Line 1,582: Line 1,582:


Print
Print
Sleep</lang>
Sleep</syntaxhighlight>


Sample output
Sample output
Line 1,593: Line 1,593:


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>while true
<syntaxhighlight lang="frink">while true
{
{
a = random[0,19]
a = random[0,19]
Line 1,602: Line 1,602:
b = random[0,19]
b = random[0,19]
print["$b "]
print["$b "]
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,609: Line 1,609:


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<lang futurebasic>include "NSLog.incl"
<syntaxhighlight lang="futurebasic">include "NSLog.incl"


long num
long num
Line 1,617: Line 1,617:
until ( num == 10 )
until ( num == 10 )


HandleEvents</lang>
HandleEvents</syntaxhighlight>


=={{header|Gambas}}==
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim iRand As Integer
Dim iRand As Integer


Line 1,628: Line 1,628:
Until iRand = 10
Until iRand = 10


End</lang> =={{header|Gambas}}==
End</syntaxhighlight> =={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]'''
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]'''
<lang gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()
Dim byNo As Byte
Dim byNo As Byte


Line 1,641: Line 1,641:
Loop
Loop


End</lang>
End</syntaxhighlight>
Output:
Output:
<pre>
<pre>
Line 1,648: Line 1,648:


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap>while true do
<syntaxhighlight lang="gap">while true do
a := Random(0, 19);
a := Random(0, 19);
Print(a);
Print(a);
Line 1,664: Line 1,664:
# 5 10
# 5 10
# 1 16
# 1 16
# 10</lang>
# 10</syntaxhighlight>


=={{header|GML}}==
=={{header|GML}}==
<lang GML>while(1)
<syntaxhighlight lang="gml">while(1)
{
{
a = floor(random(19))
a = floor(random(19))
Line 1,676: Line 1,676:
show_message(string(a))
show_message(string(a))
}
}
</syntaxhighlight>
</lang>


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,696: Line 1,696:
fmt.Println(b)
fmt.Println(b)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
<lang groovy>final random = new Random()
<syntaxhighlight lang="groovy">final random = new Random()


while (true) {
while (true) {
Line 1,707: Line 1,707:
print ' '
print ' '
println random.nextInt(20)
println random.nextInt(20)
}</lang>
}</syntaxhighlight>


=={{header|GW-BASIC}}==
=={{header|GW-BASIC}}==
<lang qbasic>10 NUM = 0
<syntaxhighlight lang="qbasic">10 NUM = 0
20 WHILE NUM <> 10
20 WHILE NUM <> 10
30 NUM = INT(RND * 20)
30 NUM = INT(RND * 20)
40 PRINT NUM
40 PRINT NUM
50 WEND</lang>
50 WEND</syntaxhighlight>


=={{header|Harbour}}==
=={{header|Harbour}}==
<lang visualfoxpro>PROCEDURE Loop()
<syntaxhighlight lang="visualfoxpro">PROCEDURE Loop()


LOCAL n
LOCAL n
Line 1,729: Line 1,729:
ENDDO
ENDDO


RETURN</lang>
RETURN</syntaxhighlight>


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad
<syntaxhighlight lang="haskell">import Control.Monad
import System.Random
import System.Random


Line 1,740: Line 1,740:
unless (r==k) $ do
unless (r==k) $ do
print =<< randomRIO (0,n)
print =<< randomRIO (0,n)
loopBreak n k</lang>
loopBreak n k</syntaxhighlight>
Use:
Use:
<lang haskell>loopBreak 19 10</lang>
<syntaxhighlight lang="haskell">loopBreak 19 10</syntaxhighlight>


=={{header|Haxe}}==
=={{header|Haxe}}==
<lang haxe>class Program {
<syntaxhighlight lang="haxe">class Program {
static public function main():Void {
static public function main():Void {
while(true) {
while(true) {
Line 1,756: Line 1,756:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|hexiscript}}==
=={{header|hexiscript}}==
<lang hexiscript>while true
<syntaxhighlight lang="hexiscript">while true
let r rand 20
let r rand 20
println r
println r
Line 1,766: Line 1,766:
endif
endif
println rand 20
println rand 20
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|HicEst}}==
=={{header|HicEst}}==
<lang hicest>1 DO i = 1, 1E20 ! "forever"
<syntaxhighlight lang="hicest">1 DO i = 1, 1E20 ! "forever"
a = INT( RAN(10, 10) )
a = INT( RAN(10, 10) )
WRITE(name) a
WRITE(name) a
Line 1,777: Line 1,777:
ENDDO
ENDDO
10
10
END</lang>
END</syntaxhighlight>


=={{header|HolyC}}==
=={{header|HolyC}}==


<lang holyc>U16 a, b;
<syntaxhighlight lang="holyc">U16 a, b;
while (1) {
while (1) {
a = RandU16 % 20;
a = RandU16 % 20;
Line 1,791: Line 1,791:
Print("%d\n", b);
Print("%d\n", b);
}
}
</syntaxhighlight>
</lang>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang Icon>procedure main()
<syntaxhighlight lang="icon">procedure main()
while 10 ~= writes(?20-1) do write(", ",?20-1)
while 10 ~= writes(?20-1) do write(", ",?20-1)
end</lang>
end</syntaxhighlight>
Notes:
Notes:
* For any positive integer i, ?i produces a value j where 1 <= j <= i
* 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).
* 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.
* 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 <lang Icon>&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))</lang> at the start of the <tt>main</tt> procedure to set the random number seed based on the time of day.
* 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|Io}}==
=={{header|Io}}==
<lang io>loop(
<syntaxhighlight lang="io">loop(
a := Random value(0,20) floor
a := Random value(0,20) floor
write(a)
write(a)
Line 1,810: Line 1,810:
b := Random value(0,20) floor
b := Random value(0,20) floor
writeln(" ",b)
writeln(" ",b)
)</lang>
)</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
<lang j>loopexample=: {{
<syntaxhighlight lang="j">loopexample=: {{
while. do.
while. do.
echo k=. ?20
echo k=. ?20
Line 1,819: Line 1,819:
echo ?20
echo ?20
end.
end.
}}</lang>
}}</syntaxhighlight>


Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>:
Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>:


<lang J>loopexample2=: verb define
<syntaxhighlight lang="j">loopexample2=: verb define
while. do.
while. do.
echo k=. ?20
echo k=. ?20
Line 1,829: Line 1,829:
echo ?20
echo ?20
end.
end.
)</lang>
)</syntaxhighlight>


<lang J>loopexample3=: {{
<syntaxhighlight lang="j">loopexample3=: {{
while. do.
while. do.
echo k=. ?20
echo k=. ?20
Line 1,838: Line 1,838:
end.
end.
label_done.
label_done.
}}</lang>
}}</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<lang java>import java.util.Random;
<syntaxhighlight lang="java">import java.util.Random;


Random rand = new Random();
Random rand = new Random();
Line 1,850: Line 1,850:
int b = rand.nextInt(20);
int b = rand.nextInt(20);
System.out.println(b);
System.out.println(b);
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>for (;;) {
<syntaxhighlight lang="javascript">for (;;) {
var a = Math.floor(Math.random() * 20);
var a = Math.floor(Math.random() * 20);
print(a);
print(a);
Line 1,860: Line 1,860:
a = Math.floor(Math.random() * 20);
a = Math.floor(Math.random() * 20);
print(a);
print(a);
}</lang>
}</syntaxhighlight>
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell.
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell.


Line 1,868: Line 1,868:
In a functional idiom of JavaScript, we might instead write something like:
In a functional idiom of JavaScript, we might instead write something like:


<lang JavaScript>(function streamTillInitialTen() {
<syntaxhighlight lang="javascript">(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
var nFirst = Math.floor(Math.random() * 20);
Line 1,880: Line 1,880:
return streamTillInitialTen();
return streamTillInitialTen();
})();</lang>
})();</syntaxhighlight>


Obtaining runs like:
Obtaining runs like:
Line 1,921: Line 1,921:
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread:
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread:


<lang JavaScript>console.log(
<syntaxhighlight lang="javascript">console.log(
(function streamTillInitialTen() {
(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
var nFirst = Math.floor(Math.random() * 20);
Line 1,934: Line 1,934:
);
);
})().join('\n')
})().join('\n')
);</lang>
);</syntaxhighlight>


Sample result:
Sample result:
Line 1,962: Line 1,962:


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 -
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 -
<lang jq># 15-bit integers generated using the same formula as rand()
<syntaxhighlight lang="jq"># 15-bit integers generated using the same formula as rand()
# from the Microsoft C Runtime.
# from the Microsoft C Runtime.
# Input: [ count, state, rand ]
# Input: [ count, state, rand ]
Line 1,977: Line 1,977:


# Generate random integers from 0 to (n-1):
# Generate random integers from 0 to (n-1):
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</lang>
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</syntaxhighlight>


'''"take"'''
'''"take"'''


<lang jq>def take(s; cond):
<syntaxhighlight lang="jq">def take(s; cond):
label $done
label $done
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</lang>
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</syntaxhighlight>


'''"count"'''
'''"count"'''


Since the PRNG used here is deterministic, we'll just count the number of integers generated:
Since the PRNG used here is deterministic, we'll just count the number of integers generated:
<lang jq>def count(s): reduce s as $i (0; . + 1);</lang>
<syntaxhighlight lang="jq">def count(s): reduce s as $i (0; . + 1);</syntaxhighlight>


'''Example'''
'''Example'''
Line 1,996: Line 1,996:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
while true
while true
n = rand(0:19)
n = rand(0:19)
Line 2,007: Line 2,007:
@printf "%4d\n" n
@printf "%4d\n" n
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,027: Line 2,027:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>import java.util.Random
<syntaxhighlight lang="scala">import java.util.Random


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 2,037: Line 2,037:
println(rand.nextInt(20))
println(rand.nextInt(20))
}
}
}</lang>
}</syntaxhighlight>


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
Line 2,050: Line 2,050:
{loops_break 0}
{loops_break 0}
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop
</syntaxhighlight>
</lang>


=={{header|Lang5}}==
=={{header|Lang5}}==
<lang lang5>do 20 ? int dup . 10 == if break then 20 ? int . loop</lang>
<syntaxhighlight lang="lang5">do 20 ? int dup . 10 == if break then 20 ? int . loop</syntaxhighlight>


=={{header|langur}}==
=={{header|langur}}==
<lang langur>for {
<syntaxhighlight lang="langur">for {
val .i = random 0..19
val .i = random 0..19
write .i, " "
write .i, " "
if .i == 10 { writeln(); break }
if .i == 10 { writeln(); break }
write random(0..19), " "
write random(0..19), " "
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,067: Line 2,067:


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>local(x = 0)
<syntaxhighlight lang="lasso">local(x = 0)
while(#x != 10) => {^
while(#x != 10) => {^
#x = integer_random(19,0)
#x = integer_random(19,0)
Line 2,073: Line 2,073:
#x == 10 ? loop_abort
#x == 10 ? loop_abort
', '+integer_random(19,0)+'\r'
', '+integer_random(19,0)+'\r'
^}</lang>
^}</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
The task specifies a "number".
The task specifies a "number".
<lang lb>while num<>10
<syntaxhighlight lang="lb">while num<>10
num=rnd(1)*20
num=rnd(1)*20
print num
print num
Line 2,083: Line 2,083:
print rnd(1)*20
print rnd(1)*20
wend
wend
</lang>If "integer" was meant, this code fulfils that requirement.
</syntaxhighlight>If "integer" was meant, this code fulfils that requirement.
<lang lb>while num<>10
<syntaxhighlight lang="lb">while num<>10
num=int(rnd(1)*20)
num=int(rnd(1)*20)
print num
print num
Line 2,090: Line 2,090:
print int(rnd(1)*20)
print int(rnd(1)*20)
wend
wend
</syntaxhighlight>
</lang>


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>repeat while TRUE
<syntaxhighlight lang="lingo">repeat while TRUE
n = random(20)-1
n = random(20)-1
put n
put n
if n = 10 then exit repeat
if n = 10 then exit repeat
put random(20)-1
put random(20)-1
end repeat</lang>
end repeat</syntaxhighlight>


=={{header|Lisaac}}==
=={{header|Lisaac}}==
<lang Lisaac>Section Header
<syntaxhighlight lang="lisaac">Section Header


+ name := TEST_LOOP_BREAK;
+ name := TEST_LOOP_BREAK;
Line 2,121: Line 2,121:
'\n'.print;
'\n'.print;
}
}
);</lang>
);</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>command loopForeverRandom
<syntaxhighlight lang="livecode">command loopForeverRandom
repeat forever
repeat forever
put random(20) - 1 into tRand
put random(20) - 1 into tRand
Line 2,132: Line 2,132:
end repeat
end repeat
end loopForeverRandom
end loopForeverRandom
</syntaxhighlight>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>repeat
<syntaxhighlight lang="lua">repeat
k = math.random(19)
k = math.random(19)
print(k)
print(k)
if k == 10 then break end
if k == 10 then break end
print(math.random(19)
print(math.random(19)
until false</lang>
until false</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{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.
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 {
Module Checkit {
M=Random(0, 19)
M=Random(0, 19)
Line 2,171: Line 2,171:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>


=={{header|M4}}==
=={{header|M4}}==
<lang M4>define(`randSeed',141592653)dnl
<syntaxhighlight lang="m4">define(`randSeed',141592653)dnl
define(`setRand',
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
Line 2,185: Line 2,185:
loopbreak')')dnl
loopbreak')')dnl
dnl
dnl
loopbreak</lang>
loopbreak</syntaxhighlight>


{{out}}
{{out}}
Line 2,197: Line 2,197:


=={{header|Maple}}==
=={{header|Maple}}==
<lang Maple>r := rand( 0 .. 19 ):
<syntaxhighlight lang="maple">r := rand( 0 .. 19 ):
do
do
n := r();
n := r();
Line 2,205: Line 2,205:
end if;
end if;
printf( "%d\n", r() );
printf( "%d\n", r() );
end do:</lang>
end do:</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>While[(Print[#];#!=10)&[RandomIntger[{0,19}]],
<syntaxhighlight lang="mathematica">While[(Print[#];#!=10)&[RandomIntger[{0,19}]],
Print[RandomInteger[{0,19}]
Print[RandomInteger[{0,19}]
]</lang>
]</syntaxhighlight>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>/* To exit the innermost block, use return(<value>) */
<syntaxhighlight lang="maxima">/* To exit the innermost block, use return(<value>) */


block([n],
block([n],
Line 2,248: Line 2,248:
> x: 2;
> x: 2;
> exit;
> exit;
2</lang>
2</syntaxhighlight>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
<lang MAXScript>
while true do
while true do
(
(
Line 2,260: Line 2,260:
format ("B: % \n") b
format ("B: % \n") b
)
)
</syntaxhighlight>
</lang>


=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.6}}
{{works with|min|0.19.6}}
<lang min>randomize
<syntaxhighlight lang="min">randomize
(19 random puts 10 ==) (19 random puts!) () () linrec</lang>
(19 random puts 10 ==) (19 random puts!) () () linrec</syntaxhighlight>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
<lang>СЧ 2 0 * П0
<syntaxhighlight lang="text">СЧ 2 0 * П0
1 0 - [x] x#0 18
1 0 - [x] x#0 18
СЧ 2 0 * П1
СЧ 2 0 * П1
БП 00 ИП0 С/П</lang>
БП 00 ИП0 С/П</syntaxhighlight>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
<lang modula3>MODULE Break EXPORTS Main;
<syntaxhighlight lang="modula3">MODULE Break EXPORTS Main;


IMPORT IO, Fmt, Random;
IMPORT IO, Fmt, Random;
Line 2,290: Line 2,290:
END;
END;
END;
END;
END Break.</lang>
END Break.</syntaxhighlight>


=={{header|MOO}}==
=={{header|MOO}}==
<lang moo>while (1)
<syntaxhighlight lang="moo">while (1)
a = random(20) - 1;
a = random(20) - 1;
player:tell(a);
player:tell(a);
Line 2,301: Line 2,301:
b = random(20) - 1;
b = random(20) - 1;
player:tell(b);
player:tell(b);
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|MUMPS}}==
=={{header|MUMPS}}==
<lang MUMPS>BREAKLOOP
<syntaxhighlight lang="mumps">BREAKLOOP
NEW A,B
NEW A,B
SET A=""
SET A=""
Line 2,318: Line 2,318:
NEW A,B
NEW A,B
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B
KILL A,B QUIT</lang>
KILL A,B QUIT</syntaxhighlight>
{{out}}
{{out}}
<pre>USER>D BREAKLOOP^ROSETTA
<pre>USER>D BREAKLOOP^ROSETTA
Line 2,343: Line 2,343:


=={{header|Neko}}==
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Loops/Break in Neko
Loops/Break in Neko
Tectonics:
Tectonics:
Line 2,364: Line 2,364:
$print(r, " ");
$print(r, " ");
}
}
$print("\n");</lang>
$print("\n");</syntaxhighlight>


{{out}}
{{out}}
Line 2,373: Line 2,373:
=={{header|Nemerle}}==
=={{header|Nemerle}}==
{{trans|C#}}
{{trans|C#}}
<lang Nemerle>using System;
<syntaxhighlight lang="nemerle">using System;
using System.Console;
using System.Console;
using Nemerle.Imperative;
using Nemerle.Imperative;
Line 2,391: Line 2,391:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
options replace format comments java crossref savelog symbols nobinary


Line 2,410: Line 2,410:
end lb
end lb
say
say
</syntaxhighlight>
</lang>


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>(until (= 10 (println (rand 20)))
<syntaxhighlight lang="newlisp">(until (= 10 (println (rand 20)))
(println (rand 20)))</lang>
(println (rand 20)))</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import random
<syntaxhighlight lang="nim">import random


while true:
while true:
Line 2,426: Line 2,426:
break
break
let b = random(19)
let b = random(19)
echo b</lang>
echo b</syntaxhighlight>


=={{header|NS-HUBASIC}}==
=={{header|NS-HUBASIC}}==
<lang NS-HUBASIC>10 I=RND(20)
<syntaxhighlight lang="ns-hubasic">10 I=RND(20)
20 PRINT I
20 PRINT I
30 IF I=10 THEN STOP
30 IF I=10 THEN STOP
40 PRINT RND(20)
40 PRINT RND(20)
50 GOTO 10</lang>
50 GOTO 10</syntaxhighlight>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
Works with oo2c Version 2
Works with oo2c Version 2
<lang oberon2>
<syntaxhighlight lang="oberon2">
MODULE LoopBreak;
MODULE LoopBreak;
IMPORT
IMPORT
Line 2,459: Line 2,459:
Do
Do
END LoopBreak.
END LoopBreak.
</syntaxhighlight>
</lang>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<syntaxhighlight lang="objeck">
while(true) {
while(true) {
a := (Float->Random() * 20.0)->As(Int);
a := (Float->Random() * 20.0)->As(Int);
Line 2,472: Line 2,472:
a->PrintLine();
a->PrintLine();
}
}
</syntaxhighlight>
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml># Random.self_init();;
<syntaxhighlight lang="ocaml"># Random.self_init();;
- : unit = ()
- : unit = ()


Line 2,492: Line 2,492:
13
13
10
10
Exception: Pervasives.Exit.</lang>
Exception: Pervasives.Exit.</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>while(1)
<syntaxhighlight lang="octave">while(1)
a = floor(unifrnd(0,20, 1));
a = floor(unifrnd(0,20, 1));
disp(a)
disp(a)
Line 2,503: Line 2,503:
b = floor(unifrnd(0,20, 1));
b = floor(unifrnd(0,20, 1));
disp(b)
disp(b)
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==
<lang Oforth>while(true) [
<syntaxhighlight lang="oforth">while(true) [
19 rand dup print ":" print
19 rand dup print ":" print
10 == ifTrue: [ break ]
10 == ifTrue: [ break ]
19 rand print " " print
19 rand print " " print
]</lang>
]</syntaxhighlight>


=={{header|Ol}}==
=={{header|Ol}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(import (otus random!))
(import (otus random!))


Line 2,522: Line 2,522:
(print (rand! 20))
(print (rand! 20))
(loop))))
(loop))))
</syntaxhighlight>
</lang>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang ooRexx>/*REXX ****************************************************************
<syntaxhighlight lang="oorexx">/*REXX ****************************************************************
* Three Ways to leave a Loop
* Three Ways to leave a Loop
* ooRexx added the possibility to leave an outer loop
* ooRexx added the possibility to leave an outer loop
Line 2,569: Line 2,569:
End
End
Else
Else
Say 'Leave label-name is probably not supported in' v</lang>
Say 'Leave label-name is probably not supported in' v</syntaxhighlight>
{{out}}
{{out}}
<pre>i1=1
<pre>i1=1
Line 2,596: Line 2,596:
=={{header|Oz}}==
=={{header|Oz}}==
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic.
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic.
<lang oz>for break:Break do
<syntaxhighlight lang="oz">for break:Break do
R = {OS.rand} mod 20
R = {OS.rand} mod 20
in
in
Line 2,603: Line 2,603:
else {Show {OS.rand} mod 20}
else {Show {OS.rand} mod 20}
end
end
end</lang>
end</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>while(1,
<syntaxhighlight lang="parigp">while(1,
t=random(20);
t=random(20);
print(t);
print(t);
if(t==10, break);
if(t==10, break);
print(random(20))
print(random(20))
)</lang>
)</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Line 2,617: Line 2,617:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>while (1) {
<syntaxhighlight lang="perl">while (1) {
my $a = int(rand(20));
my $a = int(rand(20));
print "$a\n";
print "$a\n";
Line 2,625: Line 2,625:
my $b = int(rand(20));
my $b = int(rand(20));
print "$b\n";
print "$b\n";
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
Line 2,632: Line 2,632:
The rand() function returns a random integer from 1 to the integer provided.
The rand() function returns a random integer from 1 to the integer provided.


<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span>
<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>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
Line 2,640: Line 2,640:
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


{{out}}
{{out}}
Line 2,651: Line 2,651:


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>while (true) {
<syntaxhighlight lang="php">while (true) {
$a = rand(0,19);
$a = rand(0,19);
echo "$a\n";
echo "$a\n";
Line 2,658: Line 2,658:
$b = rand(0,19);
$b = rand(0,19);
echo "$b\n";
echo "$b\n";
}</lang>
}</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Literally:
Literally:
<lang PicoLisp>(use R
<syntaxhighlight lang="picolisp">(use R
(loop
(loop
(println (setq R (rand 1 19)))
(println (setq R (rand 1 19)))
(T (= 10 R))
(T (= 10 R))
(println (rand 1 19)) ) )</lang>
(println (rand 1 19)) ) )</syntaxhighlight>
Shorter:
Shorter:
<lang PicoLisp>(until (= 10 (println (rand 1 19)))
<syntaxhighlight lang="picolisp">(until (= 10 (println (rand 1 19)))
(println (rand 1 19)) )</lang>
(println (rand 1 19)) )</syntaxhighlight>


=={{header|Pike}}==
=={{header|Pike}}==
<lang pike>int main(){
<syntaxhighlight lang="pike">int main(){
while(1){
while(1){
int a = random(20);
int a = random(20);
Line 2,682: Line 2,682:
write(b + "\n");
write(b + "\n");
}
}
}</lang>
}</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
do forever;
do forever;
k = trunc(random()*20);
k = trunc(random()*20);
Line 2,693: Line 2,693:
put skip list (k);
put skip list (k);
end;
end;
</syntaxhighlight>
</lang>


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Demonstrate breaking.
Demonstrate breaking.
Line 2,712: Line 2,712:
To write a number to the console:
To write a number to the console:
Convert the number to a string.
Convert the number to a string.
Write the string to the console.</lang>
Write the string to the console.</syntaxhighlight>


=={{header|PostScript}}==
=={{header|PostScript}}==
<lang postscript>realtime srand % init RNG
<syntaxhighlight lang="postscript">realtime srand % init RNG
{
{
rand 20 mod % generate number between 0 and 19
rand 20 mod % generate number between 0 and 19
dup = % print it
dup = % print it
10 eq { exit } if % exit if 10
10 eq { exit } if % exit if 10
} loop</lang>
} loop</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>$r = New-Object Random
<syntaxhighlight lang="powershell">$r = New-Object Random
for () {
for () {
$n = $r.Next(20)
$n = $r.Next(20)
Line 2,731: Line 2,731:
}
}
Write-Host $r.Next(20)
Write-Host $r.Next(20)
}</lang>
}</syntaxhighlight>


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>If OpenConsole()
<syntaxhighlight lang="purebasic">If OpenConsole()


Repeat
Repeat
Line 2,750: Line 2,750:
Input()
Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==
<lang python>from random import randrange
<syntaxhighlight lang="python">from random import randrange


while True:
while True:
Line 2,761: Line 2,761:
break
break
b = randrange(20)
b = randrange(20)
print(b)</lang>
print(b)</syntaxhighlight>


=={{header|QB64}}==
=={{header|QB64}}==
''CBTJD'': 2020/03/14
''CBTJD'': 2020/03/14
<lang qbasic>RANDOMIZE TIMER
<syntaxhighlight lang="qbasic">RANDOMIZE TIMER
DO
DO
n = INT(RND * 20)
n = INT(RND * 20)
Line 2,775: Line 2,775:
PRINT n
PRINT n
END IF
END IF
LOOP UNTIL 0</lang>
LOOP UNTIL 0</syntaxhighlight>


=={{header|Qi}}==
=={{header|Qi}}==
<syntaxhighlight lang="qi">
<lang qi>
(define loop -> (if (= 10 (PRINT (random 20)))
(define loop -> (if (= 10 (PRINT (random 20)))
true
true
Line 2,784: Line 2,784:
(loop))))
(loop))))
(loop)
(loop)
</syntaxhighlight>
</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery>[ 20 random
<syntaxhighlight lang="quackery">[ 20 random
dup echo sp
dup echo sp
10 = if done
10 = if done
20 random echo cr
20 random echo cr
again ]</lang>
again ]</syntaxhighlight>


{{Out}}
{{Out}}
Line 2,808: Line 2,808:
=={{header|R}}==
=={{header|R}}==
{{works with|R|2.8.1}}
{{works with|R|2.8.1}}
<lang R>sample0to19 <- function() sample(0L:19L, 1,replace=TRUE)
<syntaxhighlight lang="r">sample0to19 <- function() sample(0L:19L, 1,replace=TRUE)
repeat
repeat
{
{
Line 2,819: Line 2,819:
result2 <- sample0to19()
result2 <- sample0to19()
cat(result1, result2, "\n")
cat(result1, result2, "\n")
}</lang>
}</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(let loop ()
(let loop ()
Line 2,831: Line 2,831:
(displayln (random 20))
(displayln (random 20))
(loop)))
(loop)))
</syntaxhighlight>
</lang>


=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
{{works with|Rakudo|#21 "Seattle"}}
{{works with|Rakudo|#21 "Seattle"}}
<lang perl6>loop {
<syntaxhighlight lang="raku" line>loop {
say my $n = (0..19).pick;
say my $n = (0..19).pick;
last if $n == 10;
last if $n == 10;
say (0..19).pick;
say (0..19).pick;
}</lang>
}</syntaxhighlight>


=={{header|REBOL}}==
=={{header|REBOL}}==
<lang REBOL>REBOL [
<syntaxhighlight lang="rebol">REBOL [
Title: "Loop/Break"
Title: "Loop/Break"
URL: http://rosettacode.org/wiki/Loop/Break
URL: http://rosettacode.org/wiki/Loop/Break
Line 2,858: Line 2,858:
print rejoin [" " r20]
print rejoin [" " r20]
]
]
print ""</lang>
print ""</syntaxhighlight>


{{out}}
{{out}}
Line 2,870: Line 2,870:
=={{header|Red}}==
=={{header|Red}}==
{{trans|REBOL}}
{{trans|REBOL}}
<lang rebol>Red [
<syntaxhighlight lang="rebol">Red [
Title: "Loops/Break"
Title: "Loops/Break"
URL: http://rosettacode.org/wiki/Loops/Break
URL: http://rosettacode.org/wiki/Loops/Break
Line 2,884: Line 2,884:
print rejoin [" " r20]
print rejoin [" " r20]
]
]
print ""</lang>
print ""</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,896: Line 2,896:


=={{header|Retro}}==
=={{header|Retro}}==
<lang Retro>doc{
<syntaxhighlight lang="retro">doc{
A couple of helper functions to make the rest of the
A couple of helper functions to make the rest of the
code more readable.
code more readable.
Line 2,918: Line 2,918:


[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while
[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program demonstrates a FOREVER DO loop with a test to LEAVE (break). */
<syntaxhighlight lang="rexx">/*REXX program demonstrates a FOREVER DO loop with a test to LEAVE (break). */
/*REXX's RANDOM BIF returns an integer.*/
/*REXX's RANDOM BIF returns an integer.*/
do forever /*perform loop until da cows come home.*/
do forever /*perform loop until da cows come home.*/
Line 2,930: Line 2,930:
say right(b, 5) /*show B right─justified, column 2.*/
say right(b, 5) /*show B right─justified, column 2.*/
end /*forever*/ /* [↑] CHAROUT , xxx writes to term.*/
end /*forever*/ /* [↑] CHAROUT , xxx writes to term.*/
/*stick a fork in it, we're all done. */</lang>
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output}}
{{out|output}}


Line 2,989: Line 2,989:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
while true
while true
a = random(20)
a = random(20)
Line 2,995: Line 2,995:
if a = 10 exit ok
if a = 10 exit ok
end
end
</syntaxhighlight>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>loop do
<syntaxhighlight lang="ruby">loop do
a = rand(20)
a = rand(20)
print a
print a
Line 3,007: Line 3,007:
b = rand(20)
b = rand(20)
puts "\t#{b}"
puts "\t#{b}"
end</lang>
end</syntaxhighlight>
or
or
<lang ruby>loop do
<syntaxhighlight lang="ruby">loop do
print a = rand(20)
print a = rand(20)
puts or break if a == 10
puts or break if a == 10
puts "\t#{rand(20)}"
puts "\t#{rand(20)}"
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,032: Line 3,032:
=={{header|Rust}}==
=={{header|Rust}}==
{{libheader|rand}}
{{libheader|rand}}
<lang rust>// cargo-deps: rand
<syntaxhighlight lang="rust">// cargo-deps: rand


extern crate rand;
extern crate rand;
Line 3,047: Line 3,047:
println!("{}", rng.gen_range(0, 20));
println!("{}", rng.gen_range(0, 20));
}
}
}</lang>
}</syntaxhighlight>


=={{header|SAS}}==
=={{header|SAS}}==
<lang sas>data _null_;
<syntaxhighlight lang="sas">data _null_;
do while(1);
do while(1);
n=floor(uniform(0)*20);
n=floor(uniform(0)*20);
Line 3,056: Line 3,056:
if n=10 then leave; /* 'leave' to break a loop */
if n=10 then leave; /* 'leave' to break a loop */
end;
end;
run;</lang>
run;</syntaxhighlight>


=={{header|Sather}}==
=={{header|Sather}}==
<lang sather>-- help class for random number sequence
<syntaxhighlight lang="sather">-- help class for random number sequence
class RANDOM is
class RANDOM is
attr seed:INT;
attr seed:INT;
Line 3,087: Line 3,087:
end;
end;
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>scala> import util.control.Breaks.{breakable, break}
<syntaxhighlight lang="scala">scala> import util.control.Breaks.{breakable, break}
import util.control.Breaks.{breakable, break}
import util.control.Breaks.{breakable, break}


Line 3,109: Line 3,109:
4
4
10
10
</syntaxhighlight>
</lang>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(let loop ((first (random 20)))
(let loop ((first (random 20)))
(print first)
(print first)
Line 3,119: Line 3,119:
(print (random 20))
(print (random 20))
(loop (random 20)))))
(loop (random 20)))))
</syntaxhighlight>
</lang>


Or by using call/cc to break out:
Or by using call/cc to break out:


<lang scheme>
<syntaxhighlight lang="scheme">
(call/cc
(call/cc
(lambda (break)
(lambda (break)
Line 3,132: Line 3,132:
(print (random 20))
(print (random 20))
(loop (random 20)))))
(loop (random 20)))))
</syntaxhighlight>
</lang>


=={{header|Scilab}}==
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
{{works with|Scilab|5.5.1}}
<lang>while %T
<syntaxhighlight lang="text">while %T
a=int(rand()*20) // [0..19]
a=int(rand()*20) // [0..19]
printf("%2d ",a)
printf("%2d ",a)
Line 3,143: Line 3,143:
printf("%2d\n",b)
printf("%2d\n",b)
end
end
printf("\n")</lang>
printf("\n")</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:20ex">
<pre style="height:20ex">
Line 3,163: Line 3,163:
is 10. The second random number does never terminate the loop.
is 10. The second random number does never terminate the loop.


<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: main is func
const proc: main is func
Line 3,176: Line 3,176:
end if;
end if;
until number = 10;
until number = 10;
end func;</lang>
end func;</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var lim = 20;
<syntaxhighlight lang="ruby">var lim = 20;
loop {
loop {
say (var n = lim.rand.int);
say (var n = lim.rand.int);
n == 10 && break;
n == 10 && break;
say lim.rand.int;
say lim.rand.int;
}</lang>
}</syntaxhighlight>


=={{header|Simula}}==
=={{header|Simula}}==
{{works with|SIMULA-67}}
{{works with|SIMULA-67}}
<lang simula>! Loops/Break - simula67 - 08/03/2017;
<syntaxhighlight lang="simula">! Loops/Break - simula67 - 08/03/2017;
begin
begin
integer num,seed;
integer num,seed;
Line 3,199: Line 3,199:
end;
end;
lab:
lab:
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,210: Line 3,210:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}
<lang smalltalk>[
<syntaxhighlight lang="smalltalk">[
|first second done|
|first second done|


Line 3,220: Line 3,220:
].
].
done
done
] whileFalse</lang>
] whileFalse</syntaxhighlight>
alternative:
alternative:
<lang smalltalk>[:exit |
<syntaxhighlight lang="smalltalk">[:exit |
|first|
|first|


Line 3,228: Line 3,228:
first == 10 ifTrue:[ exit value:nil ].
first == 10 ifTrue:[ exit value:nil ].
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</lang>
] loopWithExit.</syntaxhighlight>
or shorter (because <tt>ifTrue:</tt> sends #value to its arg):
or shorter (because <tt>ifTrue:</tt> sends #value to its arg):
<lang smalltalk>[:exit |
<syntaxhighlight lang="smalltalk">[:exit |
|first|
|first|


Line 3,236: Line 3,236:
first == 10 ifTrue:exit.
first == 10 ifTrue:exit.
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</lang>
] loopWithExit.</syntaxhighlight>


=={{header|Snabel}}==
=={{header|Snabel}}==
Uses a ranged random generator as iterator.
Uses a ranged random generator as iterator.
<lang snabel>
<syntaxhighlight lang="snabel">
let: rnd 19 random;
let: rnd 19 random;


Line 3,248: Line 3,248:
@rnd pop str say
@rnd pop str say
} for
} for
</syntaxhighlight>
</lang>


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation:
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation:
<lang snobol> input(.random,io_findunit(),1,"/dev/urandom")
<syntaxhighlight lang="snobol"> input(.random,io_findunit(),1,"/dev/urandom")
while &ALPHABET random @rand
while &ALPHABET random @rand
output = rand = rand - (rand / 20) * 20
output = rand = rand - (rand / 20) * 20
eq(rand,10) :f(while)
eq(rand,10) :f(while)
end</lang>
end</syntaxhighlight>


Or using a library function:
Or using a library function:


<lang SNOBOL4>* rand(n) -> real x | 0 <= x < n
<syntaxhighlight lang="snobol4">* rand(n) -> real x | 0 <= x < n
-include 'random.sno'
-include 'random.sno'


loop ne(output = convert(rand(20)'integer'),10) :s(loop)
loop ne(output = convert(rand(20)'integer'),10) :s(loop)
end</lang>
end</syntaxhighlight>


=={{header|Spin}}==
=={{header|Spin}}==
Line 3,271: Line 3,271:
{{works with|HomeSpun}}
{{works with|HomeSpun}}
{{works with|OpenSpin}}
{{works with|OpenSpin}}
<lang spin>con
<syntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
_clkfreq = 80_000_000
Line 3,295: Line 3,295:
waitcnt(_clkfreq + cnt)
waitcnt(_clkfreq + cnt)
ser.stop
ser.stop
cogstop(0)</lang>
cogstop(0)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,303: Line 3,303:
=={{header|SPL}}==
=={{header|SPL}}==
Direct approach:
Direct approach:
<lang spl>>
<syntaxhighlight lang="spl">>
n = #.rnd(20)
n = #.rnd(20)
#.output(n)
#.output(n)
Line 3,309: Line 3,309:
n = #.rnd(20)
n = #.rnd(20)
#.output(n)
#.output(n)
<</lang>
<</syntaxhighlight>
With reusable code:
With reusable code:
<lang spl>>
<syntaxhighlight lang="spl">>
:1
:1
n = #.rnd(20)
n = #.rnd(20)
Line 3,318: Line 3,318:
<< n=10
<< n=10
1 <->
1 <->
<</lang>
<</syntaxhighlight>


=={{header|SQL PL}}==
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
With SQL PL:
<lang sql pl>
<syntaxhighlight lang="sql pl">
--#SET TERMINATOR @
--#SET TERMINATOR @


Line 3,340: Line 3,340:
END WHILE LOOP;
END WHILE LOOP;
END @
END @
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,358: Line 3,358:
</pre>
</pre>
Since V11.1, the builtin module can be used instead of RAND, like this:
Since V11.1, the builtin module can be used instead of RAND, like this:
<lang sql pl>
<syntaxhighlight lang="sql pl">
SET VAL = CALL DBMS_RANDOM.VALUE(0,20);
SET VAL = CALL DBMS_RANDOM.VALUE(0,20);
</syntaxhighlight>
</lang>


=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>while 1 {
<syntaxhighlight lang="stata">while 1 {
local n=runiformint(0,19)
local n=runiformint(0,19)
display `n'
display `n'
if `n'==10 continue, break
if `n'==10 continue, break
display runiformint(0,19)
display runiformint(0,19)
}</lang>
}</syntaxhighlight>


=== Mata ===
=== Mata ===
<lang stata>for (; 1; ) {
<syntaxhighlight lang="stata">for (; 1; ) {
printf("%f\n",n=runiformint(1,1,0,19))
printf("%f\n",n=runiformint(1,1,0,19))
if (n==10) break
if (n==10) break
printf("%f\n",runiformint(1,1,0,19))
printf("%f\n",runiformint(1,1,0,19))
}</lang>
}</syntaxhighlight>


=={{header|Suneido}}==
=={{header|Suneido}}==
<lang Suneido>forever
<syntaxhighlight lang="suneido">forever
{
{
Print(i = Random(20))
Print(i = Random(20))
Line 3,385: Line 3,385:
Print(i = Random(20))
Print(i = Random(20))
}
}
</syntaxhighlight>
</lang>


=={{header|Swift}}==
=={{header|Swift}}==
<lang Swift>while true
<syntaxhighlight lang="swift">while true
{
{
let a = Int(arc4random()) % (20)
let a = Int(arc4random()) % (20)
Line 3,399: Line 3,399:
print("b: \(b)")
print("b: \(b)")
}
}
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
a: 2 b: 7
a: 2 b: 7
Line 3,408: Line 3,408:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>while true {
<syntaxhighlight lang="tcl">while true {
set a [expr int(20*rand())]
set a [expr int(20*rand())]
puts $a
puts $a
Line 3,416: Line 3,416:
set b [expr int(20*rand())]
set b [expr int(20*rand())]
puts $b
puts $b
}</lang>
}</syntaxhighlight>


=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==


<lang ti89b>Local x
<syntaxhighlight lang="ti89b">Local x
Loop
Loop
rand(20)-1 → x
rand(20)-1 → x
Line 3,428: Line 3,428:
EndIf
EndIf
Output 64, 50, rand(20)-1 © paint text to the right on same line
Output 64, 50, rand(20)-1 © paint text to the right on same line
EndLoop</lang>
EndLoop</syntaxhighlight>


=={{header|TorqueScript}}==
=={{header|TorqueScript}}==


<lang Torque>for(%a = 0; %a > -1; %a++)
<syntaxhighlight lang="torque">for(%a = 0; %a > -1; %a++)
{
{
%number = getRandom(0, 19);
%number = getRandom(0, 19);
if(%number == 10)
if(%number == 10)
break;
break;
}</lang>
}</syntaxhighlight>


=={{header|Transact-SQL}}==
=={{header|Transact-SQL}}==


<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
DECLARE @i INT;
DECLARE @i INT;
WHILE 1=1
WHILE 1=1
Line 3,450: Line 3,450:
PRINT ABS(CHECKSUM(NewId())) % 20;
PRINT ABS(CHECKSUM(NewId())) % 20;
END;
END;
</syntaxhighlight>
</lang>


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<syntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
LOOP
LOOP
Line 3,466: Line 3,466:
IF (10==a,b) STOP
IF (10==a,b) STOP
ENDLOOP
ENDLOOP
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,480: Line 3,480:


=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
<lang>Do
<syntaxhighlight lang="text">Do
n = RND(20)
n = RND(20)
Print n
Print n
Until n = 10
Until n = 10
Print RND(20)
Print RND(20)
Loop</lang>
Loop</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
Line 3,493: Line 3,493:
{{works with|Bourne Shell}}
{{works with|Bourne Shell}}
{{libheader|jot}}
{{libheader|jot}}
<lang bash>while true; do
<syntaxhighlight lang="bash">while true; do
a=`jot -w %d -r 1 0 20` || exit $?
a=`jot -w %d -r 1 0 20` || exit $?
echo $a
echo $a
Line 3,499: Line 3,499:
b=`jot -w %d -r 1 0 20` || exit $?
b=`jot -w %d -r 1 0 20` || exit $?
echo $b
echo $b
done</lang>
done</syntaxhighlight>


Korn Shells have a RANDOM parameter.
Korn Shells have a RANDOM parameter.
Line 3,505: Line 3,505:
{{works with|Bash}}
{{works with|Bash}}
{{works with|pdksh|5.2.14}}
{{works with|pdksh|5.2.14}}
<lang bash>while true; do
<syntaxhighlight lang="bash">while true; do
echo $((a=RANDOM%20))
echo $((a=RANDOM%20))
[ $a -eq 10 ] && break
[ $a -eq 10 ] && break
echo $((b=RANDOM%20))
echo $((b=RANDOM%20))
done</lang>
done</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
{{trans|Python}}
{{trans|Python}}
<lang ursa>decl ursa.util.random r
<syntaxhighlight lang="ursa">decl ursa.util.random r
decl int a b
decl int a b
while true
while true
Line 3,523: Line 3,523:
set b (r.getint 19)
set b (r.getint 19)
out b endl console
out b endl console
end while</lang>
end while</syntaxhighlight>


=={{header|VBA}}==
=={{header|VBA}}==
<lang VB>Public Sub LoopsBreak()
<syntaxhighlight lang="vb">Public Sub LoopsBreak()
Dim value As Integer
Dim value As Integer
Randomize
Randomize
Line 3,535: Line 3,535:
Debug.Print Int(20 * Rnd)
Debug.Print Int(20 * Rnd)
Loop
Loop
End Sub</lang>
End Sub</syntaxhighlight>


=={{header|VBScript}}==
=={{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).
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).


<lang vb>Dim a, b, i
<syntaxhighlight lang="vb">Dim a, b, i


Do
Do
Line 3,556: Line 3,556:
b = Int(Rnd * 20)
b = Int(Rnd * 20)
WScript.Echo vbNullString, b
WScript.Echo vbNullString, b
Next</lang>
Next</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Program
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Sub Main()
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though).
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though).
Line 3,575: Line 3,575:
Loop
Loop
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{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>
<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|Vlang}}==
=={{header|Vlang}}==
<lang vlang>import rand
<syntaxhighlight lang="vlang">import rand
import rand.seed
import rand.seed


Line 3,594: Line 3,594:
println(b)
println(b)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random


var r = Random.new()
var r = Random.new()
Line 3,605: Line 3,605:
if (n == 10) break
if (n == 10) break
System.print(r.int(20))
System.print(r.int(20))
} </lang>
} </syntaxhighlight>


{{out}}
{{out}}
Line 3,621: Line 3,621:
=={{header|XBasic}}==
=={{header|XBasic}}==
{{works with|Windows XBasic}}
{{works with|Windows XBasic}}
<lang xbasic>
<syntaxhighlight lang="xbasic">
PROGRAM "loopbreak"
PROGRAM "loopbreak"


Line 3,658: Line 3,658:


END PROGRAM
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 3,675: Line 3,675:


=={{header|XBS}}==
=={{header|XBS}}==
<lang xbs>while(true){
<syntaxhighlight lang="xbs">while(true){
set n:number = math.random(0,19);
set n:number = math.random(0,19);
log(`first: {n}`);
log(`first: {n}`);
Line 3,681: Line 3,681:
n = math.random(0,19);
n = math.random(0,19);
log(`second: {n}`);
log(`second: {n}`);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,698: Line 3,698:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>include c:\cxpl\codes;
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
int N;
int N;
loop [N:= Ran(20);
loop [N:= Ran(20);
Line 3,706: Line 3,706:
IntOut(0, Ran(20));
IntOut(0, Ran(20));
CrLf(0);
CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,722: Line 3,722:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang yabasic>do
<syntaxhighlight lang="yabasic">do
i = int(ran(19))
i = int(ran(19))
print i using "##";
print i using "##";
Line 3,731: Line 3,731:
loop
loop
print
print
end</lang>
end</syntaxhighlight>


=={{header|Zig}}==
=={{header|Zig}}==
<lang zig>const std = @import("std");
<syntaxhighlight lang="zig">const std = @import("std");


pub fn main() !void {
pub fn main() !void {
Line 3,750: Line 3,750:
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2});
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2});
}
}
}</lang>
}</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>while(1){n:=(0).random(20); n.print(" ");
<syntaxhighlight lang="zkl">while(1){n:=(0).random(20); n.print(" ");
if (n==10){ println(); break; } (0).random().println();
if (n==10){ println(); break; } (0).random().println();
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>