Loops/Do-while: Difference between revisions

m
Moved Wren entry into correct alphabetical order.
(Added EasyLang implementation)
m (Moved Wren entry into correct alphabetical order.)
(21 intermediate revisions by 11 users not shown)
Line 516:
 
=={{header|BASIC}}==
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Just BASIC}}
<syntaxhighlight lang="qbasic">a = 0
do
a = a + 1
print a
loop while a mod 6 <> 0</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
{{works with|Commodore BASIC}}
Line 534 ⟶ 525:
20 PRINT V4LUE" ";
30 WHILE = FN MOD6(V4LUE) < > 0:DO = NOT WHILE: NEXT</syntaxhighlight>
 
==={{header|ASIC}}===
ASIC does not have a <code>do .. while</code> construct. Equivalent using <code>WHILE</code>:
Line 603 ⟶ 595:
UNTIL a MOD 6 = 0</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
In ''Chipmunk Basic Man Page'', the words <code>do</code>, <code>loop</code>, and <code>until</code> are mentioned as reserved, but the <code>do .. loop until</code> statement is not described, probably because of uncorrected abnormal behavior of the interpreter. In case of such behavior you may use equivalents (e.g. with <code>while .. wend</code>).
<syntaxhighlight lang="basic">
100 rem Loops/Do-while
110 i = 0
120 do
130 i = i+1
140 print i
150 loop until i mod 6 = 0
160 end
</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
 
==={{header|Commodore BASIC}}===
Line 612 ⟶ 624:
150 b=x/6<>int(x/6)
160 next x
</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05. 0 Win64
 
Dim i As Integer = 0
Do
i += 1
Print i; " ";
Loop While i Mod 6 <> 0
Print
Sleep</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1
 
dim as long i
 
do
i++
print i
until ( i mod 6 == 0 )
 
HandleEvents</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=57e91eab60baf7e39df9b6d16a0deddd Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short
 
Repeat
Inc siCount
Print siCount;;
Until siCount Mod 6 = 0
 
End</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
 
==={{header|GW-BASIC}}===
GW-BASIC does not have a <code>do .. while</code> construct.
Equivalent using <code>WHILE</code>:
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 ' first iteration - before the WHILE
30 LET I% = I% + 1
40 PRINT I%
50 WHILE I% MOD 6 <> 0
60 LET I% = I% + 1
70 PRINT I%
80 WEND
</syntaxhighlight>
Equivalent using <code>GOTO</code>:
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 LET I% = I% + 1
30 PRINT I%
40 IF I% MOD 6 <> 0 THEN GOTO 20
</syntaxhighlight>
 
Line 620 ⟶ 709:
130 PRINT I
140 LOOP UNTIL MOD(I,6)=0</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
a = 0
do
a = a + 1
print a
loop until (a mod 6) = 0
</syntaxhighlight>
See also [[#QBasic|QBasic]].
 
==={{header|Microsoft Small Basic}}===
Microsoft Small Basic does not have a <code>do .. while</code> construct.
Equivalent using <code>While</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
' first iteration - before the While
i = i + 1
TextWindow.WriteLine(i)
While Math.Remainder(i, 6) <> 0
i = i + 1
TextWindow.WriteLine(i)
EndWhile
</syntaxhighlight>
Equivalent using <code>Goto</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
loopStart:
i = i + 1
TextWindow.WriteLine(i)
If Math.Remainder(i, 6) <> 0 Then
Goto loopStart
EndIf
</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
Line 630 ⟶ 754:
50 IF INT(I/6)*6 <> I THEN 30
60 END
</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
 
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 PRINT "NO,"A" ISN'T A MULTIPLE OF 6."
20 A=A+1
30 IF A-(A/6)*6<>0 THEN GOTO 10
40 PRINT "YES, 6 IS A MULTIPLE OF 6."</syntaxhighlight>
 
==={{header|PureBasic}}===
{{works with|PureBasic|4.41}}
<syntaxhighlight lang="purebasic">x=0
Repeat
x+1
Debug x
Until x%6=0</syntaxhighlight>
 
==={{header|QB64}}===
''CBTJD'': 2020/03/14
<syntaxhighlight lang="qbasic">DO
PRINT n
n = n + 1
LOOP WHILE n MOD 6 <> 0</syntaxhighlight>
 
<syntaxhighlight lang="qb64">
'Another demo of DO loops
Dim As Integer Counter
Print "First loop DO..LOOP UNTIL"
Counter = 0
Do
Print Counter
Counter = Counter + 1
Loop Until Counter Mod 6 = 0
Print "Counter Mod 6 = "; Counter Mod 6
Print "First loop DO WHILE..LOOP"
Counter = 1
Do While Counter Mod 6 <> 0
Print Counter
Counter = Counter + 1
Loop
Print "Counter Mod 6 = "; Counter Mod 6
End
</syntaxhighlight>
 
Line 641 ⟶ 809:
PRINT a;
LOOP WHILE a MOD 6 <> 0</syntaxhighlight>
 
==={{header|Quite BASIC}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
 
==={{header|Run BASIC}}===
Line 655 ⟶ 826:
30 PRINT X
40 IF X/6<>INT (X/6) THEN GOTO 20</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
Tiny Basic does not have a <code>do .. while</code> construct. Equivalent using conditional jump:
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 REM Loops/Do-while
20 LET I = 0
30 LET I = I + 1
40 PRINT I
50 IF (I / 6) * 6 <> I THEN GOTO 30
60 END</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
 
==={{header|True BASIC}}===
Line 665 ⟶ 855:
PRINT
END</syntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">Public Sub LoopDoWhile()
Dim value As Integer
value = 0
Do
value = value + 1
Debug.Print value;
Loop While value Mod 6 <> 0
End Sub</syntaxhighlight>{{out}}<pre> 1 2 3 4 5 6 </pre>
 
==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0</syntaxhighlight>
 
==={{header|XBasic}}===
Line 682 ⟶ 889:
END PROGRAM
</syntaxhighlight>
 
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="tiny basic"> REM TinyBasic does not have a do .. while construct. Equivalent using conditional jump:
 
LET i = 0
10 LET i = i + 1
PRINT i
IF (i / 6) * 6 <> i THEN GOTO 10
END</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 737 ⟶ 935:
std::cout << val << std::endl;
}while(val % 6 != 0);</syntaxhighlight>
 
=={{header|C3}}==
In this example we use default zero initialization of locals in C3.
<syntaxhighlight lang="c3">int val;
do
{
io::printn(++val);
}
while (val % 6 != 0);</syntaxhighlight>
 
=={{header|Chapel}}==
Line 858 ⟶ 1,065:
{{out}}
<pre>1 2 3 4 5 6 </pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">void main() {
int val = 0;
do {
val++;
print(val);
} while (val % 6 != 0);
}
</syntaxhighlight>
 
=={{header|dc}}==
Line 972 ⟶ 1,189:
value += 1
print value
until not (value mod 6 <>= 0)
.
</syntaxhighlight>
Line 1,206 ⟶ 1,423:
5
6
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05. 0 Win64
 
Dim i As Integer = 0
Do
i += 1
Print i; " ";
Loop While i Mod 6 <> 0
Print
Sleep</syntaxhighlight>
 
{{out}}
<pre>
1 2 3 4 5 6
</pre>
 
Line 1,231 ⟶ 1,432:
println[n]
} while n mod 6 != 0</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">window 1
 
dim as long i
 
do
i++
print i
until ( i mod 6 == 0 )
 
HandleEvents</syntaxhighlight>
Output:
<pre>
1
2
3
4
5
6
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=57e91eab60baf7e39df9b6d16a0deddd Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short
 
Repeat
Inc siCount
Print siCount;;
Until siCount Mod 6 = 0
 
End</syntaxhighlight>
Output:
<pre>
1 2 3 4 5 6
</pre>
 
=={{header|GAP}}==
Line 1,375 ⟶ 1,539:
5
6</pre>
 
=={{header|GW-BASIC}}==
GW-BASIC does not have a <code>do .. while</code> construct.
Equivalent using <code>WHILE</code>:
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 ' first iteration - before the WHILE
30 LET I% = I% + 1
40 PRINT I%
50 WHILE I% MOD 6 <> 0
60 LET I% = I% + 1
70 PRINT I%
80 WEND
</syntaxhighlight>
Equivalent using <code>GOTO</code>:
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">
10 LET I% = 0
20 LET I% = I% + 1
30 PRINT I%
40 IF I% MOD 6 <> 0 THEN GOTO 20
</syntaxhighlight>
 
=={{header|Harbour}}==
Line 1,410 ⟶ 1,551:
 
=={{header|Haskell}}==
 
<syntaxhighlight lang="haskell">import Data.List
import Control.Monad
Line 1,486 ⟶ 1,626:
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
 
<syntaxhighlight lang=J> ,. ([^:(0=6|])>:)^:a: 0
0
1
2
3
4
5
</syntaxhighlight>
 
This could also be accomplished using [[j:Vocabulary/zcapco|Z:]] to provide early termination from a [[j:Vocabulary/fcap|fold]]:
 
<syntaxhighlight lang=J> 0#]F.(>: [ echo [ _2 Z: * * 0=6|]) 0
0
1
2
3
4
5
 
</syntaxhighlight>
 
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided).
Line 1,596 ⟶ 1,755:
 
====ES6====
 
A process or value of this kind might be better expressed (in functionally composed JavaScript) with an '''unfold''' or '''until''' function, returning a list.
 
Line 1,646 ⟶ 1,804:
})();
</syntaxhighlight>
 
 
<syntaxhighlight lang="javascript">[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]</syntaxhighlight>
Line 1,831 ⟶ 1,988:
</syntaxhighlight>
 
=={{header|Liberty BASICLang}}==
Lang does not have a do-while loop. A simple loop like the example below can be used.
<syntaxhighlight lang="lb">
<syntaxhighlight lang="lang">
a = 0
$i = 0
do
loop {
a =a +1
$i += 1
print a
loop until ( a mod 6) = 0
fn.println($i)
if(!($i % 6)) {
con.break
}
}
</syntaxhighlight>
 
See also [[#BASIC|BASIC]]
 
=={{header|Lingo}}==
Line 1,895 ⟶ 2,056:
x=0
\\ Do or Repeat
Do
x++
print x,
when x mod 6>0
print
// or we can use Until x mod 6 = 0
// and we can use block if we like it
x=0
Do {
x++
Printprint x,
} Untilwhen x mod 6=>0
print
x=0
{
Line 1,906 ⟶ 2,075:
if x mod 6<>0 Then loop ' set loop flag of current block to true
\\ when block end check Loop flag and if true execute block again
Printprint Xx,
}
print
}
Checkit
module Old_Style {
10 REM Loops/Do-while
20 LET I=0
30 LET I=I+1
40 PRINT I
50 IF INT(I/6)*6 <> I THEN 30
60 END
}
Old_Style
// modern style, using high order functions
module generic_iterator {
do_while = lambda (f, p)->{
{
if p(f()) then loop
}
}
funcA=lambda (start_from, do_what) -> {
=lambda i=start_from, do_what ->{
call do_what(i)
=i
i++
}
}
funcPrint=lambda ->{
print number
}
call do_while(funcA(1, funcPrint), lambda->number mod 6 <>0)
}
generic_iterator
</syntaxhighlight>
 
{{out}}
<pre>
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1
2
Line 1,975 ⟶ 2,177:
forever: show a; a := a + 1; exitif a mod 6 = 0; endfor
end</syntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
Microsoft Small Basic does not have a <code>do .. while</code> construct.
Equivalent using <code>While</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
' first iteration - before the While
i = i + 1
TextWindow.WriteLine(i)
While Math.Remainder(i, 6) <> 0
i = i + 1
TextWindow.WriteLine(i)
EndWhile
</syntaxhighlight>
Equivalent using <code>Goto</code>:
<syntaxhighlight lang="microsoftsmallbasic">
i = 0
loopStart:
i = i + 1
TextWindow.WriteLine(i)
If Math.Remainder(i, 6) <> 0 Then
Goto loopStart
EndIf
</syntaxhighlight>
 
=={{header|min}}==
Line 2,005 ⟶ 2,183:
 
=={{header|MIPS Assembly}}==
 
<syntaxhighlight lang="mips">
.text
Line 2,149 ⟶ 2,326:
echo val</syntaxhighlight>
 
=={{header|NS-HUBASICNu}}==
<syntaxhighlight lang="ns-hubasicnu">10 PRINT "NO,"A" ISN'T A MULTIPLE OF 6."
mut n = 0
20 A=A+1
while true {
30 IF A-(A/6)*6<>0 THEN GOTO 10
$n += 1
40 PRINT "YES, 6 IS A MULTIPLE OF 6."</syntaxhighlight>
print $n
if $n mod 6 == 0 {break}
}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 2,397 ⟶ 2,578:
} while (value % 6);
}</syntaxhighlight>
 
=={{header|PL/0}}==
PL/0 does not have a <code>do .. while</code> construct. Equivalent using <code>while</code>:
<syntaxhighlight lang="pascal">
var i;
begin
i := 0;
i := i + 1;
! i;
while (i / 6) * 6 <> i do
begin
i := i + 1;
! i
end;
end.
</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
 
=={{header|PL/I}}==
Line 2,475 ⟶ 2,681:
 
</syntaxhighlight>
 
=={{header|PureBasic}}==
{{works with|PureBasic|4.41}}
<syntaxhighlight lang="purebasic">x=0
Repeat
x+1
Debug x
Until x%6=0</syntaxhighlight>
 
=={{header|Python}}==
Line 2,497 ⟶ 2,695:
val += 1
print val</syntaxhighlight>
 
=={{header|QB64}}==
''CBTJD'': 2020/03/14
<syntaxhighlight lang="qbasic">DO
PRINT n
n = n + 1
LOOP WHILE n MOD 6 <> 0</syntaxhighlight>
 
<syntaxhighlight lang="qb64">
'Another demo of DO loops
Dim As Integer Counter
Print "First loop DO..LOOP UNTIL"
Counter = 0
Do
Print Counter
Counter = Counter + 1
Loop Until Counter Mod 6 = 0
Print "Counter Mod 6 = "; Counter Mod 6
Print "First loop DO WHILE..LOOP"
Counter = 1
Do While Counter Mod 6 <> 0
Print Counter
Counter = Counter + 1
Loop
Print "Counter Mod 6 = "; Counter Mod 6
End
</syntaxhighlight>
 
=={{header|Quackery}}==
Line 2,540 ⟶ 2,711:
 
=={{header|Racket}}==
 
Idiomatic Racket code is functional:
<syntaxhighlight lang="racket">
Line 2,669 ⟶ 2,839:
end
</syntaxhighlight>
 
=={{header|RPL}}==
To ensure at least one loop, <code>DO</code>..<code>UNTIL</code>..<code>END</code> must be used rather than <code>WHILE</code>..<code>REPEAT</code>..<code>END</code>. To actually print (on paper) instead of pushing in the stack successive results, the <code>DUP</code> instruction inside the loop shall be replaced by <code>PR1</code>
≪ 0
'''DO'''
1 + DUP
'''UNTIL''' DUP 6 MOD 0 == '''END'''
DROP
 
=={{header|Ruby}}==
Line 2,689 ⟶ 2,868:
|}
 
During November 2005, Yukihiro Matsumoto, the creator of Ruby, [https://web.archive.org/web/20220322235407/http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741 regretted this loop feature] and [https://web.archive.org/web/20220322235418/http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6745 suggested using Kernel#loop].
 
{| class="wikitable"
Line 3,078 ⟶ 3,257:
while ( a % 6 != 0);
</syntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Public Sub LoopDoWhile()
Dim value As Integer
value = 0
Do
value = value + 1
Debug.Print value;
Loop While value Mod 6 <> 0
End Sub</syntaxhighlight>{{out}}<pre> 1 2 3 4 5 6 </pre>
 
=={{header|Vedit macro language}}==
Line 3,104 ⟶ 3,273:
@SAY n;
};</syntaxhighlight>
 
 
=={{header|Verilog}}==
Line 3,123 ⟶ 3,291:
endmodule
</syntaxhighlight>
 
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vbnet">Dim i = 0
Do
i += 1
Console.WriteLine(i)
Loop Until i Mod 6 = 0</syntaxhighlight>
 
=={{header|Wren}}==
Wren doesn't have a ''do/while'' loop as such but we can simulate it using an infinite loop with a final conditional break.
<syntaxhighlight lang="ecmascript">var v = 0
while (true) {
v = v + 1
System.print(v)
if (v%6 == 0) break
}</syntaxhighlight>
 
{{out}}
<pre>
1
2
3
4
5
6
</pre>
It can also be simulated ''without'' using a break statement as follows:
<syntaxhighlight lang="ecmascript">var value = 0
var ok = true
while (ok) {
value = value + 1
System.print(value)
ok = value%6 != 0
}</syntaxhighlight>
 
{{out}}
<pre>
Same as before.
</pre>
 
=={{header|V (Vlang)}}==
Line 3,220 ⟶ 3,348:
println(n3) // prt 8
}</syntaxhighlight>
 
=={{header|Wren}}==
Wren doesn't have a ''do/while'' loop as such but we can simulate it using an infinite loop with a final conditional break.
<syntaxhighlight lang="wren">var v = 0
while (true) {
v = v + 1
System.print(v)
if (v%6 == 0) break
}</syntaxhighlight>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
It can also be simulated ''without'' using a break statement as follows:
<syntaxhighlight lang="wren">var value = 0
var ok = true
while (ok) {
value = value + 1
System.print(value)
ok = value%6 != 0
}</syntaxhighlight>
 
{{out}}
<pre>
Same as before.
</pre>
 
=={{header|X86 Assembly}}==
Line 3,298 ⟶ 3,457:
 
{{omit from|GUISS}}
{{omit from|Commodore BASIC}}
9,485

edits