Loops/While: Difference between revisions
< Loops
Content added Content deleted
(Added Haskell example) |
(Added Ada) |
||
Line 1: | Line 1: | ||
{{task}}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. |
{{task}}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. |
||
=={{header|Ada}}== |
|||
<ada> |
|||
declare |
|||
I : Integer := 1024; |
|||
begin |
|||
while I > 0 loop |
|||
Put_Line(Integer'Image(I)); |
|||
I := I / 2; |
|||
end loop; |
|||
end; |
|||
</ada> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
Revision as of 14:15, 12 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; }
Haskell
Introducing and especially updating a loop variable requires side effects. So it's simpler to just create a list of the required values, and then loop over it to print them. However, that doesn't really reflect the structure of the imperative solutions.
mapM print $ takeWhile (>0) $ iterate (`div` 2) 1024
Java
<java>int i = 1024; while(i > 0){
System.out.println(i); i >>= 1; //also acceptable: i /= 2;
}</java>