Loops/Do-while: Difference between revisions
Content deleted Content added
m Moved FutureBasic entry out of the BASIC group |
|||
(19 intermediate revisions by 13 users not shown) | |||
Line 245:
</pre>
===
{{works with|A60}}
While a judicious "goto" may
<syntaxhighlight lang="algol60">
begin
Line 255:
i := 0;
another := true;
for i := i
begin
i := i + 1;
outinteger(1,i);
comment -
if i = (i div 6) * 6 then another := false;
end;
Line 639 ⟶ 640:
<pre>
1 2 3 4 5 6
</pre>
Line 755 ⟶ 735:
60 END
</syntaxhighlight>
==={{header|MSX Basic}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
==={{header|NS-HUBASIC}}===
Line 806 ⟶ 789:
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 820 ⟶ 806:
30 PRINT X
40 IF X/6<>INT (X/6) THEN GOTO 20</syntaxhighlight>
==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
repeat
v++
print v
until(v mod 6 == 0)
</syntaxhighlight>
==={{Header|Tiny BASIC}}===
Line 929 ⟶ 923:
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 1,050 ⟶ 1,053:
{{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 1,158 ⟶ 1,171:
=={{header|EasyLang}}==
<syntaxhighlight lang=text>
value = 0
repeat
value += 1
print value
until
.
</syntaxhighlight>
Line 1,407 ⟶ 1,419:
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>
{{out}}
<pre>
1
2
3
4
5
6
</pre>
=={{header|GAP}}==
Line 1,972 ⟶ 2,005:
fn.println($i)
if(!
con.break
}
Line 2,064 ⟶ 2,097:
}
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>
Line 2,071 ⟶ 2,124:
1 2 3 4 5 6
1 2 3 4 5 6
1
2
3
4
5
6
1
2
Line 2,274 ⟶ 2,333:
inc val
echo val</syntaxhighlight>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
mut n = 0
while true {
$n += 1
print $n
if $n mod 6 == 0 {break}
}
</syntaxhighlight>
=={{header|Oberon-2}}==
Line 2,416 ⟶ 2,485:
until i mod 6 = 0
end.</syntaxhighlight>
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
begin
var a := 0;
repeat
a += 1;
Print(a);
until a mod 6 = 0;
end.
</syntaxhighlight>
=={{header|Perl}}==
Line 2,517 ⟶ 2,599:
} 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,757 ⟶ 2,864:
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
≫
Line 2,782 ⟶ 2,889:
|}
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 2,848 ⟶ 2,955:
end;
end;</syntaxhighlight>
=={{header|S-BASIC}}==
S-BASIC's REPEAT..UNTIL control structure is the equivalent of a
DO..WHILE statement with the terminating test inverted.
<syntaxhighlight lang="BASIC">
rem - return n mod m
function mod(n, m = integer) = integer
end = n - (n / m) * m
var i = integer
i = 0
repeat
begin
i = i + 1
print i;
end
until mod(i, 6) = 0
end
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 6
</pre>
=={{header|Scala}}==
Line 3,205 ⟶ 3,336:
endmodule
</syntaxhighlight>
=={{header|V (Vlang)}}==
Line 3,293 ⟶ 3,393:
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}}==
|