Loops/While: Difference between revisions
(added J) |
|||
Line 75: | Line 75: | ||
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: |
||
,. <.@-:^:*^: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). |
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 : |
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. |
Though it's rare to see J code like this. |
Revision as of 19:02, 2 August 2008
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.
Ada
<ada> declare
I : Integer := 1024;
begin
while I > 0 loop Put_Line(Integer'Image(I)); I := I / 2; end loop;
end; </ada>
BASIC
<qbasic>i = 1024 while i > 0
print i i = i / 2
wend</qbasic>
C
<c>int i = 1024; while(i > 0) {
printf("%d\n", i); i /= 2;
}</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 GT 0 ) { writeOutput( i + "< br/ >" ); } </cfscript>
D
<D>import std.stdio;
int i = 1024; void main() {
while(i > 0) { writefln("%s", i); i >>= 1; }
}</d>
Forth
: halving ( n -- ) begin dup 0 > while cr dup . 2/ repeat drop ; 1024 halving
Fortran
INTEGER :: i = 1024 DO WHILE (i > 0) WRITE(*,*) i i = i / 2 END DO
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.
Java
<java>int i = 1024; while(i > 0){
System.out.println(i); i >>= 1; //also acceptable: i /= 2;
}</java>
JavaScript
<javascript>var n = 1024; while (n>0) {
print(n); n/=2;
}</javascript>
Logo
make "n 1024 while [:n > 0] [print :n make "n :n / 2]
MAXScript
a = 1024 while a > 0 do ( print a a /= 2 )
OCaml
<ocaml>let n = ref 1024;; while !n > 0 do
Printf.printf "%d\n" !n; n := !n / 2
done;;</ocaml>
Pascal
<pascal> program divby2(output);
var
i: integer;
begin
i := 1024; while i > 0 do begin writeln(i); i := i div 2 end
end. </pascal>
Perl
<perl>$n = 1024; while ($n > 0) {
print "$n\n"; $n >>= 1; # also acceptable: use integer; $n /= 2;
}</perl>
Pop11
lvars i = 1024; while i > 0 do printf(i, '%p\n'); i div 2 -> i; endwhile;
Python
<python>n = 1024 while n > 0:
print n n = n / 2</python>
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