Loops/Do-while: Difference between revisions
Content deleted Content added
PascalABC.NET |
|||
(17 intermediate revisions by 10 users not shown) | |||
Line 755:
60 END
</syntaxhighlight>
==={{header|MSX Basic}}===
The [[#Minimal BASIC|Minimal BASIC]] solution works without any changes.
==={{header|NS-HUBASIC}}===
Line 806 ⟶ 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 929 ⟶ 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 1,050 ⟶ 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 1,158 ⟶ 1,183:
=={{header|EasyLang}}==
<syntaxhighlight>
value = 0
repeat
value += 1
print value
until
.
</syntaxhighlight>
Line 1,961 ⟶ 1,985:
{do_while 0}
-> 0 1 2 3 4 5 6 (end of loop)
</syntaxhighlight>
=={{header|Lang}}==
Lang does not have a do-while loop. A simple loop like the example below can be used.
<syntaxhighlight lang="lang">
$i = 0
loop {
$i += 1
fn.println($i)
if(!($i % 6)) {
con.break
}
}
</syntaxhighlight>
Line 2,016 ⟶ 2,055:
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++
}
print
x=0
{
Line 2,027 ⟶ 2,074:
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
}
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 2,244 ⟶ 2,324:
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,386 ⟶ 2,476:
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,487 ⟶ 2,590:
} 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,727 ⟶ 2,855:
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,752 ⟶ 2,880:
|}
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,175 ⟶ 3,303:
endmodule
</syntaxhighlight>
=={{header|V (Vlang)}}==
Line 3,263 ⟶ 3,360:
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}}==
|