Anonymous user
Loops/While: Difference between revisions
m
Fixed lang tags.
(→{{header|Ruby}}: added until) |
m (Fixed lang tags.) |
||
Line 2:
=={{header|ActionScript}}==
<lang actionscript>var i:int = 1024;
while (i > 0) {
trace(i);
i /= 2;
}</lang>
=={{header|Ada}}==
<lang ada>declare
I : Integer := 1024;
begin
Line 19 ⟶ 16:
I := I / 2;
end loop;
end;</lang>
=={{header|ALGOL 68}}==
Line 26 ⟶ 22:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<lang algol68>INT i := 1024;
WHILE i > 0 DO
print((i));
i := i OVER 2
OD</lang>
Output:
<lang algol68>+1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1</lang>
=={{header|AmigaE}}==
Line 48 ⟶ 40:
=={{header|AutoHotkey}}==
<lang AutoHotkey>i = 1024
While (i > 0)
{
Line 55 ⟶ 46:
i := Floor(i / 2)
}
MsgBox % output</lang>
=={{header|AWK}}==
Line 76 ⟶ 66:
=={{header|Befunge}}==
Line 99 ⟶ 89:
}</lang>
Alternatively, it can be done with <code>for</code>:
<lang cpp>for (int i = 1024; i>0; i /= 2)
std::cout << i << std::endl;</lang>
Indeed, in C++,
<lang cpp>for (init; cond; update)
statement;</lang>
is equivalent to
<lang cpp>{
init;
while (cond)
Line 117 ⟶ 102:
update;
}
}</lang>
=={{header|C sharp|C#}}==
<lang
while(i > 0){
System.Console.WriteLine(i);
Line 131 ⟶ 115:
With tags:
With script:
=={{header|Common Lisp}}==
Line 171 ⟶ 155:
=={{header|FALSE}}==
=={{header|Forth}}==
<lang forth>: halving ( n -- )
begin dup 0 >
while
repeat drop ;
1024 halving</lang>
=={{header|Factor}}==
<lang
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>INTEGER :: i = 1024
WRITE(*,*) i
i
END DO</lang>
=={{header|Haskell}}==
Line 224 ⟶ 204:
=={{header|Icon}}==
<lang icon>procedure main()
local i
i
while write(0 < (i :=
end</lang>
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
<lang j>,. <.@-:^:*^:a: 1024</lang>
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).
<lang j>3 : 0 ] 1024
)</lang>
Though it's rare to see J code like this.
Line 274 ⟶ 248:
=={{header|Joy}}==
<lang joy>DEFINE putln == put '\n putch.
1024 [0 >] [dup putln 2 /] while.</lang>
=={{header|Lisaac}}==
<lang Lisaac>+ i : INTEGER;
i := 1024;
{ i > 0 }.while_do {
Line 288 ⟶ 259:
'\n'.print;
i := i / 2;
};</lang>
=={{header|Logo}}==
=={{header|Mathematica}}==
Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function:
<lang Mathematica>i = 1024;
While[i > 0,
i =
]</lang>
=={{header|MAXScript}}==
<lang maxscript>a = 1024
while a > 0 do
(
print a
a /= 2
)</lang>
=={{header|Make}}==
Invoking it
=={{header|Metafont}}==
Line 355 ⟶ 321:
=={{header|MOO}}==
<lang moo>i = 1024;
while (i > 0)
player:tell(i);
Line 400 ⟶ 365:
=={{header|Pascal}}==
<lang pascal>program divby2(output);
var
Line 413 ⟶ 377:
i := i div 2
end
end.</lang>
=={{header|Perl}}==
Line 437 ⟶ 400:
while $n = $n div 2 {
say $n;
}</lang>
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>.
Line 455 ⟶ 417:
=={{header|Pop11}}==
<lang pop11>lvars i = 1024;
while i > 0 do
printf(i, '%p\n');
i div 2 -> i;
endwhile;</lang>
=={{header|PowerShell}}==
Line 487 ⟶ 447:
=={{header|R}}==
<lang R>i <- 1024L
while(i > 0)
{
print(i)
i <- i %/% 2
}</lang>
=={{header|Ruby}}==
Line 515 ⟶ 473:
=={{header|Scheme}}==
<lang scheme>(do ((n 1024 (quotient n 2)))
((<= n 0))
(display n)
Line 522 ⟶ 479:
=={{header|Slate}}==
<lang slate>[| n | n: 1024.
[n isPositive] whileTrue:
[inform: number printString.
n: n // 2]] do</lang>
=={{header|Smalltalk}}==
Line 562 ⟶ 517:
=={{header|TI-89 BASIC}}==
<
1024 → i
While i > 0
Disp i
intDiv(i, 2) → i
EndWhile</
=={{header|UNIX Shell}}==
Line 578 ⟶ 533:
=={{header|UnixPipes}}==
<lang
=={{header|Ursala}}==
Line 598 ⟶ 553:
whole argument. The main program takes care of list reversal and
formatting.
<lang Ursala>#import nat
g = ~&h-> ^C/half@h ~&
Line 605 ⟶ 559:
#show+
main = %nP*=tx g <1024></lang>
output:
<pre>
Line 624 ⟶ 577:
The same output is produced by the following main program
using bit manipulation.
<lang Ursala>main = %nP*=tK33 1024</lang>
=={{header|V}}==
<lang v>
=={{header|Vedit macro language}}==
Line 639 ⟶ 590:
Num_Type(#1)
#1 /= 2
}</lang>
or with for loop:
<lang vedit>for (#1 = 1024; #1 > 0; #1 /= 2) {
|