Loop/While
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
Contents |
[edit] Ada
declare I : Integer := 1024; begin while I > 0 loop Put_Line(Integer'Image(I)); I := I / 2; end loop; end;
[edit] BASIC
Works with: QuickBasic version 4.5
i = 1024 WHILE i > 0 PRINT i i = i / 2 WEND
[edit] Befunge
84*:*> :v
^/2.:_@
[edit] C
int i = 1024; while(i > 0) { printf("%d\n", i); i /= 2; }
[edit] 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>
[edit] D
import std.stdio; int i = 1024; void main() { while(i > 0) { writefln("%s", i); i >>= 1; } }
[edit] Forth
: halving ( n -- ) begin dup 0 > while cr dup . 2/ repeat drop ; 1024 halving
[edit] Factor
1024 [ dup 0 > ] [ dup . 2 /i ] [ drop ] while
[edit] Fortran
Works with: Fortran version 90 and later
INTEGER :: i = 1024 DO WHILE (i > 0) WRITE(*,*) i i = i / 2 END DO
[edit] Haskell
import Control.Monad
main = loop 1024
where loop n = when (n > 0)
(do print n
loop (n `div` 2))
[edit] J
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
,. <.@-:^:*^:a: 1024
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).
3 : 0 ] 1024
while. 0 < y do.
y 1!:2 ] 2
y =. <. -: y
end.
i. 0 0
)
Though it's rare to see J code like this.
[edit] Java
int i = 1024; while(i > 0){ System.out.println(i); i >>= 1; //also acceptable: i /= 2; }
[edit] JavaScript
var n = 1024; while (n>0) { print(n); n/=2; }
[edit] Logo
make "n 1024 while [:n > 0] [print :n make "n :n / 2]
[edit] MAXScript
a = 1024
while a > 0 do
(
print a
a /= 2
)
[edit] 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
[edit] Oberon-2
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;
[edit] OCaml
let n = ref 1024;; while !n > 0 do Printf.printf "%d\n" !n; n := !n / 2 done;;
But it is more common to write it in a tail-recursive functional style:
let rec loop n = if n > 0 then begin Printf.printf "%d\n" n; loop (n / 2) end in loop 1024
[edit] Pascal
program divby2(output); var i: integer; begin i := 1024; while i > 0 do begin writeln(i); i := i div 2 end end.
[edit] Perl
$n = 1024; while ($n > 0) { print "$n\n"; $n >>= 1; # also acceptable: use integer; $n /= 2; }
[edit] Pop11
lvars i = 1024;
while i > 0 do
printf(i, '%p\n');
i div 2 -> i;
endwhile;
[edit] Python
n = 1024 while n > 0: print n n = n / 2
[edit] Scheme
(do ((n 1024 (quotient n 2))) ((<= n 0)) (display n) (newline))
[edit] 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
[edit] V
1024 [0 >] [ dup puts 2 / >int ] while

