Loops/Break: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|J}}: some alternatives) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30:
=={{header|11l}}==
{{trans|Python}}
<
V a = random:(20)
print(a)
Line 36:
L.break
V b = random:(20)
print(b)</
=={{header|360 Assembly}}==
<
LOOPBREA CSECT
USING LOOPBREA,R13 base register
Line 77:
XDEC DS CL12
YREGS
END LOOPBREA</
{{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.
<
Line 110:
Break: PLA ;restore accumulator from stack
RTS ;return from subroutine</
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight 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>
{{output}}
<pre>
Line 227:
</pre>
=={{header|Action!}}==
<
BYTE v
Line 239:
OD
PrintE("After loop")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer]
Line 257:
=={{header|Ada}}==
<
with Ada.Numerics.Discrete_Random;
Line 274:
Put_Line (Value_Type'Image (B));
end loop;
end Test_Loop_Break;</
=={{header|Aime}}==
<
main(void)
{
Line 296:
return 0;
}</
=={{header|ALGOL 60}}==
{{works with|ALGOL 60|OS/360}}
<
'INTEGER' SEED;
'INTEGER' 'PROCEDURE' RANDOM(N);
Line 324:
LAB:
SYSACT(1,14,1); 'COMMENT' skip line;
'END'</
{{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)}}
<
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)
)</
{{out}}
<pre style="height:25ex;overflow:scroll">
Line 383:
=={{header|AppleScript}}==
<
set a to random number from 0 to 19
if a is 10 then
Line 391:
set b to random number from 0 to 19
log a & b
end repeat</
Line 439:
=={{header|Arc}}==
<
(while t
(let x (rand 20)
Line 445:
(if (is x 10)
(break)))
(prn "b: " (rand 20))))</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 635:
</syntaxhighlight>
=={{header|Arturo}}==
<
a: random 0 19
prints [a ""]
Line 647:
print b
]
print ""</
{{out}}
Line 664:
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">Loop
{
Random, var, 0, 19
Line 673:
output = %output%`n%var%
}
MsgBox % output</
=={{header|Avail}}==
<
checked : [0..19];
Do [
Line 683:
] while checked ≠ 10 alternate with [
Print: " " ++ “rng's next [0..19]” ++ "\n";
];</
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}}==
<
for (;;) {
print n = int(rand() * 20)
Line 695:
print int(rand() * 20)
}
}</
=={{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.
<
rand^20→A
Disp A▶Dec
Line 706:
rand^20→B
Disp B▶Dec,i
End</
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
==={{header|BaCon}}===
<
REPEAT
number = RANDOM(20)
Line 720:
ENDIF
PRINT "second ",RANDOM(20)
UNTIL FALSE</
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">do
i = int(rand * 19)
print i; " ";
Line 731:
until false
print
end</
==={{header|Commodore BASIC}}===
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive).
<
20 A = INT(RND(1)*20)
30 PRINT A
Line 742:
60 PRINT B
70 GOTO 20
80 END</
==={{header|IS-BASIC}}===
<
110 DO
120 LET A=RND(20)+1
Line 751:
140 IF A=10 THEN EXIT DO
150 PRINT RND(20)+1
160 LOOP</
==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<
a = int(rnd * 20)
print a
Line 761:
b = int(rnd * 20)
print b
loop</
==={{header|True BASIC}}===
<
DO
Line 777:
LOOP
PRINT
END</
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:
<
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
30 PRINT l
40 NEXT l
50 STOP</
The correct solution:
<
20 PRINT a
30 IF a = 10 THEN STOP
40 PRINT INT (RND * 20)
50 GO TO 10</
=={{header|Batch File}}==
<
:loop
set /a N=%RANDOM% %% 20
Line 805:
set /a N=%RANDOM% %% 20
echo %N%
goto loop</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
num% = RND(20)-1
PRINT num%
IF num%=10 THEN EXIT REPEAT
PRINT RND(20)-1
UNTIL FALSE</
=={{header|bc}}==
<
scale = 0
Line 839:
r() /* print 2nd number */
}
quit</
=={{header|Befunge}}==
<syntaxhighlight lang="befunge">
>60v *2\<
>?>\1-:|
Line 854:
>^ 7
^ .%++67<
</syntaxhighlight>
=={{header|C}}==
<syntaxhighlight lang="c">
int main(){
time_t t;
Line 872:
}
return 0;
}</
Output (example):
<pre>
Line 888:
=={{header|C sharp|C#}}==
<
{
static void Main(string[] args)
Line 905:
Console.ReadLine();
}
}</
=={{header|C++}}==
<
#include <ctime>
#include <cstdlib>
Line 923:
}
return 0;
}</
=={{header|Chapel}}==
<
var r = new RandomStream();
Line 936:
writeln(b);
}
delete r;</
=={{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'>
<
Makes a whole lot of sauce for two people.
Line 998:
Pour contents of the 1st mixing bowl into the 1st baking dish.
Serves 2.</
</div>
=={{header|Clojure}}==
<
(println a)
(when-not (= 10 a)
(println b)
(recur more)))</
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<
PROGRAM-ID. Random-Nums.
Line 1,035:
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20)
DISPLAY Num
.</
=={{header|CoffeeScript}}==
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this:
<
loop
print a = Math.random() * 20 // 1
break if a == 10
print Math.random() * 20 // 1
</syntaxhighlight>
=={{header|ColdFusion}}==
<
<Cfset randNum = 0>
<cfloop condition="randNum neq 10">
Line 1,056:
<Br>
</cfloop>
</syntaxhighlight>
{{out}}
My first two test outputs (I swear this is true)
Line 1,124:
=={{header|Common Lisp}}==
<
do (print a)
until (= a 10)
do (print (random 20)))</
=== Using DO ===
<
(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>
{{out}}
Line 1,159:
=={{header|D}}==
<
void main() {
Line 1,169:
write(uniform(0, 20), " ");
}
}</
{{out}}
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre>
Line 1,175:
=={{header|dc}}==
{{trans|bc}}
<
0k [scale = 0]sz
Line 1,199:
0 0 =r p sz [Print 2nd number.]sz
0 0 =B [Continue loop.]sz
]sB 0 0 =B</
=={{header|Delphi}}==
<
{$APPTYPE CONSOLE}
Line 1,219:
end.
</syntaxhighlight>
=={{header|DWScript}}==
<
while True do begin
var num := RandomInt(20);
PrintLn(num);
if num=10 then Break;
end;</
=={{header|E}}==
<
def a := entropy.nextInt(20)
print(a)
Line 1,239:
}
println(" ", entropy.nextInt(20))
}</
=={{header|EasyLang}}==
<syntaxhighlight lang="text">repeat
a = random 20
print a
until a = 10
print random 20
.</
=={{header|Eiffel}}==
<
example
-- Eiffel example code
Line 1,266:
end
end
</syntaxhighlight>
{{out}}
The output is superfluous and unneeded to read and understand what the Eiffel code is doing.
Line 1,278:
As a result, an output looks almost truly random:
<
loop = loop' 1
Line 1,289:
loop 10 ::: IO</
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<
def break, do: break(random)
Line 1,305:
end
Loops.break</
{{out}}
Line 1,327:
=={{header|Emacs Lisp}}==
<
(catch 'loop-break
(while 't
Line 1,336:
(message "current number is: %d" math) ) ) ) ) )
(wait_10)</
=={{header|Erlang}}==
<
-module(forever).
-export([main/0, for/0]).
Line 1,356:
for()
end.
</syntaxhighlight>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
LOOP
A=INT(RND(1)*20)
Line 1,366:
PRINT(INT(RND(1)*20))
END LOOP
</syntaxhighlight>
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument.
=={{header|Euphoria}}==
<
while 1 do
i = rand(20) - 1
Line 1,378:
end if
printf(1, "%g ", {rand(20)-1})
end while</
The <code>rand()</code> function returns a random integer from 1 to the integer provided.
=={{header|F_Sharp|F#}}==
<
// 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>
=={{header|Factor}}==
Using <code>with-return</code>:
<
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop
] with-return</
Idiomatic Factor:
<
=={{header|Fantom}}==
<
class ForBreak
{
Line 1,414:
}
}
</syntaxhighlight>
=={{header|Forth}}==
<
: main
Line 1,430:
10 = if leave then
i random .
loop ;</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,450:
end do
end program Example</
{{works with|Fortran|77 and later}}
<
INTEGER I, RNDINT
Line 1,521:
ENDIF
RETURN
END</
{{works with|Fortran|66 and earlier}}
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!
<syntaxhighlight lang="fortran">
SUBROUTINE RANDU(IX,IY,YFL)
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60.
Line 1,561:
20 CONTINUE
END
</syntaxhighlight>
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,567:
=={{header|FreeBASIC}}==
<
Dim i As Integer
Line 1,582:
Print
Sleep</
Sample output
Line 1,593:
=={{header|Frink}}==
<
{
a = random[0,19]
Line 1,602:
b = random[0,19]
print["$b "]
}</
{{out}}
<pre>
Line 1,609:
=={{header|FutureBasic}}==
<
long num
Line 1,617:
until ( num == 10 )
HandleEvents</
=={{header|Gambas}}==
<
Dim iRand As Integer
Line 1,628:
Until iRand = 10
End</
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]'''
<
Dim byNo As Byte
Line 1,641:
Loop
End</
Output:
<pre>
Line 1,648:
=={{header|GAP}}==
<
a := Random(0, 19);
Print(a);
Line 1,664:
# 5 10
# 1 16
# 10</
=={{header|GML}}==
<
{
a = floor(random(19))
Line 1,676:
show_message(string(a))
}
</syntaxhighlight>
=={{header|Go}}==
<
import "fmt"
Line 1,696:
fmt.Println(b)
}
}</
=={{header|Groovy}}==
<
while (true) {
Line 1,707:
print ' '
println random.nextInt(20)
}</
=={{header|GW-BASIC}}==
<
20 WHILE NUM <> 10
30 NUM = INT(RND * 20)
40 PRINT NUM
50 WEND</
=={{header|Harbour}}==
<
LOCAL n
Line 1,729:
ENDDO
RETURN</
=={{header|Haskell}}==
<
import System.Random
Line 1,740:
unless (r==k) $ do
print =<< randomRIO (0,n)
loopBreak n k</
Use:
<syntaxhighlight lang
=={{header|Haxe}}==
<
static public function main():Void {
while(true) {
Line 1,756:
}
}
}</
=={{header|hexiscript}}==
<
let r rand 20
println r
Line 1,766:
endif
println rand 20
endwhile</
=={{header|HicEst}}==
<
a = INT( RAN(10, 10) )
WRITE(name) a
Line 1,777:
ENDDO
10
END</
=={{header|HolyC}}==
<
while (1) {
a = RandU16 % 20;
Line 1,791:
Print("%d\n", b);
}
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
<
while 10 ~= writes(?20-1) do write(", ",?20-1)
end</
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 <
=={{header|Io}}==
<
a := Random value(0,20) floor
write(a)
Line 1,810:
b := Random value(0,20) floor
writeln(" ",b)
)</
=={{header|J}}==
<
while. do.
echo k=. ?20
Line 1,819:
echo ?20
end.
}}</
Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>:
<
while. do.
echo k=. ?20
Line 1,829:
echo ?20
end.
)</
<
while. do.
echo k=. ?20
Line 1,838:
end.
label_done.
}}</
=={{header|Java}}==
<
Random rand = new Random();
Line 1,850:
int b = rand.nextInt(20);
System.out.println(b);
}</
=={{header|JavaScript}}==
<
var a = Math.floor(Math.random() * 20);
print(a);
Line 1,860:
a = Math.floor(Math.random() * 20);
print(a);
}</
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell.
Line 1,868:
In a functional idiom of JavaScript, we might instead write something like:
<
var nFirst = Math.floor(Math.random() * 20);
Line 1,880:
return streamTillInitialTen();
})();</
Obtaining runs like:
Line 1,921:
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread:
<
(function streamTillInitialTen() {
var nFirst = Math.floor(Math.random() * 20);
Line 1,934:
);
})().join('\n')
);</
Sample result:
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 -
<
# from the Microsoft C Runtime.
# Input: [ count, state, rand ]
Line 1,977:
# Generate random integers from 0 to (n-1):
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</
'''"take"'''
<
label $done
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</
'''"count"'''
Since the PRNG used here is deterministic, we'll just count the number of integers generated:
<
'''Example'''
Line 1,996:
=={{header|Julia}}==
<syntaxhighlight lang="julia">
while true
n = rand(0:19)
Line 2,007:
@printf "%4d\n" n
end
</syntaxhighlight>
{{out}}
<pre>
Line 2,027:
=={{header|Kotlin}}==
{{trans|Java}}
<
fun main(args: Array<String>) {
Line 2,037:
println(rand.nextInt(20))
}
}</
=={{header|Lambdatalk}}==
Line 2,050:
{loops_break 0}
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop
</syntaxhighlight>
=={{header|Lang5}}==
<
=={{header|langur}}==
<
val .i = random 0..19
write .i, " "
if .i == 10 { writeln(); break }
write random(0..19), " "
}</
{{out}}
Line 2,067:
=={{header|Lasso}}==
<
while(#x != 10) => {^
#x = integer_random(19,0)
Line 2,073:
#x == 10 ? loop_abort
', '+integer_random(19,0)+'\r'
^}</
=={{header|Liberty BASIC}}==
The task specifies a "number".
<
num=rnd(1)*20
print num
Line 2,083:
print rnd(1)*20
wend
</
<
num=int(rnd(1)*20)
print num
Line 2,090:
print int(rnd(1)*20)
wend
</syntaxhighlight>
=={{header|Lingo}}==
<
n = random(20)-1
put n
if n = 10 then exit repeat
put random(20)-1
end repeat</
=={{header|Lisaac}}==
<
+ name := TEST_LOOP_BREAK;
Line 2,121:
'\n'.print;
}
);</
=={{header|LiveCode}}==
<
repeat forever
put random(20) - 1 into tRand
Line 2,132:
end repeat
end loopForeverRandom
</syntaxhighlight>
=={{header|Lua}}==
<
k = math.random(19)
print(k)
if k == 10 then break end
print(math.random(19)
until false</
=={{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">
Module Checkit {
M=Random(0, 19)
Line 2,171:
}
Checkit
</syntaxhighlight>
=={{header|M4}}==
<
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
Line 2,185:
loopbreak')')dnl
dnl
loopbreak</
{{out}}
Line 2,197:
=={{header|Maple}}==
<
do
n := r();
Line 2,205:
end if;
printf( "%d\n", r() );
end do:</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Print[RandomInteger[{0,19}]
]</
=={{header|Maxima}}==
<
block([n],
Line 2,248:
> x: 2;
> exit;
2</
=={{header|MAXScript}}==
<syntaxhighlight lang="maxscript">
while true do
(
Line 2,260:
format ("B: % \n") b
)
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.19.6}}
<
(19 random puts 10 ==) (19 random puts!) () () linrec</
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">СЧ 2 0 * П0
1 0 - [x] x#0 18
СЧ 2 0 * П1
БП 00 ИП0 С/П</
=={{header|Modula-3}}==
<
IMPORT IO, Fmt, Random;
Line 2,290:
END;
END;
END Break.</
=={{header|MOO}}==
<
a = random(20) - 1;
player:tell(a);
Line 2,301:
b = random(20) - 1;
player:tell(b);
endwhile</
=={{header|MUMPS}}==
<
NEW A,B
SET A=""
Line 2,318:
NEW A,B
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B
KILL A,B QUIT</
{{out}}
<pre>USER>D BREAKLOOP^ROSETTA
Line 2,343:
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
Loops/Break in Neko
Tectonics:
Line 2,364:
$print(r, " ");
}
$print("\n");</
{{out}}
Line 2,373:
=={{header|Nemerle}}==
{{trans|C#}}
<
using System.Console;
using Nemerle.Imperative;
Line 2,391:
}
}
}</
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 2,410:
end lb
say
</syntaxhighlight>
=={{header|NewLISP}}==
<
(println (rand 20)))</
=={{header|Nim}}==
{{trans|Python}}
<
while true:
Line 2,426:
break
let b = random(19)
echo b</
=={{header|NS-HUBASIC}}==
<
20 PRINT I
30 IF I=10 THEN STOP
40 PRINT RND(20)
50 GOTO 10</
=={{header|Oberon-2}}==
Works with oo2c Version 2
<
MODULE LoopBreak;
IMPORT
Line 2,459:
Do
END LoopBreak.
</syntaxhighlight>
=={{header|Objeck}}==
<
while(true) {
a := (Float->Random() * 20.0)->As(Int);
Line 2,472:
a->PrintLine();
}
</syntaxhighlight>
=={{header|OCaml}}==
<
- : unit = ()
Line 2,492:
13
10
Exception: Pervasives.Exit.</
=={{header|Octave}}==
<
a = floor(unifrnd(0,20, 1));
disp(a)
Line 2,503:
b = floor(unifrnd(0,20, 1));
disp(b)
endwhile</
=={{header|Oforth}}==
<
19 rand dup print ":" print
10 == ifTrue: [ break ]
19 rand print " " print
]</
=={{header|Ol}}==
<
(import (otus random!))
Line 2,522:
(print (rand! 20))
(loop))))
</syntaxhighlight>
=={{header|ooRexx}}==
<
* Three Ways to leave a Loop
* ooRexx added the possibility to leave an outer loop
Line 2,569:
End
Else
Say 'Leave label-name is probably not supported in' v</
{{out}}
<pre>i1=1
Line 2,596:
=={{header|Oz}}==
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic.
<
R = {OS.rand} mod 20
in
Line 2,603:
else {Show {OS.rand} mod 20}
end
end</
=={{header|PARI/GP}}==
<
t=random(20);
print(t);
if(t==10, break);
print(random(20))
)</
=={{header|Pascal}}==
Line 2,617:
=={{header|Perl}}==
<
my $a = int(rand(20));
print "$a\n";
Line 2,625:
my $b = int(rand(20));
print "$b\n";
}</
=={{header|Phix}}==
Line 2,632:
The rand() function returns a random integer from 1 to the integer provided.
<!--<
<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,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: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</
{{out}}
Line 2,651:
=={{header|PHP}}==
<
$a = rand(0,19);
echo "$a\n";
Line 2,658:
$b = rand(0,19);
echo "$b\n";
}</
=={{header|PicoLisp}}==
Literally:
<
(loop
(println (setq R (rand 1 19)))
(T (= 10 R))
(println (rand 1 19)) ) )</
Shorter:
<
(println (rand 1 19)) )</
=={{header|Pike}}==
<
while(1){
int a = random(20);
Line 2,682:
write(b + "\n");
}
}</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
do forever;
k = trunc(random()*20);
Line 2,693:
put skip list (k);
end;
</syntaxhighlight>
=={{header|Plain English}}==
<
Start up.
Demonstrate breaking.
Line 2,712:
To write a number to the console:
Convert the number to a string.
Write the string to the console.</
=={{header|PostScript}}==
<
{
rand 20 mod % generate number between 0 and 19
dup = % print it
10 eq { exit } if % exit if 10
} loop</
=={{header|PowerShell}}==
<
for () {
$n = $r.Next(20)
Line 2,731:
}
Write-Host $r.Next(20)
}</
=={{header|PureBasic}}==
<
Repeat
Line 2,750:
Input()
CloseConsole()
EndIf</
=={{header|Python}}==
<
while True:
Line 2,761:
break
b = randrange(20)
print(b)</
=={{header|QB64}}==
''CBTJD'': 2020/03/14
<
DO
n = INT(RND * 20)
Line 2,775:
PRINT n
END IF
LOOP UNTIL 0</
=={{header|Qi}}==
<syntaxhighlight lang="qi">
(define loop -> (if (= 10 (PRINT (random 20)))
true
Line 2,784:
(loop))))
(loop)
</syntaxhighlight>
=={{header|Quackery}}==
<
dup echo sp
10 = if done
20 random echo cr
again ]</
{{Out}}
Line 2,808:
=={{header|R}}==
{{works with|R|2.8.1}}
<
repeat
{
Line 2,819:
result2 <- sample0to19()
cat(result1, result2, "\n")
}</
=={{header|Racket}}==
<
#lang racket
(let loop ()
Line 2,831:
(displayln (random 20))
(loop)))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|#21 "Seattle"}}
<syntaxhighlight lang="raku"
say my $n = (0..19).pick;
last if $n == 10;
say (0..19).pick;
}</
=={{header|REBOL}}==
<
Title: "Loop/Break"
URL: http://rosettacode.org/wiki/Loop/Break
Line 2,858:
print rejoin [" " r20]
]
print ""</
{{out}}
Line 2,870:
=={{header|Red}}==
{{trans|REBOL}}
<
Title: "Loops/Break"
URL: http://rosettacode.org/wiki/Loops/Break
Line 2,884:
print rejoin [" " r20]
]
print ""</
{{out}}
<pre>
Line 2,896:
=={{header|Retro}}==
<
A couple of helper functions to make the rest of the
code more readable.
Line 2,918:
[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while
</syntaxhighlight>
=={{header|REXX}}==
<
/*REXX's RANDOM BIF returns an integer.*/
do forever /*perform loop until da cows come home.*/
Line 2,930:
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. */</
{{out|output}}
Line 2,989:
=={{header|Ring}}==
<
while true
a = random(20)
Line 2,995:
if a = 10 exit ok
end
</syntaxhighlight>
=={{header|Ruby}}==
<
a = rand(20)
print a
Line 3,007:
b = rand(20)
puts "\t#{b}"
end</
or
<
print a = rand(20)
puts or break if a == 10
puts "\t#{rand(20)}"
end</
{{out}}
Line 3,032:
=={{header|Rust}}==
{{libheader|rand}}
<
extern crate rand;
Line 3,047:
println!("{}", rng.gen_range(0, 20));
}
}</
=={{header|SAS}}==
<
do while(1);
n=floor(uniform(0)*20);
Line 3,056:
if n=10 then leave; /* 'leave' to break a loop */
end;
run;</
=={{header|Sather}}==
<
class RANDOM is
attr seed:INT;
Line 3,087:
end;
end;
end;</
=={{header|Scala}}==
<
import util.control.Breaks.{breakable, break}
Line 3,109:
4
10
</syntaxhighlight>
=={{header|Scheme}}==
<
(let loop ((first (random 20)))
(print first)
Line 3,119:
(print (random 20))
(loop (random 20)))))
</syntaxhighlight>
Or by using call/cc to break out:
<
(call/cc
(lambda (break)
Line 3,132:
(print (random 20))
(loop (random 20)))))
</syntaxhighlight>
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">while %T
a=int(rand()*20) // [0..19]
printf("%2d ",a)
Line 3,143:
printf("%2d\n",b)
end
printf("\n")</
{{out}}
<pre style="height:20ex">
Line 3,163:
is 10. The second random number does never terminate the loop.
<
const proc: main is func
Line 3,176:
end if;
until number = 10;
end func;</
=={{header|Sidef}}==
<
loop {
say (var n = lim.rand.int);
n == 10 && break;
say lim.rand.int;
}</
=={{header|Simula}}==
{{works with|SIMULA-67}}
<
begin
integer num,seed;
Line 3,199:
end;
lab:
end</
{{out}}
<pre>
Line 3,210:
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<
|first second done|
Line 3,220:
].
done
] whileFalse</
alternative:
<
|first|
Line 3,228:
first == 10 ifTrue:[ exit value:nil ].
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</
or shorter (because <tt>ifTrue:</tt> sends #value to its arg):
<
|first|
Line 3,236:
first == 10 ifTrue:exit.
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.</
=={{header|Snabel}}==
Uses a ranged random generator as iterator.
<
let: rnd 19 random;
Line 3,248:
@rnd pop str say
} for
</syntaxhighlight>
=={{header|SNOBOL4}}==
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation:
<
while &ALPHABET random @rand
output = rand = rand - (rand / 20) * 20
eq(rand,10) :f(while)
end</
Or using a library function:
<
-include 'random.sno'
loop ne(output = convert(rand(20)'integer'),10) :s(loop)
end</
=={{header|Spin}}==
Line 3,271:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 3,295:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>
Line 3,303:
=={{header|SPL}}==
Direct approach:
<
n = #.rnd(20)
#.output(n)
Line 3,309:
n = #.rnd(20)
#.output(n)
<</
With reusable code:
<
:1
n = #.rnd(20)
Line 3,318:
<< n=10
1 <->
<</
=={{header|SQL PL}}==
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 3,340:
END WHILE LOOP;
END @
</syntaxhighlight>
Output:
<pre>
Line 3,358:
</pre>
Since V11.1, the builtin module can be used instead of RAND, like this:
<
SET VAL = CALL DBMS_RANDOM.VALUE(0,20);
</syntaxhighlight>
=={{header|Stata}}==
<
local n=runiformint(0,19)
display `n'
if `n'==10 continue, break
display runiformint(0,19)
}</
=== Mata ===
<
printf("%f\n",n=runiformint(1,1,0,19))
if (n==10) break
printf("%f\n",runiformint(1,1,0,19))
}</
=={{header|Suneido}}==
<
{
Print(i = Random(20))
Line 3,385:
Print(i = Random(20))
}
</syntaxhighlight>
=={{header|Swift}}==
<
{
let a = Int(arc4random()) % (20)
Line 3,399:
print("b: \(b)")
}
</
<pre>
a: 2 b: 7
Line 3,408:
=={{header|Tcl}}==
<
set a [expr int(20*rand())]
puts $a
Line 3,416:
set b [expr int(20*rand())]
puts $b
}</
=={{header|TI-89 BASIC}}==
<
Loop
rand(20)-1 → x
Line 3,428:
EndIf
Output 64, 50, rand(20)-1 © paint text to the right on same line
EndLoop</
=={{header|TorqueScript}}==
<
{
%number = getRandom(0, 19);
if(%number == 10)
break;
}</
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
DECLARE @i INT;
WHILE 1=1
Line 3,450:
PRINT ABS(CHECKSUM(NewId())) % 20;
END;
</syntaxhighlight>
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
LOOP
Line 3,466:
IF (10==a,b) STOP
ENDLOOP
</syntaxhighlight>
{{out}}
<pre>
Line 3,480:
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">Do
n = RND(20)
Print n
Until n = 10
Print RND(20)
Loop</
=={{header|UNIX Shell}}==
Line 3,493:
{{works with|Bourne Shell}}
{{libheader|jot}}
<
a=`jot -w %d -r 1 0 20` || exit $?
echo $a
Line 3,499:
b=`jot -w %d -r 1 0 20` || exit $?
echo $b
done</
Korn Shells have a RANDOM parameter.
Line 3,505:
{{works with|Bash}}
{{works with|pdksh|5.2.14}}
<
echo $((a=RANDOM%20))
[ $a -eq 10 ] && break
echo $((b=RANDOM%20))
done</
=={{header|Ursa}}==
{{trans|Python}}
<
decl int a b
while true
Line 3,523:
set b (r.getint 19)
out b endl console
end while</
=={{header|VBA}}==
<
Dim value As Integer
Randomize
Line 3,535:
Debug.Print Int(20 * Rnd)
Loop
End Sub</
=={{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).
<
Do
Line 3,556:
b = Int(Rnd * 20)
WScript.Echo vbNullString, b
Next</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Sub Main()
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though).
Line 3,575:
Loop
End Sub
End Module</
{{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|Vlang}}==
<
import rand.seed
Line 3,594:
println(b)
}
}</
=={{header|Wren}}==
<
var r = Random.new()
Line 3,605:
if (n == 10) break
System.print(r.int(20))
} </
{{out}}
Line 3,621:
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<
PROGRAM "loopbreak"
Line 3,658:
END PROGRAM
</syntaxhighlight>
{{out}}
<pre>
Line 3,675:
=={{header|XBS}}==
<
set n:number = math.random(0,19);
log(`first: {n}`);
Line 3,681:
n = math.random(0,19);
log(`second: {n}`);
}</
{{out}}
<pre>
Line 3,698:
=={{header|XPL0}}==
<
int N;
loop [N:= Ran(20);
Line 3,706:
IntOut(0, Ran(20));
CrLf(0);
]</
{{out}}
Line 3,722:
=={{header|Yabasic}}==
<
i = int(ran(19))
print i using "##";
Line 3,731:
loop
print
end</
=={{header|Zig}}==
<
pub fn main() !void {
Line 3,750:
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2});
}
}</
=={{header|zkl}}==
<
if (n==10){ println(); break; } (0).random().println();
}</
{{out}}
<pre>
|