Loops/While: Difference between revisions
< Loops
Content added Content deleted
m (Added to iteration category) |
(added perl and python) |
||
Line 52: | Line 52: | ||
make "n 1024 |
make "n 1024 |
||
while [:n > 0] [print :n make "n :n / 2] |
while [:n > 0] [print :n make "n :n / 2] |
||
=={{header|Perl}}== |
|||
<perl>$n = 1024; |
|||
while ($n > 0) { |
|||
print "$n\n"; |
|||
$n >>= 1; # also acceptable: use integer; $n /= 2; |
|||
}</perl> |
|||
=={{header|Python}}== |
|||
<python>n = 1024 |
|||
while n > 0: |
|||
print n |
|||
n = n / 2</python> |
Revision as of 09:42, 15 April 2008
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Start a 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
int i = 1024; while(i > 0) { printf("%d\n", i); i /= 2; }
Forth
: halving ( n -- ) begin dup 0 > while cr dup . 2/ repeat drop ; 1024 halving
Java
<java>int i = 1024; while(i > 0){
System.out.println(i); i >>= 1; //also acceptable: i /= 2;
}</java>
JavaScript
var n = 1024; while (n>0) { print(n); n/=2; }
Logo
make "n 1024 while [:n > 0] [print :n make "n :n / 2]
Perl
<perl>$n = 1024; while ($n > 0) {
print "$n\n"; $n >>= 1; # also acceptable: use integer; $n /= 2;
}</perl>
Python
<python>n = 1024 while n > 0:
print n n = n / 2</python>