Jump to content

Loops/While: Difference between revisions

m
code to lang, but left something untouched because of doubts
m (code to lang, but left something untouched because of doubts)
Line 2:
 
=={{header|ActionScript}}==
<lang actionscript>
var i:int = 1024;
while (i > 0) {
Line 8:
i /= 2;
}
</lang>
</actionscript>
 
=={{header|Ada}}==
<lang ada>
declare
I : Integer := 1024;
Line 20:
end loop;
end;
</adalang>
 
=={{header|ALGOL 68}}==
Line 39:
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
<lang qbasic>i = 1024
while i > 0
print i
i = i / 2
wend</qbasiclang>
 
=={{header|Befunge}}==
Line 51:
 
=={{header|C}}==
<lang c>int i = 1024;
while(i > 0) {
printf("%d\n", i);
i /= 2;
}</clang>
 
=={{header|C sharp|C#}}==
<lang c>int i = 1024;
while(i > 0){
System.Console.WriteLine(i);
i /= 2;
}</lang c>
 
=={{header|ColdFusion}}==
Line 83:
 
=={{header|Common Lisp}}==
<lang lisp>(setq i 1024)
(loop while (> i 0) do
(print i)
(setq i (floor i 2)))</lang lisp>
 
=={{header|D}}==
<Dlang d>import std.stdio;
 
int i = 1024;
Line 97:
i >>= 1;
}
}</dlang>
 
=={{header|Forth}}==
<lang forth>
: halving ( n -- )
begin dup 0 >
Line 105 ⟶ 106:
repeat drop ;
1024 halving
</lang>
 
=={{header|Factor}}==
Line 111 ⟶ 113:
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<lang fortran>
INTEGER :: i = 1024
DO WHILE (i > 0)
Line 116 ⟶ 119:
i = i / 2
END DO
</lang>
 
=={{header|Haskell}}==
<lang haskell>
import Control.Monad
main = loop 1024
Line 123 ⟶ 128:
(do print n
loop (n `div` 2))
</lang>
 
 
=={{header|Icon}}==
<lang icon>
procedure main()
local i
Line 131 ⟶ 137:
while write(0 < (i := i / 2))
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
Line 148 ⟶ 158:
i. 0 0
)
</lang>
 
Though it's rare to see J code like this.
 
=={{header|Java}}==
<lang java>int i = 1024;
while(i > 0){
System.out.println(i);
i >>= 1; //also acceptable: i /= 2;
}</javalang>
 
=={{header|JavaScript}}==
<lang javascript>var n = 1024;
while (n>0) {
print(n);
n/=2;
}</javascriptlang>
 
=={{header|Logo}}==
Line 226 ⟶ 237:
 
=={{header|OCaml}}==
<lang ocaml>let n = ref 1024;;
while !n > 0 do
Printf.printf "%d\n" !n;
n := !n / 2
done;;</ocamllang>
 
But it is more common to write it in a tail-recursive functional style:
<lang ocaml>let rec loop n =
if n > 0 then begin
Printf.printf "%d\n" n;
loop (n / 2)
end
in loop 1024</ocamllang>
 
=={{header|Pascal}}==
<lang pascal>
program divby2(output);
 
Line 255 ⟶ 266:
end
end.
</pascallang>
 
=={{header|Perl}}==
<lang perl>$n = 1024;
while ($n > 0) {
print "$n\n";
$n >>= 1; # also acceptable: use integer; $n /= 2;
}</perllang>
 
=={{header|PHP}}==
<lang php>$i = 1024;
while ($i > 0) {
echo "$i\n";
$i >>= 1;
}</phplang>
 
=={{header|Pop11}}==
Line 281 ⟶ 292:
 
=={{header|Python}}==
<lang python>n = 1024
while n > 0:
print n
n //= 2</pythonlang>
 
=={{header|Ruby}}==
<lang ruby>i = 1024
while i > 0 do
puts i
i /= 2
end</rubylang>
 
=={{header|Scheme}}==
<lang scheme>
(do ((n 1024 (quotient n 2)))
((<= n 0))
(display n)
(newline))</schemelang>
 
=={{header|UnixPipes}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.