Loops/Break: Difference between revisions

4,352 bytes added ,  2 months ago
(Jakt)
 
(17 intermediate revisions by 10 users not shown)
Line 380:
+7 +4
+10
</pre>
 
=={{header|Amazing Hopper}}==
<p>Flavour "Jambo"</p>
<syntaxhighlight lang="c">
#include <jambo.h>
 
Main
Loop
Break if ' Int rand(20) ---show--- Is equal to (10) '
Printnl ( "--", Int rand(20) )
Back
Print '"\nEnd of Loop\n" '
End
 
</syntaxhighlight>
<p>Assembler Hopper version of this program:</p>
<syntaxhighlight lang="amazing hopper">
main:
____CODE_JUMP____997416047:,
;{20};rand;int;show;eqto(10);jt(____CODE_JUMP____803885359)
 
{"--"};{20};rand;int;{"\n"}print;
,jmp(____CODE_JUMP____997416047),____CODE_JUMP____803885359:,
{"\nEnd of Loop\n"};print;
emptystack?do{{0}};return
</syntaxhighlight>
{{out}}
<pre>
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
11--19
4--4
1--9
0--13
19--18
12--6
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
19--10
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
0--14
7--1
18--11
15--15
17--9
7--1
10
End of Loop
xxxx@debian:~/Proy$ hopper3 jm/rand.jambo
13--0
17--12
16--2
19--14
2--6
19--10
10
End of Loop
xxxx@debian:~/Proy$
</pre>
 
Line 733 ⟶ 797:
print
end</syntaxhighlight>
 
==={{header|bootBASIC}}===
In bootBASIC, the rnd statement returns an unsigned integer between 0 and 255. 255 divided by 19 gives us 13 without the fraction part, so 13 is the number to divide the random number by to get a range of 0 to 19. All division is integer division.
<syntaxhighlight lang="BASIC">
10 a=rnd/13
20 print a ;
30 if a-10 goto 50
40 goto 100
50 a=rnd/13
55 print ", ";
60 print a
70 goto 10
100 print
</syntaxhighlight>
{{out}}
<pre>
13, 19
11, 14
18, 4
17, 0
12, 15
0, 13
7, 19
2, 7
1, 3
6, 18
13, 6
9, 10
4, 7
15, 7
10
</pre>
 
==={{header|Commodore BASIC}}===
Line 781 ⟶ 877:
 
 
==={{header|uBasic/4tH}}===
In uBasic/4tH '''UNTIL''' ''<cond>'' is equivalent to '''IF''' ''<cond>'' '''THEN BREAK'''. You can add as many '''UNTIL''' and '''WHILE''' as required in '''FOR..NEXT''' or '''DO..LOOP''' loops.
<syntaxhighlight lang="qbasic">Do
n = Rnd(20)
Print n
Until n=10
Print Rnd(20)
Loop</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
On the ZX Spectrum, for loops must be terminated through the NEXT statement, otherwise a memory leak will occur. To terminate a loop prematurely, set the loop counter to the last iterative value and jump to the NEXT statement:
Line 1,243 ⟶ 1,347:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="text">repeat
repeat
a = random 20
a = randint 20
print a
until print a = 10
until a = 10
print random 20
print randint 20
.</syntaxhighlight>
.
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,338 ⟶ 1,444:
 
(wait_10)</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
for ever
int a = random(20)
write(a)
if a == 10 do break end
writeLine("," + random(20))
end
writeLine()
</syntaxhighlight>
{{out}}
<pre>
19,14
10
</pre>
 
=={{header|Erlang}}==
Line 1,825 ⟶ 1,947:
* Programmers new to Icon/Unicon need to understand that just about everything returns values including comparison operators, I/O functions like write/writes.
* This program will perform similarly but not identically under Icon and Unicon because the random operator ?i behaves differently. While both produce pseudo-random numbers a different generator is used. Also, the sequence produced by Icon begins with the same seed value and is repeatable whereas the sequence produced by Unicon does not. One way to force Icon to use different random sequences on each call would be to add the line <syntaxhighlight lang="icon">&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))</syntaxhighlight> at the start of the <tt>main</tt> procedure to set the random number seed based on the time of day.
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(while true
(print (let x (rand-int 0 20)))
(when (= x 10) (break)))
</syntaxhighlight>
 
=={{header|Io}}==
Line 2,113 ⟶ 2,243:
println(Random.nextInt(20))
}
}</syntaxhighlight>
 
A more compact version:
 
<syntaxhighlight lang="kotlin">fun main() {
while ((0..19).random().also { println(it) } != 10)
println((0..19).random())
}</syntaxhighlight>
 
Line 2,153 ⟶ 2,290:
write .i, " "
if .i == 10 { writeln(); break }
write random(0..19), " "
}</syntaxhighlight>
 
Line 2,527 ⟶ 2,663:
40 PRINT RND(20)
50 GOTO 10</syntaxhighlight>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
while true {
let a = random int 0..19
print $a
if $a == 10 {break}
print (random int 0..19)
}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 3,109 ⟶ 3,255:
end
</syntaxhighlight>
 
=={{header|RPL}}==
RPL does not have any <code>BREAK</code> command. Flags are of great help to exit loops:
≪ 1 CF
'''WHILE''' 1 FC? '''REPEAT'''
RAND 20 * IP
DUP 1 DISP
'''IF''' 10 == '''THEN''' 1 SF '''ELSE'''
RAND 20 * IP
2 DISP '''END'''
'''END'''
The error handling mechanism provides another way to break a loop:
≪ '''IFERR'''
'''WHILE''' 1 '''REPEAT'''
RAND 20 * IP DUP 1 DISP
'''IF''' 10 == '''THEN''' 0 DUP / '''END'''
RAND 20 * IP 2 DISP
'''END'''
'''THEN''' DROP2 '''END'''
 
=={{header|Ruby}}==
Line 3,741 ⟶ 3,908:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var r = Random.new()
885

edits