Loops/While: Difference between revisions
m (code to lang, but left something untouched because of doubts) |
|||
Line 2: | Line 2: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<actionscript> |
<lang actionscript> |
||
var i:int = 1024; |
var i:int = 1024; |
||
while (i > 0) { |
while (i > 0) { |
||
Line 8: | Line 8: | ||
i /= 2; |
i /= 2; |
||
} |
} |
||
</lang> |
|||
</actionscript> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<ada> |
<lang ada> |
||
declare |
declare |
||
I : Integer := 1024; |
I : Integer := 1024; |
||
Line 20: | Line 20: | ||
end loop; |
end loop; |
||
end; |
end; |
||
</ |
</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 39: | Line 39: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
<qbasic>i = 1024 |
<lang qbasic>i = 1024 |
||
while i > 0 |
while i > 0 |
||
print i |
print i |
||
i = i / 2 |
i = i / 2 |
||
wend</ |
wend</lang> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
Line 51: | Line 51: | ||
=={{header|C}}== |
=={{header|C}}== |
||
<c>int i = 1024; |
<lang c>int i = 1024; |
||
while(i > 0) { |
while(i > 0) { |
||
printf("%d\n", i); |
printf("%d\n", i); |
||
i /= 2; |
i /= 2; |
||
}</ |
}</lang> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<c>int i = 1024; |
<lang c>int i = 1024; |
||
while(i > 0){ |
while(i > 0){ |
||
System.Console.WriteLine(i); |
System.Console.WriteLine(i); |
||
i /= 2; |
i /= 2; |
||
}</c> |
}</lang c> |
||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
Line 83: | Line 83: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lisp>(setq i 1024) |
<lang lisp>(setq i 1024) |
||
(loop while (> i 0) do |
(loop while (> i 0) do |
||
(print i) |
(print i) |
||
(setq i (floor i 2)))</lisp> |
(setq i (floor i 2)))</lang lisp> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<lang d>import std.stdio; |
||
int i = 1024; |
int i = 1024; |
||
Line 97: | Line 97: | ||
i >>= 1; |
i >>= 1; |
||
} |
} |
||
}</ |
}</lang> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth> |
|||
: halving ( n -- ) |
: halving ( n -- ) |
||
begin dup 0 > |
begin dup 0 > |
||
Line 105: | Line 106: | ||
repeat drop ; |
repeat drop ; |
||
1024 halving |
1024 halving |
||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 111: | Line 113: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
<lang fortran> |
|||
INTEGER :: i = 1024 |
INTEGER :: i = 1024 |
||
DO WHILE (i > 0) |
DO WHILE (i > 0) |
||
Line 116: | Line 119: | ||
i = i / 2 |
i = i / 2 |
||
END DO |
END DO |
||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<lang haskell> |
|||
import Control.Monad |
import Control.Monad |
||
main = loop 1024 |
main = loop 1024 |
||
Line 123: | Line 128: | ||
(do print n |
(do print n |
||
loop (n `div` 2)) |
loop (n `div` 2)) |
||
</lang> |
|||
=={{header|Icon}}== |
=={{header|Icon}}== |
||
<lang icon> |
|||
procedure main() |
procedure main() |
||
local i |
local i |
||
Line 131: | Line 137: | ||
while write(0 < (i := i / 2)) |
while write(0 < (i := i / 2)) |
||
end |
end |
||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
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 |
,. <.@-:^:*^: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). |
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 |
3 : 0 ] 1024 |
||
Line 148: | Line 158: | ||
i. 0 0 |
i. 0 0 |
||
) |
) |
||
</lang> |
|||
Though it's rare to see J code like this. |
Though it's rare to see J code like this. |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<java>int i = 1024; |
<lang java>int i = 1024; |
||
while(i > 0){ |
while(i > 0){ |
||
System.out.println(i); |
System.out.println(i); |
||
i >>= 1; //also acceptable: i /= 2; |
i >>= 1; //also acceptable: i /= 2; |
||
}</ |
}</lang> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<javascript>var n = 1024; |
<lang javascript>var n = 1024; |
||
while (n>0) { |
while (n>0) { |
||
print(n); |
print(n); |
||
n/=2; |
n/=2; |
||
}</ |
}</lang> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 226: | Line 237: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>let n = ref 1024;; |
<lang ocaml>let n = ref 1024;; |
||
while !n > 0 do |
while !n > 0 do |
||
Printf.printf "%d\n" !n; |
Printf.printf "%d\n" !n; |
||
n := !n / 2 |
n := !n / 2 |
||
done;;</ |
done;;</lang> |
||
But it is more common to write it in a tail-recursive functional style: |
But it is more common to write it in a tail-recursive functional style: |
||
<ocaml>let rec loop n = |
<lang ocaml>let rec loop n = |
||
if n > 0 then begin |
if n > 0 then begin |
||
Printf.printf "%d\n" n; |
Printf.printf "%d\n" n; |
||
loop (n / 2) |
loop (n / 2) |
||
end |
end |
||
in loop 1024</ |
in loop 1024</lang> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<pascal> |
<lang pascal> |
||
program divby2(output); |
program divby2(output); |
||
Line 255: | Line 266: | ||
end |
end |
||
end. |
end. |
||
</ |
</lang> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>$n = 1024; |
<lang perl>$n = 1024; |
||
while ($n > 0) { |
while ($n > 0) { |
||
print "$n\n"; |
print "$n\n"; |
||
$n >>= 1; # also acceptable: use integer; $n /= 2; |
$n >>= 1; # also acceptable: use integer; $n /= 2; |
||
}</ |
}</lang> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<php>$i = 1024; |
<lang php>$i = 1024; |
||
while ($i > 0) { |
while ($i > 0) { |
||
echo "$i\n"; |
echo "$i\n"; |
||
$i >>= 1; |
$i >>= 1; |
||
}</ |
}</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 281: | Line 292: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<python>n = 1024 |
<lang python>n = 1024 |
||
while n > 0: |
while n > 0: |
||
print n |
print n |
||
n //= 2</ |
n //= 2</lang> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>i = 1024 |
<lang ruby>i = 1024 |
||
while i > 0 do |
while i > 0 do |
||
puts i |
puts i |
||
i /= 2 |
i /= 2 |
||
end</ |
end</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<scheme> |
<lang scheme> |
||
(do ((n 1024 (quotient n 2))) |
(do ((n 1024 (quotient n 2))) |
||
((<= n 0)) |
((<= n 0)) |
||
(display n) |
(display n) |
||
(newline))</ |
(newline))</lang> |
||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
Revision as of 16:48, 31 January 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Start an integer value at 1024. Loop while it is greater than 0. Print the value (with a newline) and divide it by two each time through the loop.
ActionScript
<lang actionscript> var i:int = 1024; while (i > 0) {
trace(i); i /= 2;
} </lang>
Ada
<lang ada> declare
I : Integer := 1024;
begin
while I > 0 loop Put_Line(Integer'Image(I)); I := I / 2; end loop;
end; </lang>
ALGOL 68
INT i := 1024; WHILE i > 0 DO print((i)); i := i OVER 2 OD
Output:
+1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1
BASIC
<lang qbasic>i = 1024 while i > 0
print i i = i / 2
wend</lang>
Befunge
84*:*> :v ^/2.:_@
C
<lang c>int i = 1024; while(i > 0) {
printf("%d\n", i); i /= 2;
}</lang>
C#
<lang c>int i = 1024; while(i > 0){
System.Console.WriteLine(i); i /= 2;
}</lang c>
ColdFusion
Remove the leading space from the line break tag.
With tags:
<cfset i = 1024 /> <cfloop condition="i GT 0"> #i#< br /> <cfset i /= 2 /> </cfloop>
With script:
<cfscript> i = 1024; while( i > 0 ) { writeOutput( i + "< br/ >" ); } </cfscript>
Common Lisp
<lang lisp>(setq i 1024) (loop while (> i 0) do
(print i) (setq i (floor i 2)))</lang lisp>
D
<lang d>import std.stdio;
int i = 1024; void main() {
while(i > 0) { writefln("%s", i); i >>= 1; }
}</lang>
Forth
<lang forth>
: halving ( n -- ) begin dup 0 > while cr dup . 2/ repeat drop ; 1024 halving
</lang>
Factor
1024 [ dup 0 > ] [ dup . 2 /i ] [ drop ] while
Fortran
<lang fortran>
INTEGER :: i = 1024 DO WHILE (i > 0) WRITE(*,*) i i = i / 2 END DO
</lang>
Haskell
<lang haskell>
import Control.Monad main = loop 1024 where loop n = when (n > 0) (do print n loop (n `div` 2))
</lang>
Icon
<lang icon>
procedure main() local i i := 1024 while write(0 < (i := i / 2)) end
</lang>
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 while. 0 < y do. y 1!:2 ] 2 y =. <. -: y end. i. 0 0 )
</lang>
Though it's rare to see J code like this.
Java
<lang java>int i = 1024; while(i > 0){
System.out.println(i); i >>= 1; //also acceptable: i /= 2;
}</lang>
JavaScript
<lang javascript>var n = 1024; while (n>0) {
print(n); n/=2;
}</lang>
Logo
make "n 1024 while [:n > 0] [print :n make "n :n / 2]
MAXScript
a = 1024 while a > 0 do ( print a a /= 2 )
Make
NEXT=`expr $* / 2` MAX=10 all: $(MAX)-n; 0-n:; %-n: %-echo @-make -f while.mk $(NEXT)-n MAX=$(MAX) %-echo: @echo $*
Invoking it
|make -f while.mk MAX=1024
Modula-3
The usual module code and imports are omitted.
PROCEDURE DivBy2() = VAR i: INTEGER := 1024; BEGIN WHILE i > 0 DO IO.PutInt(i); IO.Put("\n"); i := i DIV 2; END; END DivBy2;
Oberon-2
The usual module code and imports are ommited.
PROCEDURE DivBy2*(); VAR i: INTEGER; BEGIN i := 1024; WHILE i > 0 DO Out.Int(i,0); Out.Ln; i := i DIV 2; END; END DivBy2;
OCaml
<lang ocaml>let n = ref 1024;; while !n > 0 do
Printf.printf "%d\n" !n; n := !n / 2
done;;</lang>
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</lang>
Pascal
<lang pascal> program divby2(output);
var
i: integer;
begin
i := 1024; while i > 0 do begin writeln(i); i := i div 2 end
end. </lang>
Perl
<lang perl>$n = 1024; while ($n > 0) {
print "$n\n"; $n >>= 1; # also acceptable: use integer; $n /= 2;
}</lang>
PHP
<lang php>$i = 1024; while ($i > 0) {
echo "$i\n"; $i >>= 1;
}</lang>
Pop11
lvars i = 1024; while i > 0 do printf(i, '%p\n'); i div 2 -> i; endwhile;
Python
<lang python>n = 1024 while n > 0:
print n n //= 2</lang>
Ruby
<lang ruby>i = 1024 while i > 0 do
puts i i /= 2
end</lang>
Scheme
<lang scheme> (do ((n 1024 (quotient n 2)))
((<= n 0)) (display n) (newline))</lang>
UnixPipes
(echo 1024>p.res;tail -f p.res) | while read a ; do test $a -gt 0 && (expr $a / 2 >> p.res ; echo $a) || exit 0 done
V
1024 [0 >] [ dup puts 2 / >int ] while
Visual Basic .NET
Dim x = 1024 Do Console.WriteLine(x) x = x \ 2 Loop While x > 0