Loops/While: Difference between revisions

From Rosetta Code
Content added Content deleted
m (added output)
 
(32 intermediate revisions by 21 users not shown)
Line 10: Line 10:
Print the value (with a newline) and divide it by two each time through the loop.
Print the value (with a newline) and divide it by two each time through the loop.



;Related tasks:
;Related tasks:
Line 31: Line 30:


=={{header|0815}}==
=={{header|0815}}==
<lang 0815><:400:~}:_:%<:a:~$=<:2:=/^:_:</lang>
<syntaxhighlight lang="0815"><:400:~}:_:%<:a:~$=<:2:=/^:_:</syntaxhighlight>


=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}


<lang 11l>V n = 1024
<syntaxhighlight lang="11l">V n = 1024
L n > 0
L n > 0
print(n)
print(n)
n I/= 2</lang>
n I/= 2</syntaxhighlight>


=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
;Basic
;Basic
Using binary arithmetic. Convert results to EBCDIC printable output.
Using binary arithmetic. Convert results to EBCDIC printable output.
<lang 360asm>* While 27/06/2016
<syntaxhighlight lang="360asm">* While 27/06/2016
WHILELOO CSECT program's control section
WHILELOO CSECT program's control section
USING WHILELOO,12 set base register
USING WHILELOO,12 set base register
Line 62: Line 61:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
WTOTXT DC CL4' ' wto text
END WHILELOO</lang>
END WHILELOO</syntaxhighlight>
{{out}} (+ sign indicates "problem state" (non system key) issued WTO's
{{out}} (+ sign indicates "problem state" (non system key) issued WTO's
<pre style="height:16ex">
<pre style="height:16ex">
Line 78: Line 77:
</pre>
</pre>
;Structured Macros
;Structured Macros
<lang 360asm>* While 27/06/2016
<syntaxhighlight lang="360asm">* While 27/06/2016
WHILELOO CSECT
WHILELOO CSECT
USING WHILELOO,12 set base register
USING WHILELOO,12 set base register
Line 95: Line 94:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
WTOTXT DC CL4' ' wto text
END WHILELOO</lang>
END WHILELOO</syntaxhighlight>
{{out}}
{{out}}
Same as above
Same as above
Line 101: Line 100:
=={{header|6502 Assembly}}==
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR LoopsWhile). Specific OS/hardware routines for printing are left unimplemented.
Code is called as a subroutine (i.e. JSR LoopsWhile). Specific OS/hardware routines for printing are left unimplemented.
<lang 6502asm>LoopsWhile: PHA ;push accumulator onto stack
<syntaxhighlight lang="6502asm">LoopsWhile: PHA ;push accumulator onto stack


LDA #$00 ;the 6502 is an 8-bit processor
LDA #$00 ;the 6502 is an 8-bit processor
Line 117: Line 116:


EndLoop: PLA ;restore accumulator from stack
EndLoop: PLA ;restore accumulator from stack
RTS ;return from subroutine</lang>
RTS ;return from subroutine</syntaxhighlight>


=={{header|68000 Assembly}}==
=={{header|68000 Assembly}}==
Printing routines are left out since this is not the focus of the task.
Printing routines are left out since this is not the focus of the task.
<lang 68000devpac>main:
<syntaxhighlight lang="68000devpac">main:
MOVE.W #1024,D0
MOVE.W #1024,D0


Line 127: Line 126:
jsr PrintHexWord
jsr PrintHexWord
LSR.W #1,D0
LSR.W #1,D0
BNE WhileLoop</lang>
BNE WhileLoop</syntaxhighlight>


=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopwhile64.s */
/* program loopwhile64.s */
Line 187: Line 186:
/* for this file see task include a file in language AArch64 assembly */
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
{{Output}}
<pre>
<pre>
Line 203: Line 202:
</pre>
</pre>
=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>PROC Main()
<syntaxhighlight lang="action!">PROC Main()
CARD i=[1024]
CARD i=[1024]


Line 211: Line 210:
i=i/2
i=i/2
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/While.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/While.png Screenshot from Atari 8-bit computer]
Line 229: Line 228:


=={{header|ActionScript}}==
=={{header|ActionScript}}==
<lang actionscript>var i:int = 1024;
<syntaxhighlight lang="actionscript">var i:int = 1024;
while (i > 0) {
while (i > 0) {
trace(i);
trace(i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>declare
<syntaxhighlight lang="ada">declare
I : Integer := 1024;
I : Integer := 1024;
begin
begin
Line 243: Line 242:
I := I / 2;
I := I / 2;
end loop;
end loop;
end;</lang>
end;</syntaxhighlight>


=={{header|Agena}}==
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
Tested with Agena 2.9.5 Win32
<lang agena>scope
<syntaxhighlight lang="agena">scope
local i := 1024;
local i := 1024;
while i > 0 do
while i > 0 do
Line 253: Line 252:
i := i \ 2
i := i \ 2
od
od
epocs</lang>
epocs</syntaxhighlight>


=={{header|Aime}}==
=={{header|Aime}}==
<lang aime>integer i;
<syntaxhighlight lang="aime">integer i;


i = 1024;
i = 1024;
Line 262: Line 261:
o_plan(i, "\n");
o_plan(i, "\n");
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>


=={{header|ALGOL 60}}==
=={{header|ALGOL 60}}==
The Loops/While structure was in the Algol 60 report of January 1963.
The Loops/While structure was in the Algol 60 report of January 1963.
<lang algol60>begin
<syntaxhighlight lang="algol60">begin
comment Loops/While - algol60 - 21/10/2014;
comment Loops/While - algol60 - 21/10/2014;
integer i;
integer i;
for i:=1024,i div 2 while i>0 do outinteger(1,i)
for i:=1024,i div 2 while i>0 do outinteger(1,i)
end
end
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 283: Line 282:


{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
<lang algol68>INT i := 1024;
<syntaxhighlight lang="algol68">INT i := 1024;
WHILE i > 0 DO
WHILE i > 0 DO
print(i);
print(i);
i := i OVER 2
i := i OVER 2
OD</lang>
OD</syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 294: Line 293:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin
<syntaxhighlight lang="algolw">begin
integer i;
integer i;
i := 1024;
i := 1024;
Line 302: Line 301:
i := i div 2
i := i div 2
end
end
end.</lang>
end.</syntaxhighlight>


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<lang algol>begin
<syntaxhighlight lang="algol">begin
integer i;
integer i;
i := 1024;
i := 1024;
Line 312: Line 311:
i := i / 2;
i := i / 2;
end;
end;
end</lang>
end</syntaxhighlight>

=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <jambo.h>

Main
i=1024
Loop
Printnl 'i'
i \= 2
Back if 'i' is positive
End
</syntaxhighlight>
<p>Assembler Hopper code:</p>
<syntaxhighlight lang="amazing hopper">
main:
i=1024
____CODE_JUMP____883612951:,
{i};{"\n"}print;
i\=2
{i},jpos(____CODE_JUMP____883612951),____CODE_JUMP____854411479:,
emptystack?do{{0}};return
</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>


=={{header|AmbientTalk}}==
=={{header|AmbientTalk}}==
Line 318: Line 354:
Both arguments to this message must be blocks (aka anonymous functions or thunks).
Both arguments to this message must be blocks (aka anonymous functions or thunks).


<lang ambienttalk>// print 1024 512 etc
<syntaxhighlight lang="ambienttalk">// print 1024 512 etc
def i := 1024;
def i := 1024;
while: { i > 0 } do: {
while: { i > 0 } do: {
system.print(" "+i);
system.print(" "+i);
i := i/2;
i := i/2;
}</lang>
}</syntaxhighlight>


=={{header|AmigaE}}==
=={{header|AmigaE}}==
<lang amigae>PROC main()
<syntaxhighlight lang="amigae">PROC main()
DEF i = 1024
DEF i = 1024
WHILE i > 0
WHILE i > 0
Line 332: Line 368:
i := i / 2
i := i / 2
ENDWHILE
ENDWHILE
ENDPROC</lang>
ENDPROC</syntaxhighlight>


=={{header|AppleScript}}==
=={{header|AppleScript}}==
AppleScript does not natively support a standard out.
AppleScript does not natively support a standard out.
Use the Script Editor's Event Log as the output.
Use the Script Editor's Event Log as the output.
<lang AppleScript >set i to 1024
<syntaxhighlight lang="applescript ">set i to 1024
repeat while i > 0
repeat while i > 0
log i
log i
set i to i / 2
set i to i / 2
end repeat</lang>
end repeat</syntaxhighlight>


=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program loopwhile.s */
/* program loopwhile.s */
Line 466: Line 502:




</syntaxhighlight>
</lang>


=={{header|ArnoldC}}==
=={{header|ArnoldC}}==
<lang arnoldc>IT'S SHOWTIME
<syntaxhighlight lang="arnoldc">IT'S SHOWTIME
HEY CHRISTMAS TREE n
HEY CHRISTMAS TREE n
YOU SET US UP 1024
YOU SET US UP 1024
Line 479: Line 515:
ENOUGH TALK
ENOUGH TALK
CHILL
CHILL
YOU HAVE BEEN TERMINATED</lang>
YOU HAVE BEEN TERMINATED</syntaxhighlight>


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>i: 1024
<syntaxhighlight lang="rebol">i: 1024
while [i>0] [
while [i>0] [
print i
print i
i: i/2
i: i/2
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 504: Line 540:


=={{header|Asymptote}}==
=={{header|Asymptote}}==
<lang Asymptote>int i = 1024;
<syntaxhighlight lang="asymptote">int i = 1024;


while(i > 0) {
while(i > 0) {
Line 513: Line 549:
//# Integer division; equivalent to quotient(x,y).
//# Integer division; equivalent to quotient(x,y).
//Noting that the Python3 community adopted the comment symbol (//) for integer division, the
//Noting that the Python3 community adopted the comment symbol (//) for integer division, the
//Asymptote community decided to reciprocate and use their comment symbol for integer division!</lang>
//Asymptote community decided to reciprocate and use their comment symbol for integer division!</syntaxhighlight>




Line 519: Line 555:
With compile-time proof of termination of the loop (which is really a tail recursion).
With compile-time proof of termination of the loop (which is really a tail recursion).


<lang ats>#include "share/atspre_staload.hats"
<syntaxhighlight lang="ats">#include "share/atspre_staload.hats"


fn
fn
Line 538: Line 574:
implement
implement
main0 () =
main0 () =
loop_while ()</lang>
loop_while ()</syntaxhighlight>


{{out}}
{{out}}
Line 554: Line 590:


It is also possible to write an actual ‘while’ loop, although this method requires a variable (the ‘var’ declaration below) and has other problems, and should be used sparingly.
It is also possible to write an actual ‘while’ loop, although this method requires a variable (the ‘var’ declaration below) and has other problems, and should be used sparingly.
<lang ats>#include "share/atspre_staload.hats"
<syntaxhighlight lang="ats">#include "share/atspre_staload.hats"


fn
fn
Line 570: Line 606:
implement
implement
main0 () =
main0 () =
loop_while ()</lang>
loop_while ()</syntaxhighlight>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>i = 1024
<syntaxhighlight lang="autohotkey">i = 1024
While (i > 0)
While (i > 0)
{
{
Line 579: Line 615:
i := Floor(i / 2)
i := Floor(i / 2)
}
}
MsgBox % output</lang>
MsgBox % output</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<lang awk>BEGIN {
<syntaxhighlight lang="awk">BEGIN {
v = 1024
v = 1024
while(v > 0) {
while(v > 0) {
Line 588: Line 624:
v = int(v/2)
v = int(v/2)
}
}
}</lang>
}</syntaxhighlight>


=={{header|Axe}}==
=={{header|Axe}}==
<lang axe>1024→A
<syntaxhighlight lang="axe">1024→A
While A>0
While A>0
Disp A▶Dec,i
Disp A▶Dec,i
A/2→A
A/2→A
End</lang>
End</syntaxhighlight>

=={{header|Bait}}==
<syntaxhighlight lang="bait">
fun main() {
mut i := 1024
for i > 0 {
println(i)
i = i / 2
}
}
</syntaxhighlight>


=={{header|BASIC}}==
=={{header|BASIC}}==
In general, the <code>WHILE</code>-<code>WEND</code> (or <code>DO WHILE</code>-<code>LOOP</code>) statement is used or it is simulated with a construct with conditional jump.
{{works with|QuickBasic|4.5}}

{{works with|ASIC}}
==={{header|ANSI BASIC}}===
<lang qbasic>i = 1024
{{works with|Decimal BASIC}}
while i > 0
<syntaxhighlight lang="basic">
print i
i = i / 2
100 LET I = 1024
110 DO WHILE I > 0
wend</lang>
120 PRINT I
130 LET I = INT(I / 2)
140 LOOP
150 END
</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>


==={{header|Applesoft BASIC}}===
==={{header|Applesoft BASIC}}===
<lang Applesoft BASIC> 10 I% = 1024
<syntaxhighlight lang="applesoft basic"> 10 I% = 1024
20 IF I% > 0 THEN PRINT I%:I% = I% / 2: GOTO 20</lang>
20 IF I% > 0 THEN PRINT I%:I% = I% / 2: GOTO 20</syntaxhighlight>


==={{header|ASIC}}===
==={{header|ASIC}}===
<syntaxhighlight lang="basic">
Look also [[#BASIC|BASIC]].
<lang basic>
REM Loops/While
REM Loops/While


Line 622: Line 687:


END
END
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 639: Line 704:


==={{header|BaCon}}===
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">
Look also [[#BASIC|BASIC]].
<lang freebasic>
i = 1024
i = 1024
WHILE i > 0
WHILE i > 0
PRINT i
PRINT i
i = i / 2
i = i / 2
WEND</lang>
WEND</syntaxhighlight>

==={{header|Ballerina}}===
<syntaxhighlight lang="ballerina">int i = 1024;
while i > 0 {
io:println(i);
i = i / 2;
}
</syntaxhighlight>


==={{header|BASIC256}}===
==={{header|BASIC256}}===
<lang BASIC256>i = 1024
<syntaxhighlight lang="basic256">i = 1024


while i > 0
while i > 0
Line 655: Line 727:
end while
end while


end</lang>
end</syntaxhighlight>


==={{header|BBC BASIC}}===
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> i% = 1024
<syntaxhighlight lang="bbcbasic"> i% = 1024
WHILE i%
WHILE i%
PRINT i%
PRINT i%
i% DIV= 2
i% DIV= 2
ENDWHILE</lang>
ENDWHILE</syntaxhighlight>

==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
Both loops are equivalent
<syntaxhighlight lang="qbasic">100 i = 1024
110 do while i > 0
120 print i
130 i = int(i/2)
140 loop
150 print
160 i = 1024
170 while i > 0
180 print i
190 i = int(i/2)
200 wend</syntaxhighlight>


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
There is no WHILE construct in Commodore BASIC. A GOTO construct is used instead. Also, an integer variable name has a % sign as its suffix.
There is no WHILE construct in Commodore BASIC. A GOTO construct is used instead. Also, an integer variable name has a % sign as its suffix.
<lang gwbasic>10 N% = 1024
<syntaxhighlight lang="gwbasic">10 N% = 1024
20 IF N% = 0 THEN 60
20 IF N% = 0 THEN 60
30 PRINT N%
30 PRINT N%
40 N% = N%/2
40 N% = N%/2
50 GOTO 20
50 GOTO 20
60 END</lang>
60 END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 690: Line 777:


==={{header|Creative Basic}}===
==={{header|Creative Basic}}===
<lang Creative Basic>DEF X:INT
<syntaxhighlight lang="creative basic">DEF X:INT


X=1024
X=1024
Line 716: Line 803:


'Since this is, in fact, a Creative Basic console program.
'Since this is, in fact, a Creative Basic console program.
END</lang>
END</syntaxhighlight>
Note: Spacing is not an issue. I just find the code to be more readable with spaces.
Note: Spacing is not an issue. I just find the code to be more readable with spaces.

==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64

Dim i As Integer = 1024

While i > 0
Print i
i Shr= 1
Wend

Sleep</syntaxhighlight>

{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>

==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">window 1

long i = 1024

while i > 0
print i
i = int( i / 2 )
wend

HandleEvents</syntaxhighlight>
Output:
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>

==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=4e992013e4e7dc69a82477299a5ce23a Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short = 1024

While siCount > 0
Print siCount;;
siCount /= 2
Wend

End</syntaxhighlight>
Output:
<pre>
1024 512 256 128 64 32 16 8 4 2 1
</pre>

==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">10 I = 1024
20 WHILE I > 0
30 PRINT I
40 i = INT(i/2)
50 WEND
60 END</syntaxhighlight>


==={{header|IS-BASIC}}===
==={{header|IS-BASIC}}===
<lang IS-BASIC>100 LET I=1024
<syntaxhighlight lang="is-basic">100 LET I=1024
110 DO WHILE I>0
110 DO WHILE I>0
120 PRINT I
120 PRINT I
130 LET I=IP(I/2)
130 LET I=IP(I/2)
140 LOOP</lang>
140 LOOP</syntaxhighlight>

==={{header|IWBASIC}}===
<syntaxhighlight lang="iwbasic">
DEF X:INT

X=1024

OPENCONSOLE

WHILE X>0

PRINT X
X=X/2

ENDWHILE
'Output starts with 1024 and ends with 1.

'Putting the following in the loop will produce output starting with 512 and ending with 0:
'X=X/2
'PRINT X

'When compiled as a console only program, a press any key to continue message is automatic.
'I presume code is added by the compiler.
CLOSECONSOLE

'Since this is, in fact, an IWBASIC console program, which compiles and runs.
END</syntaxhighlight>
Note: Spacing is not an issue. I just find the code to be more readable with spaces.

==={{header|Liberty BASIC}}===
All integers are changed to floats if an operation creates a non-integer result.
Without using int() the program keeps going until erroring because accuracy was lost.
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">i = 1024
while i > 0
print i
i = int( i / 2)
wend
end</syntaxhighlight>

==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="microsoftsmallbasic">
i = 1024
While i > 0
TextWindow.WriteLine(i)
i = Math.Floor(i / 2)
EndWhile
</syntaxhighlight>


==={{header|Minimal BASIC}}===
==={{header|Minimal BASIC}}===
Line 730: Line 946:
{{works with|Commodore BASIC}}
{{works with|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="gwbasic">10 REM Loops/While
<lang gwbasic>
10 REM Loops/While
20 LET I = 1024
20 LET I = 1024
40 IF I <= 0 THEN 80
40 IF I <= 0 THEN 80
Line 737: Line 952:
60 LET I = INT(I/2)
60 LET I = INT(I/2)
70 GOTO 40
70 GOTO 40
80 END
80 END</syntaxhighlight>

</lang>
==={{header|MSX Basic}}===
There is no <code>WHILE</code> construct in MSX Basic. A <code>GOTO</code> construct is used instead.
<syntaxhighlight lang="qbasic">10 I% = 1024
20 IF I% = 0 THEN END
30 PRINT I%
40 I% = I%/2 : rem INT(I/2)
50 GOTO 20</syntaxhighlight>
Solutions [[#GW-BASIC|GW-BASIC]] and [[#Minimal _BASIC|Minimal BASIC]] work without changes.


==={{header|Nascom BASIC}}===
==={{header|Nascom BASIC}}===
Line 745: Line 968:
If the repeated sequence is short then the loop can be written in one line.
If the repeated sequence is short then the loop can be written in one line.
{{works with|Nascom ROM BASIC|4.7}}
{{works with|Nascom ROM BASIC|4.7}}
<lang basic>
<syntaxhighlight lang="basic">
10 REM Loops/While
10 REM Loops/While
20 LET I=1024
20 LET I=1024
30 IF I>0 THEN PRINT I:I=INT(I/2):GOTO 30
30 IF I>0 THEN PRINT I:I=INT(I/2):GOTO 30
40 END
40 END
</syntaxhighlight>
</lang>

==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 I=1024
20 IF I=0 THEN END
30 PRINT I
40 I=I/2
50 GOTO 20</syntaxhighlight>

==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">If OpenConsole()
x.i = 1024
While x > 0
PrintN(Str(x))
x / 2
Wend

Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</syntaxhighlight>

==={{header|QB64}}===
<syntaxhighlight lang="qb64">Dim n As Integer
n = 1024
While n > 0
Print n
n = n \ 2
Wend</syntaxhighlight>

==={{header|QBasic}}===
{{works with|QBasic|1.1}}
Both loops are equivalent
<syntaxhighlight lang="qbasic">i = 1024
WHILE i > 0
PRINT i
i = INT(i / 2)
WEND
PRINT

i = 1024
DO WHILE i > 0
PRINT i
i = i \ 2
LOOP
END</syntaxhighlight>

==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">i = 1024
WHILE i > 0
PRINT i
i = i \ 2
WEND</syntaxhighlight>

==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">i = 1024
while i > 0
print i
i = int(i / 2)
wend
end</syntaxhighlight>

==={{header|Sinclair ZX81 BASIC}}===
The distinctive thing about a <code>while</code> loop is that the conditional test happens before the loop body, not after—so that the code in the loop may be executed zero times.

Since we have no integer type, we floor the result of the division each time.
<syntaxhighlight lang="basic">10 LET I=1024
20 IF I=0 THEN GOTO 60
30 PRINT I
40 LET I=INT (I/2)
50 GOTO 20</syntaxhighlight>

==={{header|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
i = 1024
WHILE i > 0
PRINT i
i = i \ 2 ' Using \ for integer division instead of /
WEND</syntaxhighlight>

==={{header|TI-83 BASIC}}===
<syntaxhighlight lang="ti83b">1024→I
While I>0
Disp I
I/2→I
End
</syntaxhighlight>

==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">Local i
1024 → i
While i > 0
Disp i
intDiv(i, 2) → i
EndWhile</syntaxhighlight>


==={{Header|Tiny BASIC}}===
==={{Header|Tiny BASIC}}===
Tiny BASIC have no <code>while</code> construct. Equivalent using conditional jump:
Tiny BASIC have no <code>while</code> construct. Equivalent using conditional jump:
{{works with|TinyBasic}}
<lang tinybasic> REM Loops/While
<syntaxhighlight lang="basic">10 REM Loops/While
LET I = 1024
10 IF I <= 0 THEN GOTO 20
20 LET I = 1024
30 IF I <= 0 THEN GOTO 70
PRINT I
40 PRINT I
LET I = I / 2
GOTO 10
50 LET I = I / 2
60 GOTO 30
20 END</lang>
70 END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 777: Line 1,097:


==={{header|True BASIC}}===
==={{header|True BASIC}}===
<lang qbasic>LET i = 1024
<syntaxhighlight lang="qbasic">LET i = 1024


DO WHILE i > 0
DO WHILE i > 0
Line 783: Line 1,103:
LET i = INT(i / 2)
LET i = INT(i / 2)
LOOP
LOOP
END</lang>
END</syntaxhighlight>

==={{header|VBA}}===
<syntaxhighlight lang="vb">Public Sub LoopsWhile()
Dim value As Integer
value = 1024
Do While value > 0
Debug.Print value
value = value / 2
Loop
End Sub</syntaxhighlight>

==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">Dim x = 1024
Do
Console.WriteLine(x)
x = x \ 2
Loop While x > 0</syntaxhighlight>

==={{header|Wee Basic}}===
<syntaxhighlight lang="wee basic">let number=1024
while number>0.5
print 1 number
let number=number/2
wend
end</syntaxhighlight>

==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
i% = 1024
DO WHILE i% > 0
PRINT i%
i% = i% / 2
LOOP
</syntaxhighlight>

==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">i = 1024
while i > 0
Print i
i = int(i / 2)
wend
end</syntaxhighlight>


=={{header|bc}}==
=={{header|bc}}==
<lang bc>i = 1024
<syntaxhighlight lang="bc">i = 1024
while (i > 0) {
while (i > 0) {
i
i
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


=={{header|Befunge}}==
=={{header|Befunge}}==
<lang befunge>84*:*> :v
<syntaxhighlight lang="befunge">84*:*> :v
^/2,*25.:_@</lang>
^/2,*25.:_@</syntaxhighlight>


=={{header|blz}}==
=={{header|blz}}==
<lang blz>num = 1024
<syntaxhighlight lang="blz">num = 1024
while num > 1 # blz will automatically cast num to a fraction when dividing 1/2, so this is necessary to stop an infinite loop
while num > 1 # blz will automatically cast num to a fraction when dividing 1/2, so this is necessary to stop an infinite loop
print(num)
print(num)
num = num / 2
num = num / 2
end</lang>
end</syntaxhighlight>


=={{header|BQN}}==
=={{header|BQN}}==
Line 807: Line 1,170:
BQNcrate's while idiom is the closest equivalent of a while loop in the language.
BQNcrate's while idiom is the closest equivalent of a while loop in the language.


<lang bqn>_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
<syntaxhighlight lang="bqn">_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}


(⌊∘÷⟜2 •Show) _while_ (>⟜0) 1024</lang>
(⌊∘÷⟜2 •Show) _while_ (>⟜0) 1024</syntaxhighlight>


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>1024:?n & whl'(!n:>0 & out$!n & div$(!n.2):?n)</lang>
<syntaxhighlight lang="bracmat">1024:?n & whl'(!n:>0 & out$!n & div$(!n.2):?n)</syntaxhighlight>


=={{header|Brat}}==
=={{header|Brat}}==
Converts to integers so output is a little bit shorter and neater.
Converts to integers so output is a little bit shorter and neater.


<lang brat>i = 1024
<syntaxhighlight lang="brat">i = 1024
while { i > 0 } {
while { i > 0 } {
p i
p i
i = (i / 2).to_i
i = (i / 2).to_i
}</lang>
}</syntaxhighlight>


=={{header|C}}==
=={{header|C}}==
<lang c>int i = 1024;
<syntaxhighlight lang="c">int i = 1024;
while(i > 0) {
while(i > 0) {
printf("%d\n", i);
printf("%d\n", i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>
In for loop fashion:
In for loop fashion:
<lang c>int i;
<syntaxhighlight lang="c">int i;
for(i = 1024;i > 0; i/=2){
for(i = 1024;i > 0; i/=2){
printf("%d\n", i);
printf("%d\n", i);
}</lang>
}</syntaxhighlight>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>int i = 1024;
<syntaxhighlight lang="csharp">int i = 1024;
while(i > 0){
while(i > 0){
System.Console.WriteLine(i);
System.Console.WriteLine(i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>int i = 1024;
<syntaxhighlight lang="cpp">int i = 1024;
while(i > 0){
while(i > 0){
std::cout << i << std::endl;
std::cout << i << std::endl;
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>
Alternatively, it can be done with <code>for</code>:
Alternatively, it can be done with <code>for</code>:
<lang cpp>for(int i = 1024; i > 0; i /= 2)
<syntaxhighlight lang="cpp">for(int i = 1024; i > 0; i /= 2)
std::cout << i << std::endl;</lang>
std::cout << i << std::endl;</syntaxhighlight>


Instead of <code>i /= 2</code> one can also use the bit shift operator <code>i >>= 1</code> on integer variables.
Instead of <code>i /= 2</code> one can also use the bit shift operator <code>i >>= 1</code> on integer variables.


Indeed, in C++,
Indeed, in C++,
<lang cpp>for(init; cond; update){
<syntaxhighlight lang="cpp">for(init; cond; update){
statement;
statement;
}</lang>
}</syntaxhighlight>
is equivalent to
is equivalent to
<lang cpp>{
<syntaxhighlight lang="cpp">{
init;
init;
while(cond){
while(cond){
Line 865: Line 1,228:
update;
update;
}
}
}</lang>
}</syntaxhighlight>


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==
<lang Caché ObjectScript>WHILELOOP
<syntaxhighlight lang="caché objectscript">WHILELOOP
set x = 1024
set x = 1024
while (x > 0) {
while (x > 0) {
Line 875: Line 1,238:
}
}
quit</lang>
quit</syntaxhighlight>


{{out}}<pre>SAMPLES>DO ^WHILELOOP
{{out}}<pre>SAMPLES>DO ^WHILELOOP
Line 893: Line 1,256:


=={{header|Chapel}}==
=={{header|Chapel}}==
<lang chapel>var val = 1024;
<syntaxhighlight lang="chapel">var val = 1024;
while val > 0 {
while val > 0 {
writeln(val);
writeln(val);
val /= 2;
val /= 2;
}</lang>
}</syntaxhighlight>


=={{header|ChucK}}==
=={{header|ChucK}}==
<lang>
<syntaxhighlight lang="text">
1024 => int value;
1024 => int value;


Line 908: Line 1,271:
value / 2 => value;
value / 2 => value;
}
}
</syntaxhighlight>
</lang>


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(def i (ref 1024))
<syntaxhighlight lang="lisp">(def i (ref 1024))


(while (> @i 0)
(while (> @i 0)
(println @i)
(println @i)
(dosync (ref-set i (quot @i 2))))</lang>
(dosync (ref-set i (quot @i 2))))</syntaxhighlight>


2 ways without mutability:
2 ways without mutability:


<lang Clojure>(loop [i 1024]
<syntaxhighlight lang="clojure">(loop [i 1024]
(when (pos? i)
(when (pos? i)
(println i)
(println i)
Line 926: Line 1,289:


(doseq [i (take-while pos? (iterate #(quot % 2) 1024))]
(doseq [i (take-while pos? (iterate #(quot % 2) 1024))]
(println i))</lang>
(println i))</syntaxhighlight>


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>start_up = proc ()
<syntaxhighlight lang="clu">start_up = proc ()
po: stream := stream$primary_output()
po: stream := stream$primary_output()
n: int := 1024
n: int := 1024
Line 936: Line 1,299:
n := n/2
n := n/2
end
end
end start_up</lang>
end start_up</syntaxhighlight>


=={{header|COBOL}}==
=={{header|COBOL}}==
COBOL does not have a while loop construct, but it is does have a <code>PERFORM UNTIL</code> structure, which means that the normal condition used in a while loop must be negated.
COBOL does not have a while loop construct, but it is does have a <code>PERFORM UNTIL</code> structure, which means that the normal condition used in a while loop must be negated.
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Loop-While.
PROGRAM-ID. Loop-While.


Line 954: Line 1,317:


GOBACK
GOBACK
.</lang>
.</syntaxhighlight>


=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
Line 960: Line 1,323:


With tags:
With tags:
<lang cfm><cfset i = 1024 /><cfloop condition="i GT 0"> #i#< br />
<syntaxhighlight lang="cfm"><cfset i = 1024 /><cfloop condition="i GT 0"> #i#< br />
<cfset i /= 2 />
<cfset i /= 2 />
</cfloop></lang>
</cfloop></syntaxhighlight>
With script:
With script:
<lang cfm><cfscript> i = 1024;
<syntaxhighlight lang="cfm"><cfscript> i = 1024;
while( i > 0 )
while( i > 0 )
{
{
writeOutput( i + "< br/ >" );
writeOutput( i + "< br/ >" );
}
}
</cfscript></lang>
</cfscript></syntaxhighlight>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(let ((i 1024))
<syntaxhighlight lang="lisp">(let ((i 1024))
(loop while (plusp i) do
(loop while (plusp i) do
(print i)
(print i)
Line 986: Line 1,349:
(print *i*)
(print *i*)
(setf *i* (floor *i* 2)))
(setf *i* (floor *i* 2)))
</syntaxhighlight>
</lang>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


var n: uint16 := 1024;
var n: uint16 := 1024;
Line 996: Line 1,359:
print_nl();
print_nl();
n := n/2;
n := n/2;
end loop;</lang>
end loop;</syntaxhighlight>


=={{header|Crack}}==
=={{header|Crack}}==
<lang crack>i = 1024;
<syntaxhighlight lang="crack">i = 1024;
while( i > 0 ) {
while( i > 0 ) {
cout ` $i\n`;
cout ` $i\n`;
i = i/2;
i = i/2;
}</lang>
}</syntaxhighlight>


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang crystal>i = 1024
<syntaxhighlight lang="crystal">i = 1024
while i > 0
while i > 0
puts i
puts i
i //= 2
i //= 2
end</lang>
end</syntaxhighlight>


<code>until ''condition''</code> is the negated version, equivalent to <code>while !(''condition'')</code>.
<code>until ''condition''</code> is the negated version, equivalent to <code>while !(''condition'')</code>.


<lang crystal>i = 1024
<syntaxhighlight lang="crystal">i = 1024
until i <= 0
until i <= 0
puts i
puts i
i //= 2
i //= 2
end</lang>
end</syntaxhighlight>


=={{header|D}}==
=={{header|D}}==
<lang d>import std.stdio;
<syntaxhighlight lang="d">import std.stdio;


void main() {
void main() {
Line 1,030: Line 1,393:
i >>= 1;
i >>= 1;
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1024
<pre>1024
Line 1,045: Line 1,408:


=={{header|Dao}}==
=={{header|Dao}}==
<lang dao>i = 1024;
<syntaxhighlight lang="dao">i = 1024;
while( i > 0 ) i = i / 2;</lang>
while( i > 0 ) i = i / 2;</syntaxhighlight>

=={{header|Dart}}==
<syntaxhighlight lang="dart">void main() {
var val = 1024;
while (val > 0) {
print(val);
val >>= 2;
}
}</syntaxhighlight>

Instead of the bitshift operator i >>= 2, you can also use i /= 2 on double variables.
<syntaxhighlight lang="dart">void main() {
num val = 1024;
while (val > 0) {
print(val);
val /= 2;
}
}</syntaxhighlight>


=={{header|Dc}}==
=={{header|Dc}}==
<lang Dc>[ q ] sQ [ d 0!<Q p 2 / lW x ] sW 1024 lW x</lang>
<syntaxhighlight lang="dc">[ q ] sQ [ d 0!<Q p 2 / lW x ] sW 1024 lW x</syntaxhighlight>


=={{header|DCL}}==
=={{header|DCL}}==
Line 1,056: Line 1,437:
so must make do with IF/THEN/ELSE and GOTO statements.
so must make do with IF/THEN/ELSE and GOTO statements.


<lang DCL>$ i = 1024
<syntaxhighlight lang="dcl">$ i = 1024
$Loop:
$Loop:
$ IF ( i .LE. 0 ) THEN GOTO LoopEnd
$ IF ( i .LE. 0 ) THEN GOTO LoopEnd
Line 1,065: Line 1,446:
$ i = i / 2
$ i = i / 2
$ GOTO Loop
$ GOTO Loop
$LoopEnd:</lang>
$LoopEnd:</syntaxhighlight>


=={{header|Delphi}}==
=={{header|Delphi}}==


<syntaxhighlight lang="delphi">var
<lang Delphi>var
i : Integer;
i : Integer;
begin
begin
Line 1,079: Line 1,460:
i := i div 2;
i := i div 2;
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec main() void:
<syntaxhighlight lang="draco">proc nonrec main() void:
word i;
word i;
i := 1024;
i := 1024;
Line 1,089: Line 1,470:
i := i >> 1
i := i >> 1
od
od
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre>1024
<pre>1024
Line 1,104: Line 1,485:


=={{header|Dragon}}==
=={{header|Dragon}}==
<lang dragon>i = 1024
<syntaxhighlight lang="dragon">i = 1024
while(i > 0){
while(i > 0){
showln i
showln i
i >>= 1 //also acceptable: i /= 2
i >>= 1 //also acceptable: i /= 2
}</lang>
}</syntaxhighlight>


=={{header|DUP}}==
=={{header|DUP}}==


<lang dup>1024[$][$.10,2/\%]# {Short form}</lang>
<syntaxhighlight lang="dup">1024[$][$.10,2/\%]# {Short form}</syntaxhighlight>


Explanation:
Explanation:
<lang dup>1024 {push 1024 on stack}
<syntaxhighlight lang="dup">1024 {push 1024 on stack}
[ ][ ]# {while[condition>0][do]}
[ ][ ]# {while[condition>0][do]}
$ {DUP}
$ {DUP}
$. {DUP, print top of stack to STDOUT}
$. {DUP, print top of stack to STDOUT}
10, {print newline}
10, {print newline}
2/\% {2 DIV/MOD SWAP POP}</lang>
2/\% {2 DIV/MOD SWAP POP}</syntaxhighlight>


Alternative, if the interpreter allows using the shift operator:
Alternative, if the interpreter allows using the shift operator:


<lang dup>1024[$][$.10,1»]#</lang>
<syntaxhighlight lang="dup">1024[$][$.10,1»]#</syntaxhighlight>


Output:
Output:


<lang dup>1024
<syntaxhighlight lang="dup">1024
512
512
256
256
Line 1,138: Line 1,519:
4
4
2
2
1</lang>
1</syntaxhighlight>


=={{header|DWScript}}==
=={{header|DWScript}}==


<lang Delphi>var i := 1024;
<syntaxhighlight lang="delphi">var i := 1024;


while i > 0 do begin
while i > 0 do begin
PrintLn(i);
PrintLn(i);
i := i div 2;
i := i div 2;
end;</lang>
end;</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
Line 1,153: Line 1,534:
{{trans|Swift}}
{{trans|Swift}}


<lang Dyalect>var i = 1024
<syntaxhighlight lang="dyalect">var i = 1024
while i > 0 {
while i > 0 {
print(i)
print(i)
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


=={{header|E}}==
=={{header|E}}==


<lang e>var i := 1024
<syntaxhighlight lang="e">var i := 1024
while (i > 0) {
while (i > 0) {
println(i)
println(i)
i //= 2
i //= 2
}</lang>
}</syntaxhighlight>


=={{header|EasyLang}}==
=={{header|EasyLang}}==


<lang>i = 1024
<syntaxhighlight lang="text">i = 1024
while i > 0
while i > 0
print i
print i
i = i div 2
i = i div 2
.</lang>
.</syntaxhighlight>


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(set! n 1024)
(set! n 1024)
(while (> n 0) (write n) (set! n (quotient n 2)))
(while (> n 0) (write n) (set! n (quotient n 2)))
1024 512 256 128 64 32 16 8 4 2 1
1024 512 256 128 64 32 16 8 4 2 1
</syntaxhighlight>
</lang>


=={{header|EGL}}==
=={{header|EGL}}==


<lang EGL>x int = 1024;
<syntaxhighlight lang="egl">x int = 1024;
while ( x > 0 )
while ( x > 0 )
SysLib.writeStdout( x );
SysLib.writeStdout( x );
x = MathLib.floor( x / 2 );
x = MathLib.floor( x / 2 );
end</lang>
end</syntaxhighlight>


=={{header|Elena}}==
=={{header|Elena}}==
ELENA 4.x:
ELENA 6.x:
<lang elena>public program()
<syntaxhighlight lang="elena">public program()
{
{
int i := 1024;
int i := 1024;
while (i > 0)
while (i > 0)
{
{
console.writeLine:i;
console.writeLine(i);
i /= 2
i /= 2
}
}
}</lang>
}</syntaxhighlight>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Loops do
<syntaxhighlight lang="elixir">defmodule Loops do
def while(0), do: :ok
def while(0), do: :ok
def while(n) do
def while(n) do
Line 1,212: Line 1,593:
end
end


Loops.while(1024)</lang>
Loops.while(1024)</syntaxhighlight>


=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
<lang Lisp>(let ((i 1024))
<syntaxhighlight lang="lisp">(let ((i 1024))
(while (> i 0)
(while (> i 0)
(message "%d" i)
(message "%d" i)
(setq i (/ i 2))))</lang>
(setq i (/ i 2))))</syntaxhighlight>

=={{header|EMal}}==
<syntaxhighlight lang="emal">
int i = 1024
while i > 0
writeLine(i)
i /= 2
end
</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
<lang erlang>-module(while).
<syntaxhighlight lang="erlang">-module(while).
-export([loop/0]).
-export([loop/0]).


Line 1,232: Line 1,622:
loop(N) when N >0 ->
loop(N) when N >0 ->
io:format("~w~n", [N]),
io:format("~w~n", [N]),
loop(N div 2).</lang>
loop(N div 2).</syntaxhighlight>


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
I%=1024
I%=1024
WHILE I%>0 DO ! you can leave out >0
WHILE I%>0 DO ! you can leave out >0
Line 1,241: Line 1,631:
I%=I% DIV 2 ! I%=INT(I%/2) for C-64 version
I%=I% DIV 2 ! I%=INT(I%/2) for C-64 version
END WHILE
END WHILE
</syntaxhighlight>
</lang>


=={{header|Euphoria}}==
=={{header|Euphoria}}==
<lang Euphoria>integer i
<syntaxhighlight lang="euphoria">integer i
i = 1024
i = 1024


Line 1,250: Line 1,640:
printf(1, "%g\n", {i})
printf(1, "%g\n", {i})
i = floor(i/2) --Euphoria does NOT use integer division. 1/2 = 0.5
i = floor(i/2) --Euphoria does NOT use integer division. 1/2 = 0.5
end while</lang>
end while</syntaxhighlight>
Even without the <code>floor()</code> the code will in fact end. But it's FAR beyond 1.
Even without the <code>floor()</code> the code will in fact end. But it's FAR beyond 1.


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>let rec loop n = if n > 0 then printf "%d " n; loop (n / 2)
<syntaxhighlight lang="fsharp">let rec loop n = if n > 0 then printf "%d " n; loop (n / 2)
loop 1024</lang>
loop 1024</syntaxhighlight>


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>1024 [ dup 0 > ] [ dup . 2 /i ] while drop</lang>
<syntaxhighlight lang="factor">1024 [ dup 0 > ] [ dup . 2 /i ] while drop</syntaxhighlight>


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>1024[$0>][$."
<syntaxhighlight lang="false">1024[$0>][$."
"2/]#%</lang>
"2/]#%</syntaxhighlight>


=={{header|Fantom}}==
=={{header|Fantom}}==


<lang fantom>class Main
<syntaxhighlight lang="fantom">class Main
{
{
public static Void main ()
public static Void main ()
Line 1,277: Line 1,667:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Fennel}}==
=={{header|Fennel}}==
<lang fennel>(var n 1024)
<syntaxhighlight lang="fennel">(var n 1024)
(while (> i 0)
(while (> i 0)
(print i)
(print i)
(set i (// n 2)))</lang>
(set i (// n 2)))</syntaxhighlight>
=={{header|Fermat}}==
=={{header|Fermat}}==
Note that the test is against something between 1/2 and 1, rather than 0. Otherwise Fermat will continue printing numbers of the form 1/2^n indefinitely.
Note that the test is against something between 1/2 and 1, rather than 0. Otherwise Fermat will continue printing numbers of the form 1/2^n indefinitely.
<lang fermat>n:=1024;
<syntaxhighlight lang="fermat">n:=1024;
while n>2/3 do !!n;n:=n/2; od;</lang>
while n>2/3 do !!n;n:=n/2; od;</syntaxhighlight>


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: halving ( n -- )
<syntaxhighlight lang="forth">: halving ( n -- )
begin dup 0 >
begin dup 0 >
while cr dup . 2/
while cr dup . 2/
repeat drop ;
repeat drop ;
1024 halving</lang>
1024 halving</syntaxhighlight>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran>INTEGER :: i = 1024
<syntaxhighlight lang="fortran">INTEGER :: i = 1024
DO WHILE (i > 0)
DO WHILE (i > 0)
WRITE(*,*) i
WRITE(*,*) i
i = i / 2
i = i / 2
END DO</lang>
END DO</syntaxhighlight>


{{works with|Fortran|77 and later}}
{{works with|Fortran|77 and later}}
<lang fortran> PROGRAM LOOPWHILE
<syntaxhighlight lang="fortran"> PROGRAM LOOPWHILE
INTEGER I
INTEGER I


Line 1,321: Line 1,711:
ENDIF
ENDIF
STOP
STOP
END</lang>
END</syntaxhighlight>


{{works with|Fortran|IV and 66 and later}}
{{works with|Fortran|IV and 66 and later}}
<lang fortran> PROGRAM LOOPWHILE
<syntaxhighlight lang="fortran"> PROGRAM LOOPWHILE
INTEGER I
INTEGER I
C FORTRAN 66 does not have IF block.
C FORTRAN 66 does not have IF block.
Line 1,335: Line 1,725:
20 CONTINUE
20 CONTINUE
STOP
STOP
END</lang>
END</syntaxhighlight>


=={{header|Fortress}}==
=={{header|Fortress}}==
<lang fortress>
<syntaxhighlight lang="fortress">
component loops_while
component loops_while
export Executable
export Executable
Line 1,348: Line 1,738:
end
end
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,363: Line 1,753:
2
2
1
1
</pre>

=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64

Dim i As Integer = 1024

While i > 0
Print i
i Shr= 1
Wend

Sleep</lang>

{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>
</pre>


=={{header|Frink}}==
=={{header|Frink}}==
<lang frink>i=1024
<syntaxhighlight lang="frink">i=1024
while i>0
while i>0
{
{
i = i/1
i = i/1
}</lang>
}</syntaxhighlight>

=={{header|FutureBasic}}==
<lang futurebasic>window 1

long i = 1024

while i > 0
print i
i = int( i / 2 )
wend

HandleEvents</lang>
Output:
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>

=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=4e992013e4e7dc69a82477299a5ce23a Click this link to run this code]'''
<lang gambas>Public Sub Main()
Dim siCount As Short = 1024

While siCount > 0
Print siCount;;
siCount /= 2
Wend

End</lang>
Output:
<pre>
1024 512 256 128 64 32 16 8 4 2 1
</pre>


=={{header|GAP}}==
=={{header|GAP}}==
<lang gap>n := 1024;
<syntaxhighlight lang="gap">n := 1024;
while n > 0 do
while n > 0 do
Print(n, "\n");
Print(n, "\n");
n := QuoInt(n, 2);
n := QuoInt(n, 2);
od;</lang>
od;</syntaxhighlight>


=={{header|GML}}==
=={{header|GML}}==
<lang GML>i = 1024
<syntaxhighlight lang="gml">i = 1024
while(i > 0)
while(i > 0)
{
{
show_message(string(i))
show_message(string(i))
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


=={{header|Go}}==
=={{header|Go}}==
<lang go>i := 1024
<syntaxhighlight lang="go">i := 1024
for i > 0 {
for i > 0 {
fmt.Printf("%d\n", i)
fmt.Printf("%d\n", i)
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<lang groovy>int i = 1024
<syntaxhighlight lang="groovy">int i = 1024
while (i > 0) {
while (i > 0) {
println i
println i
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,485: Line 1,806:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (when)
<syntaxhighlight lang="haskell">import Control.Monad (when)


main = loop 1024
main = loop 1024
where loop n = when (n > 0)
where loop n = when (n > 0)
(do print n
(do print n
loop (n `div` 2))</lang>
loop (n `div` 2))</syntaxhighlight>


You can use whileM_ function from monad-loops package that operates on monads:
You can use whileM_ function from monad-loops package that operates on monads:


<lang haskell>import Data.IORef
<syntaxhighlight lang="haskell">import Data.IORef
import Control.Monad.Loops
import Control.Monad.Loops


Line 1,503: Line 1,824:
(do n <- readIORef r
(do n <- readIORef r
print n
print n
modifyIORef r (`div` 2))</lang>
modifyIORef r (`div` 2))</syntaxhighlight>


With MonadComprehensions extension you can write it a little bit more readable:
With MonadComprehensions extension you can write it a little bit more readable:
<lang haskell>{-# LANGUAGE MonadComprehensions #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE MonadComprehensions #-}
import Data.IORef
import Data.IORef
import Control.Monad.Loops
import Control.Monad.Loops
Line 1,516: Line 1,837:
n <- readIORef r
n <- readIORef r
print n
print n
modifyIORef r (`div` 2)</lang>
modifyIORef r (`div` 2)</syntaxhighlight>


=={{header|Haxe}}==
=={{header|Haxe}}==
Using shift right.
Using shift right.
<lang haxe>var i = 1024;
<syntaxhighlight lang="haxe">var i = 1024;
while (i > 0) {
while (i > 0) {
Sys.println(i);
Sys.println(i);
i >>= 1;
i >>= 1;
}</lang>
}</syntaxhighlight>


Using integer division.
Using integer division.
<lang haxe>var i = 1024;
<syntaxhighlight lang="haxe">var i = 1024;
while (i > 0) {
while (i > 0) {
Sys.println(i);
Sys.println(i);
i = Std.int(i / 2);
i = Std.int(i / 2);
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,551: Line 1,872:


=={{header|hexiscript}}==
=={{header|hexiscript}}==
<lang hexiscript>let i 1024
<syntaxhighlight lang="hexiscript">let i 1024
while i > 0
while i > 0
println i
println i
let i (i / 2)
let i (i / 2)
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|HolyC}}==
=={{header|HolyC}}==
<lang holyc>U16 i = 1024;
<syntaxhighlight lang="holyc">U16 i = 1024;
while (i > 0) {
while (i > 0) {
Print("%d\n", i);
Print("%d\n", i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
<lang icon>procedure main()
<syntaxhighlight lang="icon">procedure main()
local i
local i
i := 1024
i := 1024
while write(0 < (i := i / 2))
while write(0 < (i := i / 2))
end</lang>
end</syntaxhighlight>


=={{header|Inform 7}}==
=={{header|Inform 7}}==
<lang inform7>let N be 1024;
<syntaxhighlight lang="inform7">let N be 1024;
while N > 0:
while N > 0:
say "[N][line break]";
say "[N][line break]";
let N be N / 2;</lang>
let N be N / 2;</syntaxhighlight>


=={{header|IWBASIC}}==
=={{Header|Insitux}}==
<lang IWBASIC>
DEF X:INT


Use <code>/</code> for floating-point division, and use <code>(> i 0)</code> for full printing of the sequence (as the output of <code>1</code> here is due to the return of the last <code>while</code> statement).
X=1024


<syntaxhighlight lang="insitux">
OPENCONSOLE
(var i 1024)


(while (> i 1)
WHILE X>0
(print i)
(var i (// i 2)))
</syntaxhighlight>


{{out}}
PRINT X
X=X/2


<pre>
ENDWHILE
1024
'Output starts with 1024 and ends with 1.
512

256
'Putting the following in the loop will produce output starting with 512 and ending with 0:
128
'X=X/2
64
'PRINT X
32

16
'When compiled as a console only program, a press any key to continue message is automatic.
8
'I presume code is added by the compiler.
4
CLOSECONSOLE
2

1
'Since this is, in fact, an IWBASIC console program, which compiles and runs.
END</lang>
</pre>
Note: Spacing is not an issue. I just find the code to be more readable with spaces.


=={{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</lang>
<syntaxhighlight lang="j">,. <.@-:^:*^:a: 1024</syntaxhighlight>


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>monad define 1024
<syntaxhighlight lang="j">monad define 1024
while. 0 < y do.
while. 0 < y do.
smoutput y
smoutput y
Line 1,618: Line 1,939:
end.
end.
i.0 0
i.0 0
)</lang>
)</syntaxhighlight>


Note: this defines an anonymous function (monad define, and the subsequent lines) and passes it the argument 1024, which means it will be executed as soon as the full definition is available.
Note: this defines an anonymous function (monad define, and the subsequent lines) and passes it the argument 1024, which means it will be executed as soon as the full definition is available.


=={{header|Java}}==
=={{header|Java}}==
<lang java5>int i = 1024;
<syntaxhighlight lang="java5">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>
}</syntaxhighlight>
With a for loop:
With a for loop:
<lang java5>for(int i = 1024; i > 0;i /= 2 /*or i>>= 1*/){
<syntaxhighlight lang="java5">for(int i = 1024; i > 0;i /= 2 /*or i>>= 1*/){
System.out.println(i);
System.out.println(i);
}</lang>
}</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>var n = 1024;
<syntaxhighlight lang="javascript">var n = 1024;
while (n > 0) {
while (n > 0) {
print(n);
print(n);
n /= 2;
n /= 2;
}</lang>
}</syntaxhighlight>


In a functional idiom of JavaScript, however, we can not use a While '''statement''' to achieve this task, as statements return no value, mutate state, and can not be composed within other functional expressions.
In a functional idiom of JavaScript, however, we can not use a While '''statement''' to achieve this task, as statements return no value, mutate state, and can not be composed within other functional expressions.
Line 1,647: Line 1,968:
:#A conditional function, corresponding to the While test
:#A conditional function, corresponding to the While test


<lang JavaScript>function loopWhile(varValue, fnDelta, fnTest) {
<syntaxhighlight lang="javascript">function loopWhile(varValue, fnDelta, fnTest) {
'use strict';
'use strict';
var d = fnDelta(varValue);
var d = fnDelta(varValue);
Line 1,666: Line 1,987:
}
}
).join('\n')
).join('\n')
);</lang>
);</syntaxhighlight>


If we assume integer division here (Math.floor(x/2)) rather than the floating point division (x/2) used in the imperative example, we obtain the output:
If we assume integer division here (Math.floor(x/2)) rather than the floating point division (x/2) used in the imperative example, we obtain the output:


<syntaxhighlight lang="javascript">512
<lang JavaScript>512
256
256
128
128
Line 1,679: Line 2,000:
4
4
2
2
1</lang>
1</syntaxhighlight>


=={{header|Joy}}==
=={{header|Joy}}==
<lang joy>DEFINE putln == put '\n putch.
<syntaxhighlight lang="joy">DEFINE putln == put '\n putch.
1024 [] [dup putln 2 /] while.</syntaxhighlight>

1024 [] [dup putln 2 /] while.</lang>


=={{header|jq}}==
=={{header|jq}}==
'''Using recurse/1'''<lang jq># To avoid printing 0, test if the input is greater than 1
'''Using recurse/1'''<syntaxhighlight lang="jq"># To avoid printing 0, test if the input is greater than 1
1024 | recurse( if . > 1 then ./2 | floor else empty end)</lang>
1024 | recurse( if . > 1 then ./2 | floor else empty end)</syntaxhighlight>
'''Using recurse/2''' (requires jq >1.4)
'''Using recurse/2''' (requires jq >1.4)
<lang jq>1024 | recurse( ./2 | floor; . > 0)</lang>
<syntaxhighlight lang="jq">1024 | recurse( ./2 | floor; . > 0)</syntaxhighlight>
'''Using a filter'''
'''Using a filter'''
<lang jq>def task: if . > 0 then ., (./2 | floor | task) else empty end;
<syntaxhighlight lang="jq">def task: if . > 0 then ., (./2 | floor | task) else empty end;
1024|task</lang>
1024|task</syntaxhighlight>
'''Using while/2'''
'''Using while/2'''


If your jq does not include while/2 as a builtin, here is its definition:
If your jq does not include while/2 as a builtin, here is its definition:
<lang jq>def while(cond; update):
<syntaxhighlight lang="jq">def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
def _while: if cond then ., (update | _while) else empty end;
_while;</lang>
_while;</syntaxhighlight>
For example:
For example:
<lang jq>1024|while(. > 0; ./2|floor)</lang>
<syntaxhighlight lang="jq">1024|while(. > 0; ./2|floor)</syntaxhighlight>


=={{header|Jsish}}==
=={{header|Jsish}}==
<lang javascript>#!/usr/bin/env jsish
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Loops/While in Jsish */
/* Loops/While in Jsish */
var i = 1024;
var i = 1024;
Line 1,724: Line 2,044:
1
1
=!EXPECTEND!=
=!EXPECTEND!=
*/</lang>
*/</syntaxhighlight>


{{out}}
{{out}}
Line 1,731: Line 2,051:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
n = 1024
n = 1024


Line 1,738: Line 2,058:
n >>= 1
n >>= 1
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,757: Line 2,077:
Implementation of the task using anonymous function is
Implementation of the task using anonymous function is
given below
given below
<syntaxhighlight lang="k">
<lang K>
{while[x>0; \echo x; x%:2]} 1024
{while[x>0; \echo x; x%:2]} 1024
</syntaxhighlight>
</lang>


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 1,770: Line 2,090:
value /= 2
value /= 2
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,792: Line 2,112:


=={{header|Lambdatalk}}==
=={{header|Lambdatalk}}==
<lang scheme>
<syntaxhighlight lang="scheme">
{def loops_while
{def loops_while
{lambda {:i}
{lambda {:i}
Line 1,802: Line 2,122:
{loops_while 1024}
{loops_while 1024}
-> 1024 512 256 128 64 32 16 8 4 2 1 (end of loop)
-> 1024 512 256 128 64 32 16 8 4 2 1 (end of loop)
</syntaxhighlight>
</lang>


Alternative solution:
=={{header|Lang5}}==
<syntaxhighlight lang="scheme">
{{trans|Factor}}
<lang lang5>: /i / int ; : 0= 0 == ;
: dip swap '_ set execute _ ; : dupd 'dup dip ;
: 2dip swap '_x set swap '_y set execute _y _x ;
: while
do dupd 'execute 2dip
rot 0= if break else dup 2dip then
loop ;

1024 "dup 0 >" "dup . 2 /i" while</lang>

=={{header|Lambdatalk}}==
<lang scheme>
{def while
{def while
{lambda {:i}
{lambda {:i}
Line 1,837: Line 2,145:
2
2
1
1
</syntaxhighlight>
</lang>

=={{header|Lang}}==
<syntaxhighlight lang="lang">
$n = 1024
while($n > 0) {
fn.println($n)
$n //= 2
}
</syntaxhighlight>


=={{header|langur}}==
=={{header|Lang5}}==
{{trans|Factor}}
0.8 changed the keyword for a test only loop from for to while.
<syntaxhighlight lang="lang5">: /i / int ; : 0= 0 == ;
: dip swap '_ set execute _ ; : dupd 'dup dip ;
: 2dip swap '_x set swap '_y set execute _y _x ;
: while
do dupd 'execute 2dip
rot 0= if break else dup 2dip then
loop ;


1024 "dup 0 >" "dup . 2 /i" while</syntaxhighlight>
{{works with|langur|0.8}}

<lang langur>var .i = 1024
=={{header|langur}}==
<syntaxhighlight lang="langur">var .i = 1024
while .i > 0 {
while .i > 0 {
writeln .i
writeln .i
.i \= 2
.i \= 2
}</lang>
}</syntaxhighlight>

{{works with|langur|< 0.8}}
<lang langur>var .i = 1024
for .i > 0 {
writeln .i
.i \= 2
}</lang>


=={{header|Lasso}}==
=={{header|Lasso}}==
<lang Lasso>local(i = 1024)
<syntaxhighlight lang="lasso">local(i = 1024)
while(#i > 0) => {^
while(#i > 0) => {^
#i + '\r'
#i + '\r'
#i /= 2
#i /= 2
^}</lang>
^}</syntaxhighlight>


=={{header|Liberty BASIC}}==
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
All integers are changed to floats if an operation creates a non-integer result.
n is number
Without using int() the program keeps going until erroring because accuracy was lost.

<lang lb>i = 1024
procedure:
while i > 0
store 1024 in n
print i
while n is greater than 0 do
i = int( i / 2)
display n lf
wend
divide n by 2 in n
end</lang>
floor n
repeat</syntaxhighlight>


=={{header|LIL}}==
=={{header|LIL}}==
<lang tcl>set num 1024; while {$num > 0} {print $num; set num [expr $num \ 2]}</lang>
<syntaxhighlight lang="tcl">set num 1024; while {$num > 0} {print $num; set num [expr $num \ 2]}</syntaxhighlight>


Backslash is integer division, otherwise LIL would allow the division to go floating point.
Backslash is integer division, otherwise LIL would allow the division to go floating point.


=={{header|Lingo}}==
=={{header|Lingo}}==
<lang lingo>n = 1024
<syntaxhighlight lang="lingo">n = 1024
repeat while n>0
repeat while n>0
put n
put n
n = n/2 -- integer division implicitely returns floor: 1/2 -> 0
n = n/2 -- integer division implicitely returns floor: 1/2 -> 0
end repeat</lang>
end repeat</syntaxhighlight>


=={{header|Lisaac}}==
=={{header|Lisaac}}==
<lang Lisaac>+ i : INTEGER;
<syntaxhighlight lang="lisaac">+ i : INTEGER;
i := 1024;
i := 1024;
{ i > 0 }.while_do {
{ i > 0 }.while_do {
Line 1,892: Line 2,214:
i := i / 2;
i := i / 2;
};</lang>
};</syntaxhighlight>


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<lang LiveCode>put 1024 into n
<syntaxhighlight lang="livecode">put 1024 into n
repeat while n > 0
repeat while n > 0
put n & cr
put n & cr
divide n by 2
divide n by 2
end repeat</lang>
end repeat</syntaxhighlight>


=={{header|Logo}}==
=={{header|Logo}}==
<lang logo>make "n 1024
<syntaxhighlight lang="logo">make "n 1024
while [:n > 0] [print :n make "n :n / 2]</lang>
while [:n > 0] [print :n make "n :n / 2]</syntaxhighlight>


=={{header|LOLCODE}}==
=={{header|LOLCODE}}==
Line 1,909: Line 2,231:
LOLCODE's loop semantics require an afterthought if a condition is used, thus the <tt>nop</tt> in the following example. The more idiomatic approach would have been to <tt>GTFO</tt> of the loop once <tt>n</tt> had reached 0.
LOLCODE's loop semantics require an afterthought if a condition is used, thus the <tt>nop</tt> in the following example. The more idiomatic approach would have been to <tt>GTFO</tt> of the loop once <tt>n</tt> had reached 0.


<lang LOLCODE>HAI 1.3
<syntaxhighlight lang="lolcode">HAI 1.3


I HAS A n ITZ 1024
I HAS A n ITZ 1024
Line 1,918: Line 2,240:
IM OUTTA YR loop
IM OUTTA YR loop


KTHXBYE</lang>
KTHXBYE</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>n = 1024
<syntaxhighlight lang="lua">n = 1024
while n>0 do
while n>0 do
print(n)
print(n)
n = math.floor(n/2)
n = math.floor(n/2)
end</lang>
end</syntaxhighlight>


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Module Checkit {
Def long A=1024
Def long A=1024
Line 1,937: Line 2,259:
}
}
Checkit
Checkit
</syntaxhighlight>
</lang>
One line
One line
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Online { A=1024&: While A>0 {Print A: A/=2}} : OnLine
Module Online { A=1024&: While A>0 {Print A: A/=2}} : OnLine
</syntaxhighlight>
</lang>


=={{header|m4}}==
=={{header|m4}}==
This ‘loop’ is really a tail recursion, which m4 implementations generally do ''not'' optimize.
This ‘loop’ is really a tail recursion, which m4 implementations generally do ''not'' optimize.
<lang m4>divert(-1)
<syntaxhighlight lang="m4">divert(-1)


define(`loop',
define(`loop',
Line 1,952: Line 2,274:


divert`'dnl
divert`'dnl
loop(1024)dnl</lang>
loop(1024)dnl</syntaxhighlight>
{{out}}
{{out}}
$ m4 loops_while.m4
$ m4 loops_while.m4
Line 1,968: Line 2,290:


=={{header|Make}}==
=={{header|Make}}==
<lang make>NEXT=`expr $* / 2`
<syntaxhighlight lang="make">NEXT=`expr $* / 2`
MAX=10
MAX=10


Line 1,979: Line 2,301:


%-echo:
%-echo:
@echo $*</lang>
@echo $*</syntaxhighlight>


Invoking it
Invoking it
<lang make>|make -f while.mk MAX=1024</lang>
<syntaxhighlight lang="make">|make -f while.mk MAX=1024</syntaxhighlight>


=={{header|Maple}}==
=={{header|Maple}}==
To avoid generating an infinite sequence (1/2, 1/4, 1/8, 1/16, etc.) of fractions after n takes the value 1, we use integer division (iquo) rather than the solidus operation (/).
To avoid generating an infinite sequence (1/2, 1/4, 1/8, 1/16, etc.) of fractions after n takes the value 1, we use integer division (iquo) rather than the solidus operation (/).
<lang Maple>> n := 1024: while n > 0 do print(n); n := iquo(n,2) end:
<syntaxhighlight lang="maple">> n := 1024: while n > 0 do print(n); n := iquo(n,2) end:
1024
1024
512
512
Line 1,997: Line 2,319:
4
4
2
2
1</lang>
1</syntaxhighlight>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function:
Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function:
<lang Mathematica>i = 1024;
<syntaxhighlight lang="mathematica">i = 1024;
While[i > 0,
While[i > 0,
Print[i];
Print[i];
i = Floor[i/2];
i = Floor[i/2];
]</lang>
]</syntaxhighlight>


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
In Matlab (like Octave) the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0. A 'floor' is used to round the number.
In Matlab (like Octave) the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0. A 'floor' is used to round the number.
<lang Matlab>i = 1024;
<syntaxhighlight lang="matlab">i = 1024;
while (i > 0)
while (i > 0)
disp(i);
disp(i);
i = floor(i/2);
i = floor(i/2);
end</lang>
end</syntaxhighlight>


A vectorized version of the code is
A vectorized version of the code is


<lang Matlab> printf('%d\n', 2.^[log2(1024):-1:0]);</lang>
<syntaxhighlight lang="matlab"> printf('%d\n', 2.^[log2(1024):-1:0]);</syntaxhighlight>


=={{header|Maxima}}==
=={{header|Maxima}}==
<lang maxima>block([n], n: 1024, while n > 0 do (print(n), n: quotient(n, 2)));
<syntaxhighlight lang="maxima">block([n], n: 1024, while n > 0 do (print(n), n: quotient(n, 2)));


/* using a C-like loop: divide control variable by two instead of incrementing it */
/* using a C-like loop: divide control variable by two instead of incrementing it */
for n: 1024 next quotient(n, 2) while n > 0 do print(n);</lang>
for n: 1024 next quotient(n, 2) while n > 0 do print(n);</syntaxhighlight>


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<lang maxscript>a = 1024
<syntaxhighlight lang="maxscript">a = 1024
while a > 0 do
while a > 0 do
(
(
print a
print a
a /= 2
a /= 2
)</lang>
)</syntaxhighlight>


=={{header|Metafont}}==
=={{header|Metafont}}==
Line 2,037: Line 2,359:
Metafont has no <tt>while</tt> loop, but it can be "simulated" easily.
Metafont has no <tt>while</tt> loop, but it can be "simulated" easily.


<lang metafont>a := 1024;
<syntaxhighlight lang="metafont">a := 1024;
forever: exitif not (a > 0);
forever: exitif not (a > 0);
show a;
show a;
a := a div 2;
a := a div 2;
endfor</lang>
endfor</syntaxhighlight>

=={{header|Microsoft Small Basic}}==
<lang microsoftsmallbasic>
i = 1024
While i > 0
TextWindow.WriteLine(i)
i = Math.Floor(i / 2)
EndWhile
</lang>


=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.3}}
{{works with|min|0.19.3}}
<lang min>1024 :n (n 0 >) (n puts 2 div @n) while</lang>
<syntaxhighlight lang="min">1024 :n (n 0 >) (n puts 2 div @n) while</syntaxhighlight>


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>i = 1024
<syntaxhighlight lang="miniscript">i = 1024
while i > 0
while i > 0
print i
print i
i = floor(i/2)
i = floor(i/2)
end while</lang>
end while</syntaxhighlight>


{{out}}
{{out}}
Line 2,080: Line 2,393:
=={{header|MIRC Scripting Language}}==
=={{header|MIRC Scripting Language}}==


<lang mirc>alias while_loop {
<syntaxhighlight lang="mirc">alias while_loop {
var %n = 10
var %n = 10
while (%n >= 0) {
while (%n >= 0) {
Line 2,086: Line 2,399:
dec %n
dec %n
}
}
}</lang>
}</syntaxhighlight>


=={{header|MIXAL}}==
=={{header|MIXAL}}==
<syntaxhighlight lang="mixal">
<lang MIXAL>
******************************************
******************************************
* X = M / N WHILE X > 0
* X = M / N WHILE X > 0
Line 2,119: Line 2,432:
HLT
HLT
END START
END START
</syntaxhighlight>
</lang>


=={{header|МК-61/52}}==
=={{header|МК-61/52}}==
<lang>1 0 2 4 П0 ИП0 /-/ x<0 15 ИП0
<syntaxhighlight lang="text">1 0 2 4 П0 ИП0 /-/ x<0 15 ИП0
2 / П0 БП 05 С/П</lang>
2 / П0 БП 05 С/П</syntaxhighlight>


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE DivBy2;
<syntaxhighlight lang="modula2">MODULE DivBy2;
IMPORT InOut;
IMPORT InOut;


Line 2,138: Line 2,451:
i := i DIV 2
i := i DIV 2
END
END
END DivBy2.</lang>
END DivBy2.</syntaxhighlight>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
The usual module code and imports are omitted.
The usual module code and imports are omitted.
<lang modula3>PROCEDURE DivBy2() =
<syntaxhighlight lang="modula3">PROCEDURE DivBy2() =
VAR i: INTEGER := 1024;
VAR i: INTEGER := 1024;
BEGIN
BEGIN
Line 2,150: Line 2,463:
i := i DIV 2;
i := i DIV 2;
END;
END;
END DivBy2;</lang>
END DivBy2;</syntaxhighlight>


=={{header|Monte}}==
=={{header|Monte}}==


<syntaxhighlight lang="monte">
<lang Monte>
var i := 1024
var i := 1024
while (i > 0):
while (i > 0):
traceln(i)
traceln(i)
i //= 2
i //= 2
</syntaxhighlight>
</lang>


=={{header|MOO}}==
=={{header|MOO}}==
<lang moo>i = 1024;
<syntaxhighlight lang="moo">i = 1024;
while (i > 0)
while (i > 0)
player:tell(i);
player:tell(i);
i /= 2;
i /= 2;
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|Morfa}}==
=={{header|Morfa}}==
<lang morfa>
<syntaxhighlight lang="morfa">
import morfa.io.print;
import morfa.io.print;


Line 2,178: Line 2,491:
i /= 2;
i /= 2;
}
}
</syntaxhighlight>
</lang>


=={{header|Nanoquery}}==
=={{header|Nanoquery}}==
<lang nanoquery>$n = 1024
<syntaxhighlight lang="nanoquery">$n = 1024
while ($n > 0)
while ($n > 0)
println $n
println $n
$n = $n/2
$n = $n/2
end while</lang>
end while</syntaxhighlight>


=={{header|Neko}}==
=={{header|Neko}}==
<syntaxhighlight lang="neko">
<lang Neko>
var i = 1024
var i = 1024


Line 2,195: Line 2,508:
i = $idiv(i, 2)
i = $idiv(i, 2)
}
}
</syntaxhighlight>
</lang>


=={{header|Nemerle}}==
=={{header|Nemerle}}==
<lang Nemerle>mutable x = 1024;
<syntaxhighlight lang="nemerle">mutable x = 1024;
while (x > 0)
while (x > 0)
{
{
WriteLine($"$x");
WriteLine($"$x");
x /= 2;
x /= 2;
}</lang>
}</syntaxhighlight>
Or, with immutable types, after Haskell:
Or, with immutable types, after Haskell:
<lang Nemerle>// within another function, eg Main()
<syntaxhighlight lang="nemerle">// within another function, eg Main()
def loop(n : int) : void
def loop(n : int) : void
{
{
Line 2,215: Line 2,528:
}
}
loop(1024)</lang>
loop(1024)</syntaxhighlight>


=={{header|NetRexx}}==
=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
options replace format comments java crossref savelog symbols nobinary


Line 2,228: Line 2,541:
say x_.right(6)
say x_.right(6)
x_ = x_ % 2 -- integer division
x_ = x_ % 2 -- integer division
end</lang>
end</syntaxhighlight>


=={{header|NewLISP}}==
=={{header|NewLISP}}==
<lang NewLISP>(let (i 1024)
<syntaxhighlight lang="newlisp">(let (i 1024)
(while (> i 0)
(while (> i 0)
(println i)
(println i)
(setq i (/ i 2))))</lang>
(setq i (/ i 2))))</syntaxhighlight>


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>var n: int = 1024
<syntaxhighlight lang="nim">var n: int = 1024
while n > 0:
while n > 0:
echo(n)
echo(n)
n = n div 2</lang>
n = n div 2</syntaxhighlight>

=={{header|NS-HUBASIC}}==
<lang NS-HUBASIC>10 I=1024
20 IF I=0 THEN END
30 PRINT I
40 I=I/2
50 GOTO 20</lang>


=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
The usual module code and imports are ommited.
The usual module code and imports are ommited.
<lang oberon2>PROCEDURE DivBy2*();
<syntaxhighlight lang="oberon2">PROCEDURE DivBy2*();
VAR i: INTEGER;
VAR i: INTEGER;
BEGIN
BEGIN
Line 2,260: Line 2,566:
i := i DIV 2;
i := i DIV 2;
END;
END;
END DivBy2;</lang>
END DivBy2;</syntaxhighlight>


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>i := 1024;
<syntaxhighlight lang="objeck">i := 1024;
while(i > 0) {
while(i > 0) {
i->PrintLine();
i->PrintLine();
i /= 2;
i /= 2;
};</lang>
};</syntaxhighlight>


=={{header|ObjectIcon}}==
=={{header|ObjectIcon}}==
Line 2,273: Line 2,579:
An important thing here is the test for termination is a succeed-or-fail operation, rather than a boolean expression. No boolean value is written to the output; rather, the whole statement ''fails'' when '''n''' reaches zero. The expression '''0 < n''' either returns '''n''' or ''fails''.
An important thing here is the test for termination is a succeed-or-fail operation, rather than a boolean expression. No boolean value is written to the output; rather, the whole statement ''fails'' when '''n''' reaches zero. The expression '''0 < n''' either returns '''n''' or ''fails''.


<lang objecticon>import io
<syntaxhighlight lang="objecticon">import io


procedure main()
procedure main()
Line 2,279: Line 2,585:
n := 1024
n := 1024
while n := write(0 < n) / 2
while n := write(0 < n) / 2
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 2,296: Line 2,602:


The following also works, and shows a way to avoid having to code the method of writing the output into the loop itself.
The following also works, and shows a way to avoid having to code the method of writing the output into the loop itself.
<lang objecticon>import io
<syntaxhighlight lang="objecticon">import io


procedure main()
procedure main()
Line 2,306: Line 2,612:
procedure generator(n)
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
while 0 < n do { suspend n; n /:= 2 }
end</lang>
end</syntaxhighlight>


To show what I mean, let’s do some (merely illustrative) loop-unrolling where the output gets written.
To show what I mean, let’s do some (merely illustrative) loop-unrolling where the output gets written.
<lang objecticon>import io
<syntaxhighlight lang="objecticon">import io


procedure main()
procedure main()
Line 2,326: Line 2,632:
procedure generator(n)
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
while 0 < n do { suspend n; n /:= 2 }
end</lang>
end</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml>let n = ref 1024;;
<syntaxhighlight 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;;</lang>
done;;</syntaxhighlight>


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:
<lang ocaml>let rec loop n =
<syntaxhighlight 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</lang>
in loop 1024</syntaxhighlight>


=={{header|Octave}}==
=={{header|Octave}}==
<lang octave>i = 1024;
<syntaxhighlight lang="octave">i = 1024;
while (i > 0)
while (i > 0)
disp(i)
disp(i)
i = floor(i/2);
i = floor(i/2);
endwhile</lang>
endwhile</syntaxhighlight>


The usage of the type int32 is not convenient, since the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0.
The usage of the type int32 is not convenient, since the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0.

=={{header|Odin}}==

Odin only has one loop type: for

<syntaxhighlight lang="odin">
package main

import "core:fmt"

main :: proc() {
for i := 1024 ; i > 0 ; i /= 2 {
fmt.println(i)
}
}
</syntaxhighlight>


=={{header|Oforth}}==
=={{header|Oforth}}==


<lang Oforth>1024 while ( dup ) [ dup println 2 / ]</lang>
<syntaxhighlight lang="oforth">1024 while ( dup ) [ dup println 2 / ]</syntaxhighlight>


=={{header|OOC}}==
=={{header|OOC}}==
<lang ooc>
<syntaxhighlight lang="ooc">
main: func {
main: func {
value := 1024
value := 1024
Line 2,365: Line 2,687:
}
}
}
}
</syntaxhighlight>
</lang>


=={{header|Oz}}==
=={{header|Oz}}==
Oz' for-loop can be used in a C-like manner:
Oz' for-loop can be used in a C-like manner:
<lang oz>for I in 1024; I>0; I div 2 do
<syntaxhighlight lang="oz">for I in 1024; I>0; I div 2 do
{Show I}
{Show I}
end</lang>
end</syntaxhighlight>


Alternatively, we can use the <code>while</code> feature of the for-loop with a mutable variable:
Alternatively, we can use the <code>while</code> feature of the for-loop with a mutable variable:
<lang oz>declare
<syntaxhighlight lang="oz">declare
I = {NewCell 1024}
I = {NewCell 1024}
in
in
Line 2,380: Line 2,702:
{Show @I}
{Show @I}
I := @I div 2
I := @I div 2
end</lang>
end</syntaxhighlight>


=={{header|Panda}}==
=={{header|Panda}}==
Panda doesn't have explicit loops, instead we solve it by using the transitive closure operator. It applies a function to each successive value, each unique value is outputted. Our function halves, we make sure that the result is greater than 0 and add newline.
Panda doesn't have explicit loops, instead we solve it by using the transitive closure operator. It applies a function to each successive value, each unique value is outputted. Our function halves, we make sure that the result is greater than 0 and add newline.
<lang panda>fun half(a) type integer->integer a.divide(2)
<syntaxhighlight lang="panda">fun half(a) type integer->integer a.divide(2)
1024.trans(func:half).gt(0) nl
1024.trans(func:half).gt(0) nl
</syntaxhighlight>
</lang>


=={{header|Panoramic}}==
=={{header|Panoramic}}==
<lang Panoramic>dim x%:rem an integer
<syntaxhighlight lang="panoramic">dim x%:rem an integer


x%=1024
x%=1024
Line 2,402: Line 2,724:
rem output starts with 1024 and ends with 1.
rem output starts with 1024 and ends with 1.


terminate</lang>
terminate</syntaxhighlight>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>n=1024;
<syntaxhighlight lang="parigp">n=1024;
while(n,
while(n,
print(n);
print(n);
n/=2
n/=2
);</lang>
);</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>program divby2(output);
<syntaxhighlight lang="pascal">program divby2(output);


var
var
Line 2,424: Line 2,746:
i := i div 2
i := i div 2
end
end
end.</lang>
end.</syntaxhighlight>


=={{header|PeopleCode}}==
=={{header|PeopleCode}}==
<syntaxhighlight lang="peoplecode">
<lang PeopleCode>
Local string &CRLF;
Local string &CRLF;
Local number &LoopNumber;
Local number &LoopNumber;
Line 2,437: Line 2,759:
&LoopNumber = &LoopNumber / 2;
&LoopNumber = &LoopNumber / 2;
End-While;
End-While;
</syntaxhighlight>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>my $n = 1024;
<syntaxhighlight lang="perl">my $n = 1024;
while($n){
while($n){
print "$n\n";
print "$n\n";
$n = int $n / 2;
$n = int $n / 2;
}</lang>
}</syntaxhighlight>


or written as a for-loop and using the bit-shift operator
or written as a for-loop and using the bit-shift operator


<lang perl>for(my $n = 1024; $n > 0; $n >>= 1){
<syntaxhighlight lang="perl">for(my $n = 1024; $n > 0; $n >>= 1){
print "$n\n";
print "$n\n";
}</lang>
}</syntaxhighlight>


<code>until (''condition'')</code> is equivalent to <code>while (not ''condition'')</code>.
<code>until (''condition'')</code> is equivalent to <code>while (not ''condition'')</code>.


<lang perl>my $n = 1024;
<syntaxhighlight lang="perl">my $n = 1024;
until($n == 0){
until($n == 0){
print "$n\n";
print "$n\n";
$n = int $n / 2;
$n = int $n / 2;
}</lang>
}</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1024</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1024</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
Line 2,467: Line 2,789:
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (see note)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
note: using i=i/2 would iterate over 1000 times until i is 4.94e-324 before the final division made it 0, if it didn't typecheck when it got set to 0.5
note: using i=i/2 would iterate over 1000 times until i is 4.94e-324 before the final division made it 0, if it didn't typecheck when it got set to 0.5

=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/While
by Galileo, 11/2022 #/

include ..\Utilitys.pmt

1024 dup while dup ? 2 / int dup endwhile</syntaxhighlight>
{{out}}
<pre>1024
512
256
128
64
32
16
8
4
2
1

=== Press any key to exit ===</pre>


=={{header|PHL}}==
=={{header|PHL}}==


<lang phl>var i = 1024;
<syntaxhighlight lang="phl">var i = 1024;
while (i > 0) {
while (i > 0) {
printf("%i\n", i);
printf("%i\n", i);
i = i/2;
i = i/2;
}</lang>
}</syntaxhighlight>


=={{header|PHP}}==
=={{header|PHP}}==
<lang php>$i = 1024;
<syntaxhighlight lang="php">$i = 1024;
while ($i > 0) {
while ($i > 0) {
echo "$i\n";
echo "$i\n";
$i >>= 1;
$i >>= 1;
}</lang>
}</syntaxhighlight>


=={{header|Picat}}==
=={{header|Picat}}==
While loop
While loop
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
N = 1024,
N = 1024,
while (N > 0)
while (N > 0)
println(N),
println(N),
N := N // 2
N := N // 2
end.</lang>
end.</syntaxhighlight>


Recursion
Recursion
{{trans|Prolog}}
{{trans|Prolog}}
<lang Picat>go2 =>
<syntaxhighlight lang="picat">go2 =>
while_loop(1024).
while_loop(1024).


Line 2,502: Line 2,846:
while_loop(N) =>
while_loop(N) =>
println(N),
println(N),
while_loop(N//2).</lang>
while_loop(N//2).</syntaxhighlight>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(let N 1024
<syntaxhighlight lang="picolisp">(let N 1024
(while (gt0 N)
(while (gt0 N)
(println N)
(println N)
(setq N (/ N 2)) ) )</lang>
(setq N (/ N 2)) ) )</syntaxhighlight>


=={{header|Pike}}==
=={{header|Pike}}==
<lang pike>int main(){
<syntaxhighlight lang="pike">int main(){
int i = 1024;
int i = 1024;
while(i > 0){
while(i > 0){
Line 2,518: Line 2,862:
i = i / 2;
i = i / 2;
}
}
}</lang>
}</syntaxhighlight>

=={{header|PL/0}}==
<syntaxhighlight lang="pascal">
var i;
begin
i := 1024;
while i > 0 do
begin
! i;
i := i / 2
end;
end.
</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>declare i fixed binary initial (1024);
<syntaxhighlight lang="pl/i">declare i fixed binary initial (1024);


do while (i>0);
do while (i>0);
put skip list (i);
put skip list (i);
i = i / 2;
i = i / 2;
end;</lang>
end;</syntaxhighlight>


=={{header|PL/SQL}}==
=={{header|PL/SQL}}==
{{works with|Oracle}}
{{works with|Oracle}}
<lang plsql>
<syntaxhighlight lang="plsql">
set serveroutput on
set serveroutput on
declare
declare
Line 2,541: Line 2,912:
end;
end;
/
/
</syntaxhighlight>
</lang>


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Show the halvings of 1024.
Show the halvings of 1024.
Line 2,555: Line 2,926:
Write the string to the console.
Write the string to the console.
Divide the number by 2.
Divide the number by 2.
Repeat.</lang>
Repeat.</syntaxhighlight>


=={{header|Pop11}}==
=={{header|Pop11}}==
<lang pop11>lvars i = 1024;
<syntaxhighlight lang="pop11">lvars i = 1024;
while i > 0 do
while i > 0 do
printf(i, '%p\n');
printf(i, '%p\n');
i div 2 -> i;
i div 2 -> i;
endwhile;</lang>
endwhile;</syntaxhighlight>


=={{header|PostScript}}==
=={{header|PostScript}}==
PostScript has no real <code>while</code> loop,
PostScript has no real <code>while</code> loop,
but it can easily be created with an endless loop and a check at the beginning:
but it can easily be created with an endless loop and a check at the beginning:
<lang postscript>1024
<syntaxhighlight lang="postscript">1024
{
{
dup 0 le % check whether still greater than 0
dup 0 le % check whether still greater than 0
Line 2,575: Line 2,946:
2 idiv % divide by two
2 idiv % divide by two
}
}
loop</lang>
loop</syntaxhighlight>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<lang powershell>[int]$i = 1024
<syntaxhighlight lang="powershell">[int]$i = 1024
while ($i -gt 0) {
while ($i -gt 0) {
$i
$i
$i /= 2
$i /= 2
}</lang>
}</syntaxhighlight>


=={{header|Prolog}}==
=={{header|Prolog}}==
<lang prolog>while(0) :- !.
<syntaxhighlight lang="prolog">while(0) :- !.
while(X) :-
while(X) :-
writeln(X),
writeln(X),
X1 is X // 2,
X1 is X // 2,
while(X1).</lang>
while(X1).</syntaxhighlight>


Start the calculation at a top-level like this:
Start the calculation at a top-level like this:


<lang prolog>?- while(1024).</lang>
<syntaxhighlight lang="prolog">?- while(1024).</syntaxhighlight>

=={{header|PureBasic}}==
<lang PureBasic>If OpenConsole()
x.i = 1024
While x > 0
PrintN(Str(x))
x / 2
Wend

Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</lang>


=={{header|Python}}==
=={{header|Python}}==
<lang python>n = 1024
<syntaxhighlight lang="python">n = 1024
while n > 0:
while n > 0:
print n
print n
n //= 2</lang>
n //= 2</syntaxhighlight>

=={{header|QB64}}==
<lang qb64>Dim n As Integer
n = 1024
While n > 0
Print n
n = n \ 2
Wend</lang>


=={{header|Quackery}}==
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">1024

<lang Quackery>1024
[ dup 0 > while
[ dup 0 > while
dup echo cr 2 /
dup echo cr 2 /
again ]
again ]
drop</lang>
drop</syntaxhighlight>


{{Out}}
{{Out}}
Line 2,648: Line 2,996:


=={{header|R}}==
=={{header|R}}==
<lang R>i <- 1024L
<syntaxhighlight lang="r">i <- 1024L
while(i > 0)
while(i > 0)
{
{
print(i)
print(i)
i <- i %/% 2
i <- i %/% 2
}</lang>
}</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
===Loop/When===
===Loop/When===
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(let loop ([n 1024])
(let loop ([n 1024])
(when (positive? n)
(when (positive? n)
(displayln n)
(displayln n)
(loop (quotient n 2))))</lang>
(loop (quotient n 2))))</syntaxhighlight>


===Macro===
===Macro===
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define-syntax-rule (while condition body ...)
(define-syntax-rule (while condition body ...)
(let loop ()
(let loop ()
Line 2,674: Line 3,022:
(while (positive? n)
(while (positive? n)
(displayln n)
(displayln n)
(set! n (sub1 n)))</lang>
(set! n (sub1 n)))</syntaxhighlight>


=={{header|Raku}}==
=={{header|Raku}}==
Line 2,680: Line 3,028:


Here is a straightforward translation of the task description:
Here is a straightforward translation of the task description:
<lang perl6>my $n = 1024; while $n > 0 { say $n; $n div= 2 }</lang>
<syntaxhighlight lang="raku" line>my $n = 1024; while $n > 0 { say $n; $n div= 2 }</syntaxhighlight>


The same thing with a C-style loop and a bitwise shift operator:
The same thing with a C-style loop and a bitwise shift operator:
<lang perl6>loop (my $n = 1024; $n > 0; $n +>= 1) { say $n }</lang>
<syntaxhighlight lang="raku" line>loop (my $n = 1024; $n > 0; $n +>= 1) { say $n }</syntaxhighlight>


And here's how you'd <em>really</em> write it, using a sequence operator that intuits the division for you:
And here's how you'd <em>really</em> write it, using a sequence operator that intuits the division for you:


<lang perl6>.say for 1024, 512, 256 ... 1</lang>
<syntaxhighlight lang="raku" line>.say for 1024, 512, 256 ... 1</syntaxhighlight>


=={{header|REBOL}}==
=={{header|REBOL}}==
<lang REBOL>REBOL [
<syntaxhighlight lang="rebol">REBOL [
Title: "Loop/While"
Title: "Loop/While"
URL: http://rosettacode.org/wiki/Loop/While
URL: http://rosettacode.org/wiki/Loop/While
Line 2,699: Line 3,047:
print value
print value
value: to-integer value / 2
value: to-integer value / 2
]</lang>
]</syntaxhighlight>




=={{header|ReScript}}==
=={{header|ReScript}}==
<lang ReScript>let n = ref(1024)
<syntaxhighlight lang="rescript">let n = ref(1024)
while n.contents > 0 {
while n.contents > 0 {
Js.log(n.contents)
Js.log(n.contents)
n := n.contents / 2
n := n.contents / 2
}</lang>
}</syntaxhighlight>


=={{header|Retro}}==
=={{header|Retro}}==
<lang Retro>1024 [ cr &putn sip 2 / dup ] while</lang>
<syntaxhighlight lang="retro">1024 [ cr &putn sip 2 / dup ] while</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
===version 1, simple===
===version 1, simple===
<lang rexx>/*REXX program demonstrates a DO WHILE with index reduction construct.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO WHILE with index reduction construct.*/
j=1024 /*define the initial value of J.*/
j=1024 /*define the initial value of J.*/
do while j>0 /*test if made at the top of DO.*/
do while j>0 /*test if made at the top of DO.*/
Line 2,720: Line 3,068:
j=j%2 /*in REXX, % is integer division.*/
j=j%2 /*in REXX, % is integer division.*/
end
end
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,741: Line 3,089:
::::: '''DO WHILE x\==0'''
::::: '''DO WHILE x\==0'''
but that wouldn't be compliant with the wording of the task.
but that wouldn't be compliant with the wording of the task.
<lang rexx>/*REXX program demonstrates a DO WHILE with index reduction construct.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO WHILE with index reduction construct.*/
x=1024 /*define the initial value of X.*/
x=1024 /*define the initial value of X.*/
do while x>0 /*test if made at the top of DO.*/
do while x>0 /*test if made at the top of DO.*/
Line 2,747: Line 3,095:
x=x%2 /*in REXX, % is integer division.*/
x=x%2 /*in REXX, % is integer division.*/
end
end
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,764: Line 3,112:


===version 3, faster WHILE comparison===
===version 3, faster WHILE comparison===
<lang rexx>/*REXX program demonstrates a DO WHILE with index reduction construct.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO WHILE with index reduction construct.*/
x=1024 /*define the initial value of X.*/
x=1024 /*define the initial value of X.*/
do while x>>0 /*this is an exact comparison. */
do while x>>0 /*this is an exact comparison. */
Line 2,770: Line 3,118:
x=x%2 /*in REXX, % is integer division.*/
x=x%2 /*in REXX, % is integer division.*/
end
end
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</syntaxhighlight>
'''output''' is the same as version 2.
'''output''' is the same as version 2.
<br>
<br>


===version 4, index reduction===
===version 4, index reduction===
<lang rexx>/*REXX program demonstrates a DO WHILE with index reduction construct.*/
<syntaxhighlight lang="rexx">/*REXX program demonstrates a DO WHILE with index reduction construct.*/
/* [↓] note: BY defaults to 1*/
/* [↓] note: BY defaults to 1*/
do j=1024 by 0 while j>>0 /*this is an exact comparison. */
do j=1024 by 0 while j>>0 /*this is an exact comparison. */
Line 2,781: Line 3,129:
j=j%2 /*in REXX, % is integer division.*/
j=j%2 /*in REXX, % is integer division.*/
end
end
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done.*/</syntaxhighlight>
'''output''' is the same as version 2.
'''output''' is the same as version 2.
<br><br>
<br><br>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
i = 1024
i = 1024
while i > 0
while i > 0
Line 2,792: Line 3,140:
i = floor(i / 2)
i = floor(i / 2)
end
end
</syntaxhighlight>
</lang>

=={{header|Rockstar}}==
<syntaxhighlight lang="rockstar">The sky is a television on fire.
The floor is ornamental.
The table is hydrochloric.
While the sky is higher than the floor,
Shout the sky.
Put the sky over the table into the sky.
(Rockstar is known for doing that JavaScript quirk, since all numbers are floats, but due to it having the same limits as JavaScript and the most popular interpreter being JS-based, the loop is finite, yet you don't get the result you would get in something like Python.)</syntaxhighlight>
=={{header|RPL}}==
≪ 1024
'''WHILE''' DUP '''REPEAT'''
DUP 1 DISP
2 /
'''END'''
DROP


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>i = 1024
<syntaxhighlight lang="ruby">i = 1024
while i > 0 do
while i > 0 do
puts i
puts i
i /= 2
i /= 2
end</lang>
end</syntaxhighlight>
The above can be written in one statement:
The above can be written in one statement:
<lang ruby>puts i = 1024
<syntaxhighlight lang="ruby">puts i = 1024
puts i /= 2 while i > 0</lang>
puts i /= 2 while i > 0</syntaxhighlight>


<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>.
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>.


<lang ruby>i = 1024
<syntaxhighlight lang="ruby">i = 1024
until i <= 0 do
until i <= 0 do
puts i
puts i
i /= 2
i /= 2
end</lang>
end</syntaxhighlight>

=={{header|Run BASIC}}==
<lang runbasic>i = 1024
while i > 0
print i
i = int(i / 2)
wend
end</lang>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn main() {
<syntaxhighlight lang="rust">fn main() {
let mut n: i32 = 1024;
let mut n: i32 = 1024;
while n > 0 {
while n > 0 {
Line 2,827: Line 3,184:
n /= 2;
n /= 2;
}
}
}</lang>
}</syntaxhighlight>


=={{header|SAS}}==
=={{header|SAS}}==
<lang sas>data _null_;
<syntaxhighlight lang="sas">data _null_;
n=1024;
n=1024;
do while(n>0);
do while(n>0);
Line 2,836: Line 3,193:
n=int(n/2);
n=int(n/2);
end;
end;
run;</lang>
run;</syntaxhighlight>


=={{header|Sather}}==
=={{header|Sather}}==
<lang sather>class MAIN is
<syntaxhighlight lang="sather">class MAIN is
main is
main is
i ::= 1024;
i ::= 1024;
Line 2,847: Line 3,204:
end;
end;
end;
end;
end;</lang>
end;</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}
{{libheader|Scala}}
===Imperative===
===Imperative===
<lang scala>var i = 1024
<syntaxhighlight lang="scala">var i = 1024
while (i > 0) {
while (i > 0) {
println(i)
println(i)
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


===Tail recursive===
===Tail recursive===
<lang scala> @tailrec
<syntaxhighlight lang="scala"> @tailrec
def loop(iter: Int) {
def loop(iter: Int) {
if (iter > 0) {
if (iter > 0) {
Line 2,866: Line 3,223:
}
}
}
}
loop(1024)</lang>
loop(1024)</syntaxhighlight>


===Iterator===
===Iterator===
<lang scala> def loop = new Iterator[Int] {
<syntaxhighlight lang="scala"> def loop = new Iterator[Int] {
var i = 1024
var i = 1024
def hasNext = i > 0
def hasNext = i > 0
def next(): Int = { val tmp = i; i = i / 2; tmp }
def next(): Int = { val tmp = i; i = i / 2; tmp }
}
}
loop.foreach(println(_))</lang>
loop.foreach(println(_))</syntaxhighlight>


===Stream===
===Stream===
Finite stream (1024..0) filtered by takeWhile (1024..1).
Finite stream (1024..0) filtered by takeWhile (1024..1).
<lang scala> def loop(i: Int): Stream[Int] = i #:: (if (i > 0) loop(i / 2) else Stream.empty)
<syntaxhighlight lang="scala"> def loop(i: Int): Stream[Int] = i #:: (if (i > 0) loop(i / 2) else Stream.empty)
loop(1024).takeWhile(_ > 0).foreach(println(_))</lang>
loop(1024).takeWhile(_ > 0).foreach(println(_))</syntaxhighlight>


=={{header|Scheme}}==
=={{header|Scheme}}==
<lang scheme>(do ((n 1024 (quotient n 2)))
<syntaxhighlight lang="scheme">(do ((n 1024 (quotient n 2)))
((<= n 0))
((<= n 0))
(display n)
(display n)
(newline))</lang>
(newline))</syntaxhighlight>


=={{header|Scilab}}==
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
{{works with|Scilab|5.5.1}}
<lang>i=1024
<syntaxhighlight lang="text">i=1024
while i>0
while i>0
printf("%4d\n",i)
printf("%4d\n",i)
i=int(i/2)
i=int(i/2)
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>1024
<pre>1024
Line 2,908: Line 3,265:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: main is func
const proc: main is func
Line 2,918: Line 3,275:
i := i div 2
i := i div 2
end while;
end while;
end func;</lang>
end func;</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
<lang sensetalk>put 1024 into x
<syntaxhighlight lang="sensetalk">put 1024 into x
log x
log x
Repeat until x = 1
Repeat until x = 1
divide x by 2
divide x by 2
log x
log x
End repeat</lang>
End repeat</syntaxhighlight>


=={{header|SETL}}==
=={{header|SETL}}==
<lang ada>n := 1024;
<syntaxhighlight lang="ada">n := 1024;
while n > 0 loop
while n > 0 loop
print( n );
print( n );
n := n div 2;
n := n div 2;
end loop;</lang>
end loop;</syntaxhighlight>


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var i = 1024
<syntaxhighlight lang="ruby">var i = 1024
while (i > 0) {
while (i > 0) {
say i
say i
i //= 2
i //= 2
}</lang>
}</syntaxhighlight>


=={{header|Simula}}==
=={{header|Simula}}==
{{works with|SIMULA-67}}
{{works with|SIMULA-67}}
<lang simula>begin
<syntaxhighlight lang="simula">begin
integer i;
integer i;
i:=1024;
i:=1024;
Line 2,952: Line 3,323:
i:=i//2-1
i:=i//2-1
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
 1024  511  254  126   62   30   14    6    2 
 1024  511  254  126   62   30   14    6    2 
</pre>
</pre>

=={{header|Sinclair ZX81 BASIC}}==
The distinctive thing about a <code>while</code> loop is that the conditional test happens before the loop body, not after—so that the code in the loop may be executed zero times.

Since we have no integer type, we floor the result of the division each time.
<lang basic>10 LET I=1024
20 IF I=0 THEN GOTO 60
30 PRINT I
40 LET I=INT (I/2)
50 GOTO 20</lang>


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>#n := 1024.
<syntaxhighlight lang="slate">#n := 1024.
[n isPositive] whileTrue:
[n isPositive] whileTrue:
[inform: number printString.
[inform: number printString.
n := n // 2]</lang>
n := n // 2]</syntaxhighlight>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
The Block (aka lambda closure) class provides a number of loop messages; with test at begin, test at end and with exit (break).
The Block (aka lambda closure) class provides a number of loop messages; with test at begin, test at end and with exit (break).


<lang smalltalk >[s atEnd] whileFalse: [s next. ...].
<syntaxhighlight lang="smalltalk ">[s atEnd] whileFalse: [s next. ...].
[foo notNil] whileTrue: [s next. ...].
[foo notNil] whileTrue: [s next. ...].
[...] doWhile: [ ... someBooleanExpression ].
[...] doWhile: [ ... someBooleanExpression ].
[...] doUntil: [ ... someBooleanExpression ].
[...] doUntil: [ ... someBooleanExpression ].
[:exit | ... cold ifTrue:[exit value]. ...] loopWithExit</lang>
[:exit | ... cold ifTrue:[exit value]. ...] loopWithExit</syntaxhighlight>


Examples:
Examples:
<lang smalltalk>number := 1024.
<syntaxhighlight lang="smalltalk">number := 1024.
[ number > 0 ] whileTrue:
[ number > 0 ] whileTrue:
[ Transcript print: number; nl.
[ Transcript print: number; nl.
number := number // 2 ]</lang>
number := number // 2 ]</syntaxhighlight>


<lang smalltalk>number := 1024.
<syntaxhighlight lang="smalltalk">number := 1024.
[ number <= 0 ] whileFalse:
[ number <= 0 ] whileFalse:
[ Transcript print: number; nl.
[ Transcript print: number; nl.
number := number // 2 ]</lang>
number := number // 2 ]</syntaxhighlight>


=={{header|Sparkling}}==
=={{header|Sparkling}}==
<lang sparkling>var i = 1024;
<syntaxhighlight lang="sparkling">var i = 1024;
while i > 0 {
while i > 0 {
print(i);
print(i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>


=={{header|Spin}}==
=={{header|Spin}}==
Line 3,006: Line 3,367:
{{works with|HomeSpun}}
{{works with|HomeSpun}}
{{works with|OpenSpin}}
{{works with|OpenSpin}}
<lang spin>con
<syntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
_clkfreq = 80_000_000
Line 3,024: Line 3,385:
waitcnt(_clkfreq + cnt)
waitcnt(_clkfreq + cnt)
ser.stop
ser.stop
cogstop(0)</lang>
cogstop(0)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,031: Line 3,392:


=={{header|SPL}}==
=={{header|SPL}}==
<lang spl>n = 1024
<syntaxhighlight lang="spl">n = 1024
>
>
#.output(n)
#.output(n)
n /= 2
n /= 2
< n!<1</lang>
< n!<1</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,054: Line 3,415:
{{works with|Db2 LUW}} version 9.7 or higher.
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
With SQL PL:
<lang sql pl>
<syntaxhighlight lang="sql pl">
--#SET TERMINATOR @
--#SET TERMINATOR @


Line 3,067: Line 3,428:
END WHILE Loop;
END WHILE Loop;
END @
END @
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,092: Line 3,453:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<lang sml>val n = ref 1024;
<syntaxhighlight lang="sml">val n = ref 1024;
while !n > 0 do (
while !n > 0 do (
print (Int.toString (!n) ^ "\n");
print (Int.toString (!n) ^ "\n");
n := !n div 2
n := !n div 2
)</lang>
)</syntaxhighlight>


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:
<lang sml>let
<syntaxhighlight lang="sml">let
fun loop n =
fun loop n =
if n > 0 then (
if n > 0 then (
Line 3,107: Line 3,468:
in
in
loop 1024
loop 1024
end</lang>
end</syntaxhighlight>


=={{header|Stata}}==
=={{header|Stata}}==
<lang stata>local n=1024
<syntaxhighlight lang="stata">local n=1024
while `n'>0 {
while `n'>0 {
display `n'
display `n'
local n=floor(`n'/2)
local n=floor(`n'/2)
}</lang>
}</syntaxhighlight>


=={{header|Suneido}}==
=={{header|Suneido}}==
<lang Suneido>i = 1024
<syntaxhighlight lang="suneido">i = 1024
while (i > 0)
while (i > 0)
{
{
Print(i)
Print(i)
i = (i / 2).Floor()
i = (i / 2).Floor()
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1024
<pre>1024
Line 3,137: Line 3,498:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>var i = 1024
<syntaxhighlight lang="swift">var i = 1024
while i > 0 {
while i > 0 {
println(i)
println(i)
i /= 2
i /= 2
}</lang>
}</syntaxhighlight>


=={{header|Tailspin}}==
=={{header|Tailspin}}==
In Tailspin you can loop by sending a value back to the matchers (by "-> #"). Depending on how you set that up, you create different loops.
In Tailspin you can loop by sending a value back to the matchers (by "-> #"). Depending on how you set that up, you create different loops.
<lang tailspin>
<syntaxhighlight lang="tailspin">
1024 -> \(
1024 -> \(
<0~..> '$;$#10;' -> !OUT::write
<0~..> '$;$#10;' -> !OUT::write
$ ~/ 2 -> #
$ ~/ 2 -> #
\) -> !VOID
\) -> !VOID
</syntaxhighlight>
</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>set i 1024
<syntaxhighlight lang="tcl">set i 1024
while {$i > 0} {
while {$i > 0} {
puts $i
puts $i
set i [expr {$i / 2}]
set i [expr {$i / 2}]
}</lang>
}</syntaxhighlight>


=={{header|TeX}}==
=={{header|TeX}}==


<lang TeX>\newcount\rosetta
<syntaxhighlight lang="tex">\newcount\rosetta
\rosetta=1024
\rosetta=1024
\loop
\loop
Line 3,168: Line 3,529:
\ifnum\rosetta > 0
\ifnum\rosetta > 0
\repeat
\repeat
\end</lang>
\end</syntaxhighlight>

=={{header|TI-83 BASIC}}==

<lang ti83b>1024→I
While I>0
Disp I
I/2→I
End
</lang>

=={{header|TI-89 BASIC}}==

<lang ti89b>Local i
1024 → i
While i > 0
Disp i
intDiv(i, 2) → i
EndWhile</lang>


=={{header|TorqueScript}}==
=={{header|TorqueScript}}==
This has to make use of mFloor because torque has automatic type shuffling,
This has to make use of mFloor because torque has automatic type shuffling,
causing an infiniteloop.
causing an infiniteloop.
<lang Torque>%num = 1024;
<syntaxhighlight lang="torque">%num = 1024;
while(%num > 0)
while(%num > 0)
{
{
echo(%num);
echo(%num);
%num = mFloor(%num / 2);
%num = mFloor(%num / 2);
}</lang>
}</syntaxhighlight>


=={{header|Transact-SQL}}==
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
DECLARE @i INT = 1024;
DECLARE @i INT = 1024;
WHILE @i >0
WHILE @i >0
Line 3,206: Line 3,549:
SET @i = @i / 2;
SET @i = @i / 2;
END;
END;
</syntaxhighlight>
</lang>


=={{header|Trith}}==
=={{header|Trith}}==
<lang trith>1024 [dup print 2 / floor] [dup 0 >] while drop</lang>
<syntaxhighlight lang="trith">1024 [dup print 2 / floor] [dup 0 >] while drop</syntaxhighlight>
<lang trith>1024 [dup print 1 shr] [dup 0 >] while drop</lang>
<syntaxhighlight lang="trith">1024 [dup print 1 shr] [dup 0 >] while drop</syntaxhighlight>


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>$$ MODE TUSCRIPT
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
i=1024
i=1024
LOOP
LOOP
Line 3,219: Line 3,562:
i=i/2
i=i/2
IF (i==0) EXIT
IF (i==0) EXIT
ENDLOOP</lang>
ENDLOOP</syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 3,240: Line 3,583:
=={{header|Uniface}}==
=={{header|Uniface}}==


<lang Uniface>variables
<syntaxhighlight lang="uniface">variables
numeric I
numeric I
endvariables
endvariables
Line 3,248: Line 3,591:
putmess I
putmess I
I = (I/2)[trunc]
I = (I/2)[trunc]
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Bourne Again SHell}}
<lang bash>x=1024
<syntaxhighlight lang="bash">x=1024
while [[ $x -gt 0 ]]; do
while [[ $x -gt 0 ]]; do
echo $x
echo $x
x=$(( $x/2 ))
x=$(( $x/2 ))
done</lang>
done</syntaxhighlight>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
<lang bash>(echo 1024>p.res;tail -f p.res) | while read a ; do
<syntaxhighlight lang="bash">(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
test $a -gt 0 && (expr $a / 2 >> p.res ; echo $a) || exit 0
done</lang>
done</syntaxhighlight>


=={{header|Ursa}}==
=={{header|Ursa}}==
<lang ursa>decl int n
<syntaxhighlight lang="ursa">decl int n
set n 1024
set n 1024


Line 3,270: Line 3,613:
out n endl console
out n endl console
set n (int (/ n 2))
set n (int (/ n 2))
end while</lang>
end while</syntaxhighlight>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 3,288: Line 3,631:
truncated half of the head of its argument with a copy of the whole argument.
truncated half of the head of its argument with a copy of the whole argument.
The main program takes care of list reversal and formatting.
The main program takes care of list reversal and formatting.
<lang Ursala>#import nat
<syntaxhighlight lang="ursala">#import nat


g = ~&h-> ^C/half@h ~&
g = ~&h-> ^C/half@h ~&
Line 3,294: Line 3,637:
#show+
#show+


main = %nP*=tx g <1024></lang>
main = %nP*=tx g <1024></syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 3,312: Line 3,655:
The same output is produced by the following main program
The same output is produced by the following main program
using bit manipulation.
using bit manipulation.
<lang Ursala>main = %nP*=tK33 1024</lang>
<syntaxhighlight lang="ursala">main = %nP*=tK33 1024</syntaxhighlight>


=={{header|V}}==
=={{header|V}}==
<lang v>1024 [0 >] [
<syntaxhighlight lang="v">1024 [0 >] [
dup puts
dup puts
2 / >int
2 / >int
] while</lang>
] while</syntaxhighlight>


=={{header|Vala}}==
=={{header|Vala}}==
<lang vala>int i = 1024;
<syntaxhighlight lang="vala">int i = 1024;
while (i > 0) {
while (i > 0) {
stdout.printf("%d\n", i);
stdout.printf("%d\n", i);
i /= 2;
i /= 2;
}</lang>
}</syntaxhighlight>

=={{header|VBA}}==
<lang VB>Public Sub LoopsWhile()
Dim value As Integer
value = 1024
Do While value > 0
Debug.Print value
value = value / 2
Loop
End Sub</lang>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==
<lang vedit>#1 = 1024
<syntaxhighlight lang="vedit">#1 = 1024
while (#1 > 0) {
while (#1 > 0) {
Num_Type(#1)
Num_Type(#1)
#1 /= 2
#1 /= 2
}</lang>
}</syntaxhighlight>
or with for loop:
or with for loop:
<lang vedit>for (#1 = 1024; #1 > 0; #1 /= 2) {
<syntaxhighlight lang="vedit">for (#1 = 1024; #1 > 0; #1 /= 2) {
Num_Type(#1)
Num_Type(#1)
}</lang>
}</syntaxhighlight>


=={{header|Verbexx}}==
=={{header|Verbexx}}==
<lang verbexx>// Basic @LOOP while: verb
<syntaxhighlight lang="verbexx">// Basic @LOOP while: verb


@LOOP init:{@VAR n = 1024} while:(n > 0) next:{n /= 2}
@LOOP init:{@VAR n = 1024} while:(n > 0) next:{n /= 2}
{
{
@SAY n;
@SAY n;
};</lang>
};</syntaxhighlight>




=={{header|Verilog}}==
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
<lang Verilog>
module main;
module main;
integer i;
integer i;
Line 3,372: Line 3,705:
end
end
endmodule
endmodule
</syntaxhighlight>
</lang>




=={{header|Vim Script}}==
=={{header|Vim Script}}==
<lang vim>let i = 1024
<syntaxhighlight lang="vim">let i = 1024
while i > 0
while i > 0
echo i
echo i
let i = i / 2
let i = i / 2
endwhile</lang>
endwhile</syntaxhighlight>


=={{header|Visual Basic .NET}}==
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
<lang vbnet>Dim x = 1024
Do
Console.WriteLine(x)
x = x \ 2
Loop While x > 0</lang>

=={{header|Vlang}}==
<lang vlang>fn main() {
mut i := 1024
mut i := 1024
for i > 0 {
for i > 0 {
Line 3,396: Line 3,722:
i /= 2
i /= 2
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,414: Line 3,740:


=={{header|Wart}}==
=={{header|Wart}}==
<lang wart>i <- 1024
<syntaxhighlight lang="wart">i <- 1024
while (i > 0)
while (i > 0)
prn i
prn i
i <- (int i/2)</lang>
i <- (int i/2)</syntaxhighlight>

=={{header|Wee Basic}}==
<lang Wee Basic>let number=1024
while number>0.5
print 1 number
let number=number/2
wend
end</lang>


=={{header|Whitespace}}==
=={{header|Whitespace}}==
<lang Whitespace>
<syntaxhighlight lang="whitespace">


Line 3,444: Line 3,762:




</syntaxhighlight>
</lang>
Pseudo-assembly equivalent:
Pseudo-assembly equivalent:
<lang asm>push 1024
<syntaxhighlight lang="asm">push 1024


0:
0:
Line 3,453: Line 3,771:
push 0 swap sub
push 0 swap sub
jn 0
jn 0
pop exit</lang>
pop exit</syntaxhighlight>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var i = 1024
<syntaxhighlight lang="wren">var i = 1024
while (i > 0) {
while (i > 0) {
System.print(i)
System.print(i)
i = (i / 2).floor
i = (i / 2).floor
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,478: Line 3,796:


=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
<lang asm>
<syntaxhighlight lang="asm">
; NASM 64 bit X86-64 assembly on Linux
; NASM 64 bit X86-64 assembly on Linux


Line 3,519: Line 3,837:
leave ; fix stack
leave ; fix stack
ret ; return
ret ; return
</syntaxhighlight>
</lang>

=={{header|XBasic}}==
{{works with|Windows XBasic}}
<lang xbasic>
i% = 1024
DO WHILE i% > 0
PRINT i%
i% = i% / 2
LOOP
</lang>


=={{header|XLISP}}==
=={{header|XLISP}}==
The specification calls for an integer value and for the loop to run <tt>WHILE</tt> that value is greater than zero. In a dynamically typed language like XLISP, variables cannot be declared as integer or real; but the same result is obtained by looping <tt>WHILE</tt> the value of the variable <i>i</i> is greater than or equal to one.
The specification calls for an integer value and for the loop to run <tt>WHILE</tt> that value is greater than zero. In a dynamically typed language like XLISP, variables cannot be declared as integer or real; but the same result is obtained by looping <tt>WHILE</tt> the value of the variable <i>i</i> is greater than or equal to one.
<lang xlisp>(DEFINE I 1024)
<syntaxhighlight lang="xlisp">(DEFINE I 1024)


(WHILE (>= I 1)
(WHILE (>= I 1)
(PRINT I)
(PRINT I)
(DEFINE I (/ I 2)))</lang>
(DEFINE I (/ I 2)))</syntaxhighlight>


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>code CrLf=9, IntOut=11;
<syntaxhighlight lang="xpl0">code CrLf=9, IntOut=11;
int I;
int I;
[I:= 1024;
[I:= 1024;
Line 3,547: Line 3,855:
I:= I>>1; \(same as I/2 for positive I)
I:= I>>1; \(same as I/2 for positive I)
];
];
]</lang>
]</syntaxhighlight>


=={{header|Z80 Assembly}}==
{{works with|CP/M 3.1|YAZE-AG-2.51.2 Z80 emulator}}
{{works with|ZSM4 macro assembler|YAZE-AG-2.51.2 Z80 emulator}}
Use the /S8 switch on the ZSM4 assembler for 8 significant characters for labels and names
<syntaxhighlight lang="z80">
;
; while loop, dividing 1024 repeatedly by 2, using Z80 assembly language
;
; Runs under CP/M 3.1 on YAZE-AG-2.51.2 Z80 emulator
; Assembled with zsm4 on same emulator/OS, uses macro capabilities of said assembler
; Created with vim under Windows
;
; Thanks to https://wikiti.brandonw.net for the idea for the conversion routine hl -> decimal ASCII
;
;
; 2023-05-19 Xorph
;


;
=={{header|Yabasic}}==
; Useful definitions
<lang Yabasic>i = 1024
;
while i > 0
Print i
i = int(i / 2)
wend


bdos equ 05h ; Call to CP/M BDOS function
end</lang>
strdel equ 6eh ; Set string delimiter
wrtstr equ 09h ; Write string to console


nul equ 00h ; ASCII control characters
cr equ 0dh
lf equ 0ah

cnull equ '0' ; ASCII character constants

;
; Macros for BDOS calls
;

setdel macro char ; Set string delimiter to char
ld c,strdel
ld e,char
call bdos
endm

print macro msg ; Output string to console
ld c,wrtstr
ld de,msg
call bdos
endm

newline macro ; Print newline
ld c,wrtstr
ld de,crlf
call bdos
endm

;
; =====================
; Start of main program
; =====================
;

cseg

setdel nul ; Set string delimiter to 00h
ld ix,value ; Register ix points to memory location of counter

while:
ld a,(ix) ; Z80 has no 16 bit compare, so we check the value byte by byte for 0
or a ; In contrast to other CPUs, loading a register does NOT set the flags
jr nz,docalc ; or-ing the accumulator with itself sets the flags and is faster than "cp 0"
ld a,(ix+1)
or a
jr z,endprog ; If both bytes are 0, end program - this jump could be optimized away
; and replaced with a direct "ret z", but we want to simulate a "real"
; while loop, so we continue (jump to) after the last loop statement

docalc:
ld hl,(value) ; Print the current value, followed by newline
ld iy,buffer ; Register iy points to memory location for current value as text for printout
call dispHL ; dispHL modifies iy, so it must be reset to the buffer on every iteration

print buffer
newline

srl (ix+1) ; Neither has the Z80 a 16 bit shift operation for dividing by 2...
rr (ix) ; Shift the MSB of value right and then rotate the LSB with carry to the right

jr while ; Next iteration

endprog:
ret ; Return to CP/M

;
; ===================
; End of main program
; ===================
;

;
; Helper routines - notice that the Z80 does not have a divide instruction
; Notice further that CP/M does not have any support for pretty-printing
; formatted numbers and stuff like that. So we have to do all this by hand...
;

;
; Converts the value (unsigned int) in register hl to its decimal representation
; Register iy has memory address of target for converted value
; String is terminated with nul character (\0)
;

dispHL:
ld b,1 ; Flag for leading '0'
irp x,<-10000,-1000,-100,-10,-1>
ld de,x ; Subtract powers of 10 and determine digit
call calcdig
endm

ld a,nul ; Terminate result string with nul
ld (iy+0),a

ret ; End of conversion routine

calcdig:
ld a,cnull-1 ; Determine the digit character
incrdig:
inc a ; Start with '0'
add hl,de ; As long as subtraction is possible, increment digit character
jr c,incrdig

sbc hl,de ; If negative, undo last subtraction and continue with remainder
cp cnull ; Check for leading '0', these are ignored
jr nz,adddig
bit 0,b ; Use bit instruction for check if flag set, register a contains digit
ret nz ; If '0' found and flag set, it is a leading '0' and we return
adddig:
ld b,0 ; Reset flag for leading '0', we are now outputting digits
ld (iy+0),a ; Store character in memory and set iy to next location
inc iy

ret ; End of conversion helper routine

;
; ================
; Data definitions
; ================
;

dseg

value: defw 1024d ; Starting value for loop, 16 bit little endian
crlf: defb cr,lf,nul ; Generic newline
buffer: defs 10 ; Buffer for conversion of number to text

</syntaxhighlight>

{{out}}
<pre>
E>whilelp
1024
512
256
128
64
32
16
8
4
2
1

</pre>


=={{header|Zig}}==
=={{header|Zig}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
const std = @import("std");


Line 3,570: Line 4,037:
std.debug.print("{}\n", .{i});
std.debug.print("{}\n", .{i});
}
}
</syntaxhighlight>
</lang>

=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>n:=1024; while(n>0){println(n); n/=2;}</lang>
<syntaxhighlight lang="zkl">n:=1024; while(n>0){println(n); n/=2;}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>

Latest revision as of 23:09, 9 March 2024

Task
Loops/While
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Start an integer value at   1024.

Loop while it is greater than zero.

Print the value (with a newline) and divide it by two each time through the loop.

Related tasks



0815

<:400:~}:_:%<:a:~$=<:2:=/^:_:

11l

Translation of: Python
V n = 1024
L n > 0
   print(n)
   n I/= 2

360 Assembly

Basic

Using binary arithmetic. Convert results to EBCDIC printable output.

*        While                     27/06/2016
WHILELOO CSECT                     program's control section 
         USING WHILELOO,12         set base register
         LR    12,15               load base register
         LA    6,1024              v=1024
LOOP     LTR   6,6                 while v>0 
         BNP   ENDLOOP             .
         CVD   6,PACKED              convert v to packed decimal
         OI    PACKED+7,X'0F'        prepare unpack
         UNPK  WTOTXT,PACKED         packed decimal to zoned printable
         WTO   MF=(E,WTOMSG)         display v
         SRA   6,1                   v=v/2   by right shift 
         B     LOOP                end while
ENDLOOP  BR    14                  return to caller
PACKED   DS    PL8                 packed decimal
WTOMSG   DS    0F                  full word alignment for wto
WTOLEN   DC    AL2(8),H'0'         length of wto buffer (4+1)
WTOTXT   DC    CL4' '              wto text
         END   WHILELOO
Output:

(+ sign indicates "problem state" (non system key) issued WTO's

+1024 
+0512 
+0256 
+0128 
+0064 
+0032 
+0016 
+0008 
+0004 
+0002 
+0001 
Structured Macros
*        While                     27/06/2016
WHILELOO CSECT
         USING WHILELOO,12         set base register
         LR    12,15               load base register
         LA    6,1024              v=1024
         DO WHILE=(LTR,6,P,6)      do while v>0 
         CVD   6,PACKED              convert v to packed decimal
         OI    PACKED+7,X'0F'        prepare unpack
         UNPK  WTOTXT,PACKED         packed decimal to zoned printable
         WTO   MF=(E,WTOMSG)         display
         SRA   6,1                   v=v/2   by right shift
         ENDDO ,                   end while
         BR    14                  return to caller
PACKED   DS    PL8                 packed decimal 
WTOMSG   DS    0F                  full word alignment for wto
WTOLEN   DC    AL2(8),H'0'         length of wto buffer (4+1)
WTOTXT   DC    CL4' '              wto text
         END   WHILELOO
Output:

Same as above

6502 Assembly

Code is called as a subroutine (i.e. JSR LoopsWhile). Specific OS/hardware routines for printing are left unimplemented.

LoopsWhile:	PHA			;push accumulator onto stack

		LDA #$00		;the 6502 is an 8-bit processor
		STA Ilow		;and so 1024 ($0400) must be stored in two memory locations
		LDA #$04
		STA Ihigh		
WhileLoop:	LDA Ilow
		BNE NotZero
		LDA Ihigh
		BEQ EndLoop
NotZero:	JSR PrintI		;routine not implemented
		LSR Ihigh		;shift right
		ROR Ilow		;rotate right
		JMP WhileLoop

EndLoop:	PLA			;restore accumulator from stack
		RTS			;return from subroutine

68000 Assembly

Printing routines are left out since this is not the focus of the task.

main:
MOVE.W #1024,D0

WhileLoop:
jsr PrintHexWord
LSR.W #1,D0
BNE WhileLoop

AArch64 Assembly

Works with: as version Raspberry Pi 3B version Buster 64 bits
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program loopwhile64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessResult:      .asciz "@"            // message result
szCarriageReturn:  .asciz "\n"
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss 
sZoneConv:           .skip 24
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                                    // entry of program 
    mov x20,#1024                        // loop counter
1:                                       // begin loop 
    mov x0,x20
    ldr x1,qAdrsZoneConv                 // display value
    bl conversion10                      // decimal conversion
    ldr x0,qAdrszMessResult
    ldr x1,qAdrsZoneConv
    bl strInsertAtCharInc                // insert result at @ character
    bl affichageMess                     // display message
    ldr x0,qAdrszCarriageReturn
    bl affichageMess                     // display return line
    lsr x20,x20,1                        // division by 2
    cmp x20,0                            // end ?
    bgt 1b                               // no ->begin loop one
 
 
100:                                     // standard end of the program 
    mov x0,0                             // return code
    mov x8,EXIT                          // request to exit program
    svc 0                                // perform the system call
 
qAdrsZoneConv:            .quad sZoneConv
qAdrszMessResult:         .quad szMessResult
qAdrszCarriageReturn:     .quad szCarriageReturn

/********************************************************/
/*        File Include fonctions                        */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
Output:
1024
512
256
128
64
32
16
8
4
2
1

Action!

PROC Main()
  CARD i=[1024]

  WHILE i>0
  DO
    PrintCE(i)
    i=i/2
  OD
RETURN
Output:

Screenshot from Atari 8-bit computer

1024
512
256
128
64
32
16
8
4
2
1

ActionScript

var i:int = 1024;
while (i > 0) {
    trace(i);
    i /= 2;
}

Ada

declare
   I : Integer := 1024;
begin
   while I > 0 loop
      Put_Line(Integer'Image(I));
      I := I / 2;
   end loop;
end;

Agena

Tested with Agena 2.9.5 Win32

scope
    local i := 1024;
    while i > 0 do
        print( i );
        i := i \ 2
    od
epocs

Aime

integer i;

i = 1024;
while (i) {
    o_plan(i, "\n");
    i /= 2;
}

ALGOL 60

The Loops/While structure was in the Algol 60 report of January 1963.

begin
	comment Loops/While - algol60 - 21/10/2014;
	integer i;
	for i:=1024,i div 2 while i>0 do outinteger(1,i)
end
Output:
1024  512  256  128  64  32  16  8  4  2  1

ALGOL 68

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8-8d
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

ALGOL W

begin
    integer i;
    i := 1024;
    while i > 0 do
    begin
        write( i );
        i := i div 2
    end
end.

ALGOL-M

begin
    integer i;
    i := 1024;
    while i > 0 do begin
        write( i );
        i := i / 2;
    end;
end

Amazing Hopper

#include <jambo.h>

Main
   i=1024
   Loop
       Printnl 'i'
       i \= 2
   Back if 'i' is positive
End

Assembler Hopper code:

main:
i=1024
____CODE_JUMP____883612951:,
{i};{"\n"}print;
i\=2
{i},jpos(____CODE_JUMP____883612951),____CODE_JUMP____854411479:,
emptystack?do{{0}};return
Output:
1024
512
256
128
64
32
16
8
4
2
1

AmbientTalk

Note: in AmbientTalk, while:do: is a keyworded message (as in Smalltalk). Both arguments to this message must be blocks (aka anonymous functions or thunks).

// print 1024 512 etc
def i := 1024;
while: { i > 0 } do: {
  system.print(" "+i);
  i := i/2;
}

AmigaE

PROC main()
  DEF i = 1024
  WHILE i > 0
    WriteF('\d\n', i)
    i := i / 2
  ENDWHILE
ENDPROC

AppleScript

AppleScript does not natively support a standard out. Use the Script Editor's Event Log as the output.

set i to 1024
repeat while i > 0
	log i
	set i to i / 2
end repeat

ARM Assembly

Works with: as version Raspberry Pi
/* ARM assembly Raspberry PI  */
/*  program loopwhile.s   */

/* Constantes    */
.equ STDOUT, 1     @ Linux output console
.equ EXIT,   1     @ Linux syscall
.equ WRITE,  4     @ Linux syscall

/*********************************/
/* Initialized data              */
/*********************************/
.data
szMessResult:      .ascii ""                    @ message result
sMessValeur:       .fill 11, 1, ' '
szCarriageReturn:  .asciz "\n"
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss 
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                                       @ entry of program 
    mov r4,#1024                            @ loop counter
1:                                          @ begin loop 
    mov r0,r4
    ldr r1,iAdrsMessValeur                  @ display value
    bl conversion10                         @ decimal conversion
    ldr r0,iAdrszMessResult
    bl affichageMess                        @ display message
    ldr r0,iAdrszCarriageReturn
    bl affichageMess                        @ display return line
    lsr r4,#1                               @ division by 2
    cmp r4,#0                               @ end ?
    bgt 1b                                  @ no ->begin loop one


100:                                        @ standard end of the program 
    mov r0, #0                              @ return code
    mov r7, #EXIT                           @ request to exit program
    svc #0                                  @ perform the system call

iAdrsMessValeur:          .int sMessValeur
iAdrszMessResult:         .int szMessResult
iAdrszCarriageReturn:     .int szCarriageReturn
/******************************************************************/
/*     display text with size calculation                         */ 
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
    push {r0,r1,r2,r7,lr}                   @ save  registres
    mov r2,#0                               @ counter length 
1:                                          @ loop length calculation 
    ldrb r1,[r0,r2]                         @ read octet start position + index 
    cmp r1,#0                               @ if 0 its over 
    addne r2,r2,#1                          @ else add 1 in the length 
    bne 1b                                  @ and loop 
                                            @ so here r2 contains the length of the message 
    mov r1,r0                               @ address message in r1 
    mov r0,#STDOUT                          @ code to write to the standard output Linux 
    mov r7, #WRITE                          @ code call system "write" 
    svc #0                                  @ call systeme 
    pop {r0,r1,r2,r7,lr}                    @ restaur registers */ 
    bx lr                                   @ return  
/******************************************************************/
/*     Converting a register to a decimal                                 */ 
/******************************************************************/
/* r0 contains value and r1 address area   */
.equ LGZONECAL,   10
conversion10:
    push {r1-r4,lr}                         @ save registers 
    mov r3,r1
    mov r2,#LGZONECAL
1:                                          @ start loop
    bl divisionpar10                        @ r0 <- dividende. quotient ->r0 reste -> r1
    add r1,#48                              @ digit
    strb r1,[r3,r2]                         @ store digit on area
    cmp r0,#0                               @ stop if quotient = 0 
    subne r2,#1                               @ previous position    
    bne 1b                                  @ else loop
                                            @ end replaces digit in front of area
    mov r4,#0
2:
    ldrb r1,[r3,r2] 
    strb r1,[r3,r4]                         @ store in area begin
    add r4,#1
    add r2,#1                               @ previous position
    cmp r2,#LGZONECAL                       @ end
    ble 2b                                  @ loop
    mov r1,#0                               @ final zero 
    strb r1,[r3,r4]
100:
    pop {r1-r4,lr}                          @ restaur registres 
    bx lr                                   @return
/***************************************************/
/*   division par 10   signé                       */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*  
/* and   http://www.hackersdelight.org/            */
/***************************************************/
/* r0 dividende   */
/* r0 quotient */	
/* r1 remainder  */
divisionpar10:	
  /* r0 contains the argument to be divided by 10 */
    push {r2-r4}                           @ save registers  */
    mov r4,r0  
    mov r3,#0x6667                         @ r3 <- magic_number  lower
    movt r3,#0x6666                        @ r3 <- magic_number  upper
    smull r1, r2, r3, r0                   @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) 
    mov r2, r2, ASR #2                     @ r2 <- r2 >> 2
    mov r1, r0, LSR #31                    @ r1 <- r0 >> 31
    add r0, r2, r1                         @ r0 <- r2 + r1 
    add r2,r0,r0, lsl #2                   @ r2 <- r0 * 5 
    sub r1,r4,r2, lsl #1                   @ r1 <- r4 - (r2 * 2)  = r4 - (r0 * 10)
    pop {r2-r4}
    bx lr                                  @ return

ArnoldC

IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP 1024
STICK AROUND n
TALK TO THE HAND n
GET TO THE CHOPPER n
HERE IS MY INVITATION n
HE HAD TO SPLIT 2
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED

Arturo

i: 1024
 
while [i>0] [
	print i
	i: i/2
]
Output:
1024
512
256
128
64
32
16
8
4
2
1

Asymptote

int i = 1024;

while(i > 0) {
  write(i);
  i = i # 2;       //or also i = quotient(i, 2);
}

//# Integer division; equivalent to quotient(x,y).
//Noting that the Python3 community adopted the comment symbol (//) for integer division, the
//Asymptote community decided to reciprocate and use their comment symbol for integer division!


ATS

With compile-time proof of termination of the loop (which is really a tail recursion).

#include "share/atspre_staload.hats"

fn
loop_while () : void =
  let
    fun
    loop {n : int | 0 <= n} .<n>.
         (n : uint n) : void =
      if n <> 0U then
        begin
          println! (n);
          loop (n / 2U)
        end
  in
    loop 1024U
  end

implement
main0 () =
  loop_while ()
Output:
1024
512
256
128
64
32
16
8
4
2
1

It is also possible to write an actual ‘while’ loop, although this method requires a variable (the ‘var’ declaration below) and has other problems, and should be used sparingly.

#include "share/atspre_staload.hats"

fn
loop_while () : void =
  let
    var n : uint = 1024U
  in
    while (0U < n)
      begin
        println! (n);
        n := n / 2U
      end
  end

implement
main0 () =
  loop_while ()

AutoHotkey

i = 1024
While (i > 0)
{
  output = %output%`n%i%
  i := Floor(i / 2)
}
MsgBox % output

AWK

BEGIN {
  v = 1024
  while(v > 0) {
    print v
    v = int(v/2)
  }
}

Axe

1024→A
While A>0
 Disp A▶Dec,i
 A/2→A
End

Bait

fun main() {
	mut i := 1024
	for i > 0 {
		println(i)
		i = i / 2
	}
}

BASIC

In general, the WHILE-WEND (or DO WHILE-LOOP) statement is used or it is simulated with a construct with conditional jump.

ANSI BASIC

Works with: Decimal BASIC
100 LET I = 1024
110 DO WHILE I > 0
120    PRINT I
130    LET I = INT(I / 2)
140 LOOP
150 END
Output:
 1024 
 512 
 256 
 128 
 64 
 32 
 16 
 8 
 4 
 2 
 1 

Applesoft BASIC

 10 I% = 1024
 20  IF I% > 0 THEN  PRINT I%:I% = I% / 2: GOTO 20

ASIC

REM Loops/While

I = 1024
WHILE I > 0
  PRINT I
  I = I / 2
WEND

END
Output:
  1024
   512
   256
   128
    64
    32
    16
     8
     4
     2
     1

BaCon

i = 1024
WHILE i > 0
   PRINT i
   i = i / 2
WEND

Ballerina

int i = 1024;
while i > 0 {
    io:println(i);
    i = i / 2;
}

BASIC256

i = 1024

while i > 0
	print i
	i = i \ 2
end while

end

BBC BASIC

      i% = 1024
      WHILE i%
        PRINT i%
        i% DIV= 2
      ENDWHILE

Chipmunk Basic

Works with: Chipmunk Basic version 3.6.4

Both loops are equivalent

100 i = 1024
110 do while i > 0
120   print i
130   i = int(i/2)
140 loop
150 print
160 i = 1024
170 while i > 0
180   print i
190   i = int(i/2)
200 wend

Commodore BASIC

There is no WHILE construct in Commodore BASIC. A GOTO construct is used instead. Also, an integer variable name has a % sign as its suffix.

10 N% = 1024
20 IF N% = 0 THEN 60
30 PRINT N%
40 N% = N%/2
50 GOTO 20
60 END
Output:
 1024                                   
 512                                    
 256                                    
 128                                    
 64                                     
 32                                     
 16                                     
 8                                      
 4                                      
 2                                      
 1                                      

See also Minimal BASIC

Creative Basic

DEF X:INT

X=1024

OPENCONSOLE

WHILE X>0

   PRINT X
   X=X/2
  
ENDWHILE
'Output starts with 1024 and ends with 1.

'Putting the following in the loop will produce output starting with 512 and ending with 0:
'X=X/2
'PRINT X

PRINT:PRINT"Press any key to end."

'Keep console from closing right away so the figures can be read.
WHILE INKEY$="":ENDWHILE

CLOSECONSOLE

'Since this is, in fact, a Creative Basic console program.
END

Note: Spacing is not an issue. I just find the code to be more readable with spaces.

FreeBASIC

' FB 1.05.0 Win64

Dim i As Integer = 1024

While i > 0
  Print i
  i Shr= 1
Wend

Sleep
Output:
 1024
 512
 256
 128
 64
 32
 16
 8
 4
 2
 1

FutureBasic

window 1

long i = 1024

while i > 0
  print i
  i = int( i / 2 )
wend

HandleEvents

Output:

 1024
 512
 256
 128
 64
 32
 16
 8
 4
 2
 1

Gambas

Click this link to run this code

Public Sub Main()
Dim siCount As Short = 1024

While siCount > 0 
  Print siCount;;
  siCount /= 2
Wend

End

Output:

1024 512 256 128 64 32 16 8 4 2 1

GW-BASIC

Works with: BASICA
Works with: Chipmunk Basic
Works with: PC-BASIC version any
Works with: QBasic
10 I = 1024
20 WHILE I > 0
30   PRINT I
40   i = INT(i/2)
50 WEND
60 END

IS-BASIC

100 LET I=1024
110 DO WHILE I>0
120   PRINT I
130   LET I=IP(I/2)
140 LOOP

IWBASIC

DEF X:INT

X=1024

OPENCONSOLE

WHILE X>0

    PRINT X
    X=X/2 

ENDWHILE
'Output starts with 1024 and ends with 1.

'Putting the following in the loop will produce output starting with 512 and ending with 0:
'X=X/2
'PRINT X

'When compiled as a console only program, a press any key to continue message is automatic.
'I presume code is added by the compiler.
CLOSECONSOLE

'Since this is, in fact, an IWBASIC console program, which compiles and runs.
END

Note: Spacing is not an issue. I just find the code to be more readable with spaces.

Liberty BASIC

All integers are changed to floats if an operation creates a non-integer result. Without using int() the program keeps going until erroring because accuracy was lost.

Works with: Just BASIC
i = 1024
while i > 0
   print i
   i = int( i / 2)
wend
end

Microsoft Small Basic

i = 1024
While i > 0
  TextWindow.WriteLine(i)
  i = Math.Floor(i / 2)
EndWhile

Minimal BASIC

Minimal BASIC have no while construct. Equivalent using conditional jump:

Works with: Commodore BASIC
Works with: Nascom ROM BASIC version 4.7
10 REM Loops/While
20 LET I = 1024
40 IF I <= 0 THEN 80
50 PRINT I
60 LET I = INT(I/2)
70 GOTO 40
80 END

MSX Basic

There is no WHILE construct in MSX Basic. A GOTO construct is used instead.

10 I% = 1024
20 IF I% = 0 THEN END
30 PRINT I%
40 I% = I%/2 : rem INT(I/2)
50 GOTO 20

Solutions GW-BASIC and Minimal BASIC work without changes.

Nascom BASIC

See Minimal BASIC.

If the repeated sequence is short then the loop can be written in one line.

Works with: Nascom ROM BASIC version 4.7
10 REM Loops/While
20 LET I=1024
30 IF I>0 THEN PRINT I:I=INT(I/2):GOTO 30
40 END

NS-HUBASIC

10 I=1024
20 IF I=0 THEN END
30 PRINT I
40 I=I/2
50 GOTO 20

PureBasic

If OpenConsole()  
  
  x.i = 1024
  While x > 0
    PrintN(Str(x))
    x / 2
  Wend

  Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
  Input()
  CloseConsole()
EndIf

QB64

Dim n As Integer
n = 1024
While n > 0
    Print n
    n = n \ 2
Wend

QBasic

Works with: QBasic version 1.1

Both loops are equivalent

i = 1024
WHILE i > 0
   PRINT i
   i = INT(i / 2)
WEND
PRINT

i = 1024
DO WHILE i > 0
   PRINT i
   i = i \ 2
LOOP
END

QuickBASIC

Works with: QuickBasic version 4.5
i = 1024
WHILE i > 0
  PRINT i
  i = i \ 2
WEND

Run BASIC

i = 1024
while i > 0
   print i
   i = int(i / 2)
wend
end

Sinclair ZX81 BASIC

The distinctive thing about a while loop is that the conditional test happens before the loop body, not after—so that the code in the loop may be executed zero times.

Since we have no integer type, we floor the result of the division each time.

10 LET I=1024
20 IF I=0 THEN GOTO 60
30 PRINT I
40 LET I=INT (I/2)
50 GOTO 20

SmallBASIC

i = 1024
WHILE i > 0
   PRINT i
   i = i \ 2  ' Using \ for integer division instead of /
WEND

TI-83 BASIC

1024→I
While I>0
Disp I
I/2→I
End

TI-89 BASIC

Local i
1024 → i
While i > 0
  Disp i
  intDiv(i, 2) → i
EndWhile

Tiny BASIC

Tiny BASIC have no while construct. Equivalent using conditional jump:

Works with: TinyBasic
10 REM Loops/While
20 LET I = 1024
30 IF I <= 0 THEN GOTO 70
40 PRINT I
50 LET I = I / 2
60 GOTO 30
70 END
Output:
1024
512
256
128
64
32
16
8
4
2
1

True BASIC

LET i = 1024

DO WHILE i > 0
   PRINT i
   LET i = INT(i / 2)
LOOP
END

VBA

Public Sub LoopsWhile()
    Dim value As Integer
    value = 1024
    Do While value > 0
        Debug.Print value
        value = value / 2
    Loop
End Sub

Visual Basic .NET

Dim x = 1024
Do
    Console.WriteLine(x)
    x = x \ 2
Loop While x > 0

Wee Basic

let number=1024
while number>0.5
print 1 number
let number=number/2
wend
end

XBasic

Works with: Windows XBasic
i% = 1024
DO WHILE i% > 0
  PRINT i%
  i% = i% / 2
LOOP

Yabasic

i = 1024 
while i > 0
  Print i
  i = int(i / 2)
wend
end

bc

i = 1024
while (i > 0) {
    i
    i /= 2
}

Befunge

84*:*>       :v
     ^/2,*25.:_@

blz

num = 1024
while num > 1 # blz will automatically cast num to a fraction when dividing 1/2, so this is necessary to stop an infinite loop
    print(num)
    num = num / 2
end

BQN

BQNcrate's while idiom is the closest equivalent of a while loop in the language.

_while_  {𝔽𝔾𝔽_𝕣_𝔾𝔽𝔾𝕩}

(÷2 •Show) _while_ (>0) 1024

Bracmat

1024:?n & whl'(!n:>0 & out$!n & div$(!n.2):?n)

Brat

Converts to integers so output is a little bit shorter and neater.

i = 1024
while { i > 0 } {
    p i
    i = (i / 2).to_i
}

C

int i = 1024;
while(i > 0) {
  printf("%d\n", i);
  i /= 2;
}

In for loop fashion:

int i;
for(i = 1024;i > 0; i/=2){
   printf("%d\n", i);
}

C#

int i = 1024;
while(i > 0){
   System.Console.WriteLine(i);
   i /= 2;
}

C++

int i = 1024;
while(i > 0){
  std::cout << i << std::endl;
  i /= 2;
}

Alternatively, it can be done with for:

for(int i = 1024; i > 0; i /= 2)
  std::cout << i << std::endl;

Instead of i /= 2 one can also use the bit shift operator i >>= 1 on integer variables.

Indeed, in C++,

for(init; cond; update){
  statement;
}

is equivalent to

{
  init;
  while(cond){
    statement;
    update;
  }
}

Caché ObjectScript

WHILELOOP
    set x = 1024
    while (x > 0) {
        write x,!
        set x = (x \ 2)    ; using non-integer division will never get to 0
    }
	
    quit
Output:
SAMPLES>DO ^WHILELOOP

1024 512 256 128 64 32 16 8 4 2 1

Chapel

var val = 1024;
while val > 0 {
        writeln(val);
        val /= 2;
}

ChucK

1024 => int value;

while(value > 0)
{
    <<<value>>>;
    value / 2 => value;
}

Clojure

(def i (ref 1024))

(while (> @i 0)
  (println @i)
  (dosync (ref-set i (quot @i 2))))

2 ways without mutability:

(loop [i 1024]
  (when (pos? i)
    (println i)
    (recur (quot i 2))))


(doseq [i (take-while pos? (iterate #(quot % 2) 1024))]
  (println i))

CLU

start_up = proc ()
    po: stream := stream$primary_output()
    n: int := 1024
    while n>0 do
        stream$putl(po, int$unparse(n))
        n := n/2
    end
end start_up

COBOL

COBOL does not have a while loop construct, but it is does have a PERFORM UNTIL structure, which means that the normal condition used in a while loop must be negated.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. Loop-While.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  I PIC 9999 VALUE 1024.

       PROCEDURE DIVISION.
           PERFORM UNTIL NOT 0 < I
               DISPLAY I
               DIVIDE 2 INTO I
           END-PERFORM

           GOBACK
           .

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

(let ((i 1024))
  (loop while (plusp i) do
        (print i)
        (setf i (floor i 2))))

(loop with i = 1024
      while (plusp i) do
      (print i)
      (setf i (floor i 2)))

(defparameter *i* 1024)
(loop while (plusp *i*) do
      (print *i*)
      (setf *i* (floor *i* 2)))

Cowgol

include "cowgol.coh";

var n: uint16 := 1024;
while n > 0 loop
    print_i16(n);
    print_nl();
    n := n/2;
end loop;

Crack

i = 1024;
while( i > 0 ) {
  cout ` $i\n`;
  i = i/2;
}

Crystal

i = 1024
while i > 0
    puts i
    i //= 2
end

until condition is the negated version, equivalent to while !(condition).

i = 1024
until i <= 0
   puts i
   i //= 2
end

D

import std.stdio;

void main() {
    int i = 1024;

    while (i > 0) {
        writeln(i);
        i >>= 1;
    }
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

Dao

i = 1024;
while( i > 0 ) i = i / 2;

Dart

void main() {
  var val = 1024;
  while (val > 0) {
    print(val);
    val >>= 2;
  }
}

Instead of the bitshift operator i >>= 2, you can also use i /= 2 on double variables.

void main() {
  num val = 1024;
  while (val > 0) {
    print(val);
    val /= 2;
  }
}

Dc

[ q ] sQ [ d 0!<Q p 2 / lW x ] sW 1024 lW x

DCL

DCL is quite primitive in terms of "control statements", no WHILE, REPEAT, UNLESS or FOR, so must make do with IF/THEN/ELSE and GOTO statements.

$ i = 1024
$Loop:
$ IF ( i .LE. 0 ) THEN GOTO LoopEnd
$ WRITE sys$output F$FAO( "  i = !4UL", i )  ! formatted ASCII output, fixed-width field
$ ! Output alternatives:
$ !   WRITE sys$output F$STRING( i )         ! explicit integer-to-string conversion
$ !   WRITE sys$output i                     ! implicit conversion to string/output
$ i = i / 2
$ GOTO Loop
$LoopEnd:

Delphi

var
  i : Integer;
begin
  i := 1024;

  while i > 0 do
  begin
    Writeln(i);
    i := i div 2;
  end;
end;

Draco

proc nonrec main() void:
    word i;
    i := 1024;
    while i > 0 do
        writeln(i);
        i := i >> 1
    od
corp
Output:
1024
512
256
128
64
32
16
8
4
2
1

Dragon

i = 1024
while(i > 0){
   showln i
   i >>= 1 //also acceptable: i /= 2
}

DUP

1024[$][$.10,2/\%]# {Short form}

Explanation:

1024                {push 1024 on stack}
    [ ][         ]# {while[condition>0][do]}
     $              {DUP}
        $.          {DUP, print top of stack to STDOUT}
          10,       {print newline}
             2/\%   {2 DIV/MOD SWAP POP}

Alternative, if the interpreter allows using the shift operator:

1024[$][$.10,1»]#

Output:

1024
512
256
128
64
32
16
8
4
2
1

DWScript

var i := 1024;

while i > 0 do begin
   PrintLn(i);
   i := i div 2;
end;

Dyalect

Translation of: Swift
var i = 1024
while i > 0 {
  print(i)
  i /= 2
}

E

var i := 1024
while (i > 0) {
    println(i)
    i //= 2
}

EasyLang

i = 1024
while i > 0
  print i
  i = i div 2
.

EchoLisp

(set! n 1024)
(while (> n 0) (write n) (set! n (quotient n 2)))
1024 512 256 128 64 32 16 8 4 2 1

EGL

x int = 1024;
while ( x > 0 )
   SysLib.writeStdout( x );
   x = MathLib.floor( x / 2 );
end

Elena

ELENA 6.x:

public program()
{
    int i := 1024;
    while (i > 0)
    {
        console.writeLine(i);
 
        i /= 2
    }
}

Elixir

defmodule Loops do
  def while(0), do: :ok
  def while(n) do
    IO.puts n
    while( div(n,2) )
  end
end

Loops.while(1024)

Emacs Lisp

(let ((i 1024))
  (while (> i 0)
    (message "%d" i)
    (setq i (/ i 2))))

EMal

int i = 1024
while i > 0
  writeLine(i)
  i /= 2
end

Erlang

-module(while).
-export([loop/0]).

loop() ->
	loop(1024).
 
loop(N) when N div 2 =:= 0 ->
	io:format("~w~n", [N]);
	
loop(N) when N >0 ->
	io:format("~w~n", [N]),
	loop(N div 2).

ERRE

   I%=1024
   WHILE I%>0 DO  ! you can leave out >0
     PRINT(I%)
     I%=I% DIV 2  ! I%=INT(I%/2) for C-64 version
   END WHILE

Euphoria

integer i
i = 1024

while i > 0 do
    printf(1, "%g\n", {i})
    i = floor(i/2) --Euphoria does NOT use integer division.  1/2 = 0.5
end while

Even without the floor() the code will in fact end. But it's FAR beyond 1.

F#

let rec loop n = if n > 0 then printf "%d " n; loop (n / 2)
loop 1024

Factor

1024 [ dup 0 > ] [ dup . 2 /i ] while drop

FALSE

1024[$0>][$."
"2/]#%

Fantom

class Main
{
  public static Void main ()
  {
    Int i := 1024
    while (i > 0)
    {
      echo (i)
      i /= 2
    }
  }
}

Fennel

(var n 1024)
(while (> i 0)
  (print i)
  (set i (// n 2)))

Fermat

Note that the test is against something between 1/2 and 1, rather than 0. Otherwise Fermat will continue printing numbers of the form 1/2^n indefinitely.

n:=1024;
while n>2/3 do !!n;n:=n/2; od;

Forth

: halving ( n -- )
  begin  dup 0 >
  while  cr dup .  2/
  repeat drop ;
1024 halving

Fortran

Works with: Fortran version 90 and later
INTEGER :: i = 1024
DO WHILE (i > 0)
  WRITE(*,*) i
  i = i / 2
END DO
Works with: Fortran version 77 and later
      PROGRAM LOOPWHILE
        INTEGER I

C       FORTRAN 77 does not have a while loop, so we use GOTO statements
C       with conditions instead. This is one of two easy ways to do it.
        I = 1024
   10   CONTINUE
C       Check condition.
        IF (I .GT. 0) THEN
C         Handle I.
          WRITE (*,*) I
          I = I / 2
C         Jump back to before the IF block.
          GOTO 10
        ENDIF
        STOP
      END
Works with: Fortran version IV and 66 and later
      PROGRAM LOOPWHILE
      INTEGER I
C     FORTRAN 66 does not have IF block.
      I = 1024
 10   CONTINUE
      IF (I .LE. 0) GOTO 20
      WRITE (*,*) I
      I = I / 2
      GOTO 10
 20   CONTINUE
      STOP
      END

Fortress

component loops_while
  export Executable

  var i:ZZ32 = 1024
  run() = while i > 0 do
    println(i)
    i := i DIV 2
  end
end
Output:
1024
512
256
128
64
32
16
8
4
2
1

Frink

i=1024
while i>0
{
   i = i/1
}

GAP

n := 1024;
while n > 0 do
    Print(n, "\n");
    n := QuoInt(n, 2);
od;

GML

i = 1024
while(i > 0)
    {
    show_message(string(i))
    i /= 2
    }

Go

i := 1024
for i > 0 {
  fmt.Printf("%d\n", i)
  i /= 2
}

Groovy

Solution:

int i = 1024
while (i > 0) {
    println i
    i /= 2
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

Haskell

import Control.Monad (when)

main = loop 1024      
  where loop n = when (n > 0)
                      (do print n
                          loop (n `div` 2))

You can use whileM_ function from monad-loops package that operates on monads:

import Data.IORef
import Control.Monad.Loops

main :: IO ()
main = do r <- newIORef 1024
          whileM_ (do n <- readIORef r
                     return (n > 0))
                  (do n <- readIORef r
                     print n
                     modifyIORef r (`div` 2))

With MonadComprehensions extension you can write it a little bit more readable:

{-# LANGUAGE MonadComprehensions #-}
import Data.IORef 
import Control.Monad.Loops

main :: IO ()
main = do
   r <- newIORef 1024 
   whileM_ [n > 0 | n <- readIORef r] $ do
        n <- readIORef r  
        print n
        modifyIORef r (`div` 2)

Haxe

Using shift right.

var i = 1024;
    
while (i > 0) {
  Sys.println(i);
  i >>= 1;
}

Using integer division.

var i = 1024;
    
while (i > 0) {
  Sys.println(i);
  i = Std.int(i / 2);
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

hexiscript

let i 1024
while i > 0
  println i
  let i (i / 2)
endwhile

HolyC

U16 i = 1024;
while (i > 0) {
  Print("%d\n", i);
  i /= 2;
}

Icon and Unicon

procedure main()
   local i
   i := 1024
   while write(0 < (i := i / 2))
end

Inform 7

let N be 1024;
while N > 0:
	say "[N][line break]";
	let N be N / 2;

Insitux

Use / for floating-point division, and use (> i 0) for full printing of the sequence (as the output of 1 here is due to the return of the last while statement).

(var i 1024)

(while (> i 1)
  (print i)
  (var i (// i 2)))
Output:
1024
512
256
128
64
32
16
8
4
2
1

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).

monad define 1024
  while. 0 < y do.
    smoutput y
    y =. <. -: y 
  end.
  i.0 0
)

Note: this defines an anonymous function (monad define, and the subsequent lines) and passes it the argument 1024, which means it will be executed as soon as the full definition is available.

Java

int i = 1024;
while(i > 0){
   System.out.println(i);
   i >>= 1; //also acceptable: i /= 2;
}

With a for loop:

for(int i = 1024; i > 0;i /= 2 /*or i>>= 1*/){
   System.out.println(i);
}

JavaScript

var n = 1024;
while (n > 0) {
  print(n);
  n /= 2;
}

In a functional idiom of JavaScript, however, we can not use a While statement to achieve this task, as statements return no value, mutate state, and can not be composed within other functional expressions.

Instead, we can define a composable loopWhile() function which has no side effects, and takes 3 arguments:

  1. An initial value
  2. A function which returns some derived value, corresponding to the body of the While loop
  3. A conditional function, corresponding to the While test
function loopWhile(varValue, fnDelta, fnTest) {
  'use strict';
  var d = fnDelta(varValue);

  return fnTest(d) ? [d].concat(
    loopWhile(d, fnDelta, fnTest)
  ) : [];
}

console.log(
  loopWhile(
    1024,
    function (x) {
      return Math.floor(x/2);
    },
    function (x) {
      return x > 0;
    }
  ).join('\n')
);

If we assume integer division here (Math.floor(x/2)) rather than the floating point division (x/2) used in the imperative example, we obtain the output:

512
256
128
64
32
16
8
4
2
1

Joy

DEFINE putln == put '\n putch.
1024 [] [dup putln 2 /] while.

jq

Using recurse/1

# To avoid printing 0, test if the input is greater than 1
1024 | recurse( if . > 1 then ./2 | floor else empty end)

Using recurse/2 (requires jq >1.4)

1024 | recurse( ./2 | floor; . > 0)

Using a filter

def task: if . > 0 then ., (./2 | floor | task) else empty end;
1024|task

Using while/2

If your jq does not include while/2 as a builtin, here is its definition:

def while(cond; update):
  def _while: if cond then ., (update | _while) else empty end;
  _while;

For example:

1024|while(. > 0; ./2|floor)

Jsish

#!/usr/bin/env jsish
/* Loops/While in Jsish */
var i = 1024;

while (i > 0) { puts(i); i = i / 2 | 0; }

/*
=!EXPECTSTART!=
1024
512
256
128
64
32
16
8
4
2
1
=!EXPECTEND!=
*/
Output:
prompt$ jsish -u loopsWhile.jsi
[PASS] loopsWhile.jsi

Julia

n = 1024

while n > 0
    println(n)
    n >>= 1
end
Output:
1024
512
256
128
64
32
16
8
4
2
1

K

Implementation of the task using anonymous function is given below

{while[x>0; \echo x; x%:2]} 1024

Kotlin

// version 1.0.6

fun main(args: Array<String>) {
    var value = 1024
    while (value > 0) {
        println(value)
        value /= 2
    }
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

LabVIEW

Use Round Towards -Inf to prevent the integer becoming a float.
This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

Lambdatalk

{def loops_while
 {lambda {:i}
  {if {< :i 1}
   then (end of loop)
   else :i {loops_while {/ :i 2}}}}}
-> loops_while

{loops_while 1024}
-> 1024 512 256 128 64 32 16 8 4 2 1 (end of loop)

Alternative solution:

{def while
 {lambda {:i}
  {if {< :i 1}
   then
   else {br}:i {while {/ :i 2}}}}}

{while 1024} -> 

1024 
512 
256 
128 
64 
32 
16 
8 
4 
2 
1

Lang

$n = 1024
while($n > 0) {
	fn.println($n)
	
	$n //= 2
}

Lang5

Translation of: Factor
: /i  / int ; : 0=  0 == ;
: dip  swap '_ set execute _ ; : dupd  'dup dip ;
: 2dip  swap '_x set swap '_y set execute _y _x ;
: while
    do  dupd 'execute 2dip
        rot 0= if break else dup 2dip then
    loop ;

1024 "dup 0 >" "dup . 2 /i" while

langur

var .i = 1024
while .i > 0 {
    writeln .i
    .i \= 2
}

Lasso

local(i = 1024)
while(#i > 0) => {^
	#i + '\r'
	#i /= 2
^}

LDPL

data:
n is number

procedure:
store 1024 in n
while n is greater than 0 do
    display n lf
    divide n by 2 in n
    floor n
repeat

LIL

set num 1024; while {$num > 0} {print $num; set num [expr $num \ 2]}

Backslash is integer division, otherwise LIL would allow the division to go floating point.

Lingo

n = 1024
repeat while n>0
  put n
  n = n/2 -- integer division implicitely returns floor: 1/2 -> 0
end repeat

Lisaac

+ i : INTEGER;
i := 1024;
{ i > 0 }.while_do {
  i.println;
  
  i := i / 2;
};

LiveCode

put 1024 into n
repeat while n > 0
    put n & cr
    divide n by 2
end repeat

make "n 1024
while [:n > 0] [print :n  make "n :n / 2]

LOLCODE

LOLCODE's loop semantics require an afterthought if a condition is used, thus the nop in the following example. The more idiomatic approach would have been to GTFO of the loop once n had reached 0.

HAI 1.3

I HAS A n ITZ 1024

IM IN YR loop UPPIN YR nop WILE n
    VISIBLE n
    n R QUOSHUNT OF n AN 2
IM OUTTA YR loop

KTHXBYE

Lua

n = 1024
while n>0 do
  print(n)
  n = math.floor(n/2)
end

M2000 Interpreter

Module Checkit {
      Def long A=1024
      While A>0 {
            Print A
            A/=2
      }
}
Checkit

One line

Module Online { A=1024&: While A>0 {Print A: A/=2}} : OnLine

m4

This ‘loop’ is really a tail recursion, which m4 implementations generally do not optimize.

divert(-1)

define(`loop',
  `ifelse(eval(0 < ($1)),1,`$1`
'loop(eval(($1) / 2))')')

divert`'dnl
loop(1024)dnl
Output:

$ m4 loops_while.m4

1024
512
256
128
64
32
16
8
4
2
1

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

Maple

To avoid generating an infinite sequence (1/2, 1/4, 1/8, 1/16, etc.) of fractions after n takes the value 1, we use integer division (iquo) rather than the solidus operation (/).

> n := 1024: while n > 0 do print(n); n := iquo(n,2) end:
                                  1024
                                  512
                                  256
                                  128
                                   64
                                   32
                                   16
                                   8
                                   4
                                   2
                                   1

Mathematica/Wolfram Language

Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function:

i = 1024;
While[i > 0,
 Print[i];
 i = Floor[i/2];
]

MATLAB / Octave

In Matlab (like Octave) the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0. A 'floor' is used to round the number.

i = 1024;
while (i > 0)
    disp(i);
    i = floor(i/2);
end

A vectorized version of the code is

  printf('%d\n', 2.^[log2(1024):-1:0]);

Maxima

block([n], n: 1024, while n > 0 do (print(n), n: quotient(n, 2)));

/* using a C-like loop: divide control variable by two instead of incrementing it */
for n: 1024 next quotient(n, 2) while n > 0 do print(n);

MAXScript

a = 1024
while a > 0 do
(
    print a
    a /= 2
)

Metafont

Metafont has no while loop, but it can be "simulated" easily.

a := 1024;
forever: exitif not (a > 0);
  show a;
  a := a div 2;
endfor

min

Works with: min version 0.19.3
1024 :n (n 0 >) (n puts 2 div @n) while

MiniScript

i = 1024
while i > 0
    print i
    i = floor(i/2)
end while
Output:
1024
512
256
128
64
32
16
8
4
2
1

MIRC Scripting Language

alias while_loop {
  var %n = 10
  while (%n >= 0) {
    echo -a Countdown: %n
    dec %n
  }
}

MIXAL

******************************************
* X = M / N WHILE X > 0
* STORE EACH X IN NUMERIC ARRAY
* PRINT ARRAY
*******************************************
M	EQU	1024		
N	EQU	2
LPR	EQU	18		
BUF0	EQU	100		
MSG	EQU	2000		
LENGTH	EQU	500		
	ORIG	3000
START	IOC	0(LPR) 		
	ENTX	M		
CALC	STX	BUF0,1 		
	DIV	=N=		
	SRAX	5		
	INC1	1		
	JXP	CALC		
	ST1	LENGTH		
PRINT	LDA	BUF0,2		
	CHAR			
	STX	MSG		
	OUT	MSG(LPR)	
	INC2	1		
	CMP2	LENGTH		
	JNE	PRINT
	HLT			
	END	START

МК-61/52

1	0	2	4	П0	ИП0	/-/	x<0	15	ИП0
2	/	П0	БП	05	С/П

Modula-2

MODULE DivBy2;
  IMPORT InOut;

  VAR
    i: INTEGER;
BEGIN
  i := 1024;
  WHILE i > 0 DO
    InOut.WriteInt(i, 4);
    InOut.WriteLn;
    i := i DIV 2
  END
END DivBy2.

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;

Monte

var i := 1024
while (i > 0):
    traceln(i)
    i //= 2

MOO

i = 1024;
while (i > 0)
  player:tell(i);
  i /= 2;
endwhile

Morfa

import morfa.io.print;

var i = 1024;
while(i > 0)
{
    println(i);
    i /= 2;
}

Nanoquery

$n = 1024
while ($n > 0)
    println $n
    $n = $n/2
end while

Neko

var i = 1024

while(i > 0) {
    $print(i + "\n");
    i = $idiv(i, 2)
}

Nemerle

mutable x = 1024;
while (x > 0)
{
    WriteLine($"$x");
    x /= 2;
}

Or, with immutable types, after Haskell:

// within another function, eg Main()
def loop(n : int) : void
{
    when (n > 0)
    {
        WriteLine($"$n");
        loop(n / 2);
    }
}
       
loop(1024)

NetRexx

/* NetRexx */
options replace format comments java crossref savelog symbols nobinary

  say
  say 'Loops/While'

  x_ = 1024
  loop while x_ > 0
    say x_.right(6)
    x_ = x_ % 2 -- integer division
    end

NewLISP

(let (i 1024)
  (while (> i 0)
    (println i)
    (setq i (/ i 2))))

Nim

var n: int = 1024
while n > 0:
  echo(n)
  n = n div 2

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;

Objeck

i := 1024;
while(i > 0) {
   i->PrintLine();
   i /= 2;
};

ObjectIcon

An important thing here is the test for termination is a succeed-or-fail operation, rather than a boolean expression. No boolean value is written to the output; rather, the whole statement fails when n reaches zero. The expression 0 < n either returns n or fails.

import io

procedure main()
   local n
   n := 1024
   while n := write(0 < n) / 2
end
Output:

$ oit -s loops_while-oi.icn && ./loops_while-oi

1024
512
256
128
64
32
16
8
4
2
1

The following also works, and shows a way to avoid having to code the method of writing the output into the loop itself.

import io

procedure main()
  local coexpression
  coexpression := create generator(1024)
  while write(@coexpression)
end

procedure generator(n)
  while 0 < n do { suspend n; n /:= 2 }
end

To show what I mean, let’s do some (merely illustrative) loop-unrolling where the output gets written.

import io

procedure main()
  local coexpression
  coexpression := create generator(1024)
  write(@coexpression) &
      write(@coexpression) &
      write(@coexpression) &
      write(@coexpression)&
      while (write(@coexpression) &
             write(@coexpression) &
             write(@coexpression) &
             write(@coexpression))
end

procedure generator(n)
  while 0 < n do { suspend n; n /:= 2 }
end

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

Octave

i = 1024;
while (i > 0)
  disp(i)
  i = floor(i/2);
endwhile

The usage of the type int32 is not convenient, since the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0.

Odin

Odin only has one loop type: for

package main

import "core:fmt"

main :: proc() {
    for i := 1024 ; i > 0 ; i /= 2 {
		fmt.println(i)
    }
}

Oforth

1024 while ( dup ) [ dup println 2 / ]

OOC

main: func {
  value := 1024
  while (value > 0) {
    value toString() println()
    value /= 2
  }
}

Oz

Oz' for-loop can be used in a C-like manner:

for I in 1024; I>0; I div 2 do
   {Show I}
end

Alternatively, we can use the while feature of the for-loop with a mutable variable:

declare
  I = {NewCell 1024}
in
  for while:@I > 0 do
     {Show @I}
     I := @I div 2
  end

Panda

Panda doesn't have explicit loops, instead we solve it by using the transitive closure operator. It applies a function to each successive value, each unique value is outputted. Our function halves, we make sure that the result is greater than 0 and add newline.

fun half(a) type integer->integer a.divide(2)
1024.trans(func:half).gt(0) nl

Panoramic

dim x%:rem an integer

x%=1024

while x%>0

     print x%
     x%=x%/2

end_while

rem output starts with 1024 and ends with 1.

terminate

PARI/GP

n=1024;
while(n,
  print(n);
  n/=2
);

Pascal

program divby2(output);

var
  i: integer;

begin
  i := 1024;
  while i > 0 do
    begin
      writeln(i);
      i := i div 2
    end
end.

PeopleCode

Local string &CRLF;
Local number &LoopNumber;
&LoopNumber = 1024;
&CRLF = Char(10) | Char(13);

While &LoopNumber > 0;
 WinMessage(&LoopNumber | &CRLF);
 &LoopNumber = &LoopNumber / 2;
End-While;

Perl

my $n = 1024;
while($n){
    print "$n\n";
    $n = int $n / 2;
}

or written as a for-loop and using the bit-shift operator

for(my $n = 1024; $n > 0; $n >>= 1){
    print "$n\n";
}

until (condition) is equivalent to while (not condition).

my $n = 1024;
until($n == 0){
    print "$n\n";
    $n = int $n / 2;
}

Phix

integer i = 1024
while i!=0 do
    ?i
    i = floor(i/2)  -- (see note)
end while

note: using i=i/2 would iterate over 1000 times until i is 4.94e-324 before the final division made it 0, if it didn't typecheck when it got set to 0.5

Phixmonti

/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/While
by Galileo, 11/2022 #/

include ..\Utilitys.pmt

1024 dup while dup ? 2 / int dup endwhile
Output:
1024
512
256
128
64
32
16
8
4
2
1

=== Press any key to exit ===

PHL

var i = 1024;
while (i > 0) {
	printf("%i\n", i);
	i = i/2;
}

PHP

$i = 1024;
while ($i > 0) {
   echo "$i\n";
   $i >>= 1;
}

Picat

While loop

go =>
 N = 1024,
 while (N > 0)
   println(N),
   N := N // 2
 end.

Recursion

Translation of: Prolog
go2 =>
  while_loop(1024).

while_loop(0) => true.
while_loop(N) =>
  println(N),
  while_loop(N//2).


PicoLisp

(let N 1024
   (while (gt0 N)
      (println N)
      (setq N (/ N 2)) ) )

Pike

int main(){
   int i = 1024;
   while(i > 0){
      write(i + "\n");
      i = i / 2;
   }
}

PL/0

var i;
begin
  i := 1024;
  while i > 0 do
  begin
    ! i;
    i := i / 2
  end;
end.
Output:
    1024
     512
     256
     128
      64
      32
      16
       8
       4
       2
       1

PL/I

declare i fixed binary initial (1024);

do while (i>0);
   put skip list (i);
   i = i / 2;
end;

PL/SQL

Works with: Oracle
set serveroutput on
declare
  n number := 1024;
begin
  while n > 0 loop
    dbms_output.put_line(n);
    n := trunc(n / 2);
  end loop;
end;
/

Plain English

To run:
Start up.
Show the halvings of 1024.
Wait for the escape key.
Shut down.

To show the halvings of a number:
If the number is 0, exit.
Convert the number to a string.
Write the string to the console.
Divide the number by 2.
Repeat.

Pop11

lvars i = 1024;
while i > 0 do
    printf(i, '%p\n');
    i div 2 -> i;
endwhile;

PostScript

PostScript has no real while loop, but it can easily be created with an endless loop and a check at the beginning:

1024
{
    dup 0 le     % check whether still greater than 0
    { pop exit } % if not, exit the loop
    if
    dup =        % print the number
    2 idiv       % divide by two
}
loop

PowerShell

[int]$i = 1024
while ($i -gt 0) {
    $i
    $i /= 2
}

Prolog

while(0) :- !.
while(X) :-
    writeln(X),
    X1 is X // 2,
    while(X1).

Start the calculation at a top-level like this:

?- while(1024).

Python

n = 1024
while n > 0:
    print n
    n //= 2

Quackery

1024
[ dup 0 > while 
  dup echo cr 2 /
  again ]
drop
Output:
1024
512
256
128
64
32
16
8
4
2
1


R

i <- 1024L
while(i > 0) 
{
   print(i)
   i <- i %/% 2
}

Racket

Loop/When

#lang racket
(let loop ([n 1024])
  (when (positive? n)
    (displayln n)
    (loop (quotient n 2))))

Macro

#lang racket
(define-syntax-rule (while condition body ...)
  (let loop ()
    (when condition
      body ...
      (loop))))

(define n 1024)
(while (positive? n)
  (displayln n)
  (set! n (sub1 n)))

Raku

(formerly Perl 6)

Here is a straightforward translation of the task description:

my $n = 1024; while $n > 0 { say $n; $n div= 2 }

The same thing with a C-style loop and a bitwise shift operator:

loop (my $n = 1024; $n > 0; $n +>= 1) { say $n }

And here's how you'd really write it, using a sequence operator that intuits the division for you:

.say for 1024, 512, 256 ... 1

REBOL

REBOL [
	Title: "Loop/While"
	URL: http://rosettacode.org/wiki/Loop/While
]

value: 1024
while [value > 0][
	print value
	value: to-integer value / 2
]


ReScript

let n = ref(1024)
while n.contents > 0 {
  Js.log(n.contents)
  n := n.contents / 2
}

Retro

1024 [ cr &putn sip 2 / dup ] while

REXX

version 1, simple

/*REXX program demonstrates a  DO WHILE  with index reduction construct.*/
j=1024                                 /*define the initial value of  J.*/
        do  while  j>0                 /*test if made at the top of  DO.*/
        say j
        j=j%2                          /*in REXX, % is integer division.*/
        end
                                       /*stick a fork in it, we're done.*/
Output:
1024
512
256
128
64
32
16
8
4
2
1

version 2, right justified

Note that a faster version could be implemented with

DO WHILE x\==0

but that wouldn't be compliant with the wording of the task.

/*REXX program demonstrates a  DO WHILE  with index reduction construct.*/
x=1024                                 /*define the initial value of  X.*/
        do  while  x>0                 /*test if made at the top of  DO.*/
        say right(x,10)                /*pretty output by aligning right*/
        x=x%2                          /*in REXX, % is integer division.*/
        end
                                       /*stick a fork in it, we're done.*/
Output:
       1024
        512
        256
        128
         64
         32
         16
          8
          4
          2
          1

version 3, faster WHILE comparison

/*REXX program demonstrates a  DO WHILE  with index reduction construct.*/
x=1024                                 /*define the initial value of  X.*/
        do  while  x>>0                /*this is an  exact  comparison. */
        say right(x,10)                /*pretty output by aligning right*/
        x=x%2                          /*in REXX, % is integer division.*/
        end
                                       /*stick a fork in it, we're done.*/

output is the same as version 2.

version 4, index reduction

/*REXX program demonstrates a  DO WHILE  with index reduction construct.*/
                                       /* [↓] note:   BY   defaults to 1*/  
        do j=1024  by 0  while  j>>0   /*this is an  exact  comparison. */
        say right(j,10)                /*pretty output by aligning right*/
        j=j%2                          /*in REXX, % is integer division.*/
        end
                                       /*stick a fork in it, we're done.*/

output is the same as version 2.

Ring

i = 1024
while i > 0 
      see i + nl
      i = floor(i / 2)
end

Rockstar

The sky is a television on fire.
The floor is ornamental.
The table is hydrochloric.
While the sky is higher than the floor,
Shout the sky.
Put the sky over the table into the sky.
(Rockstar is known for doing that JavaScript quirk, since all numbers are floats, but due to it having the same limits as JavaScript and the most popular interpreter being JS-based, the loop is finite, yet you don't get the result you would get in something like Python.)

RPL

≪ 1024
   WHILE DUP REPEAT
      DUP 1 DISP
      2 /
   END
   DROP
≫

Ruby

i = 1024
while i > 0 do
   puts i
   i /= 2
end

The above can be written in one statement:

puts i = 1024
puts i /= 2 while i > 0

until condition is equivalent to while not condition.

i = 1024
until i <= 0 do
   puts i
   i /= 2
end

Rust

fn main() {
    let mut n: i32 = 1024;
    while n > 0 {
        println!("{}", n);
        n /= 2;
    }
}

SAS

data _null_;
n=1024;
do while(n>0);
  put n;
  n=int(n/2);
end;
run;

Sather

class MAIN is
  main is
    i ::= 1024;
    loop while!(i > 0);
      #OUT + i + "\n";
      i := i / 2;
    end;
  end;
end;

Scala

Library: Scala

Imperative

var i = 1024
while (i > 0) {
  println(i)
  i /= 2
}

Tail recursive

  @tailrec
  def loop(iter: Int) {
    if (iter > 0) {
      println(iter)
      loop(iter / 2)
    }
  }
  loop(1024)

Iterator

  def loop = new Iterator[Int] {
    var i = 1024
    def hasNext = i > 0
    def next(): Int = { val tmp = i; i = i / 2; tmp }
  }
  loop.foreach(println(_))

Stream

Finite stream (1024..0) filtered by takeWhile (1024..1).

  def loop(i: Int): Stream[Int] = i #:: (if (i > 0) loop(i / 2) else Stream.empty)
  loop(1024).takeWhile(_ > 0).foreach(println(_))

Scheme

(do ((n 1024 (quotient n 2)))
    ((<= n 0))
    (display n)
    (newline))

Scilab

Works with: Scilab version 5.5.1
i=1024
while i>0
    printf("%4d\n",i)
    i=int(i/2)
end
Output:
1024
 512
 256
 128
  64
  32
  16
   8
   4
   2
   1

Seed7

$ include "seed7_05.s7i";

const proc: main is func
  local
    var integer: i is 1024;
  begin
    while i > 0 do
      writeln(i);
      i := i div 2
    end while;
  end func;
Output:
1024
512
256
128
64
32
16
8
4
2
1

SenseTalk

put 1024 into x
log x
Repeat until x = 1
	divide x by 2
	log x
End repeat

SETL

n := 1024;
while n > 0 loop
    print( n );
    n := n div 2;
end loop;

Sidef

var i = 1024
while (i > 0) {
    say i
    i //= 2
}

Simula

Works with: SIMULA-67
begin
  integer i;
  i:=1024;
  while i>0 do
  begin
     outint(i,5);
     i:=i//2-1
  end
end
Output:
 1024  511  254  126   62   30   14    6    2 

Slate

#n := 1024.
[n isPositive] whileTrue: 
  [inform: number printString.
   n := n // 2]

Smalltalk

The Block (aka lambda closure) class provides a number of loop messages; with test at begin, test at end and with exit (break).

[s atEnd] whileFalse: [s next. ...].
[foo notNil] whileTrue: [s next. ...].
[...] doWhile: [ ... someBooleanExpression ].
[...] doUntil: [ ... someBooleanExpression ].
[:exit | ... cold ifTrue:[exit value]. ...] loopWithExit

Examples:

number := 1024.
[ number > 0 ] whileTrue: 
  [ Transcript print: number; nl.
  number := number // 2 ]
number := 1024.
[ number <= 0 ] whileFalse: 
  [ Transcript print: number; nl.
  number := number // 2 ]

Sparkling

var i = 1024;
while i > 0 {
    print(i);
    i /= 2;
}

Spin

Works with: BST/BSTC
Works with: FastSpin/FlexSpin
Works with: HomeSpun
Works with: OpenSpin
con
  _clkmode = xtal1 + pll16x
  _clkfreq = 80_000_000

obj
  ser : "FullDuplexSerial.spin"

pub main | n
  ser.start(31, 30, 0, 115200)

  n := 1024
  repeat while n > 0
    ser.dec(n)
    ser.tx(32)
    n /= 2

  waitcnt(_clkfreq + cnt)
  ser.stop
  cogstop(0)
Output:
1024 512 256 128 64 32 16 8 4 2 1

SPL

n = 1024
>
  #.output(n)
  n /= 2
< n!<1
Output:
1024
512
256
128
64
32
16
8
4
2
1

SQL PL

Works with: Db2 LUW

version 9.7 or higher.

With SQL PL:

--#SET TERMINATOR @

SET SERVEROUTPUT ON @

BEGIN
 DECLARE I SMALLINT DEFAULT 1024;

 Loop: WHILE (I > 0) DO
  CALL DBMS_OUTPUT.PUT_LINE(I);
  SET I = I / 2;
 END WHILE Loop;
END @

Output:

db2 -td@
db2 => SET SERVEROUTPUT ON @
DB20000I  The SET SERVEROUTPUT command completed successfully.
db2 => BEGIN
...
db2 (cont.) => END @
DB20000I  The SQL command completed successfully.

1024
512
256
128
64
32
16
8
4
2
1

Standard ML

val n = ref 1024;
while !n > 0 do (
  print (Int.toString (!n) ^ "\n");
  n := !n div 2
)

But it is more common to write it in a tail-recursive functional style:

let
  fun loop n =
    if n > 0 then (
      print (Int.toString n ^ "\n");
      loop (n div 2)
    ) else ()
in
  loop 1024
end

Stata

local n=1024
while `n'>0 {
	display `n'
	local n=floor(`n'/2)
}

Suneido

i = 1024
while (i > 0)
    {
    Print(i)
    i = (i / 2).Floor()
    }
Output:
1024
512
256
128
64
32
16
8
4
2
1

Swift

var i = 1024
while i > 0 {
  println(i)
  i /= 2
}

Tailspin

In Tailspin you can loop by sending a value back to the matchers (by "-> #"). Depending on how you set that up, you create different loops.

1024 -> \(
  <0~..> '$;$#10;' -> !OUT::write
    $ ~/ 2 -> #
\) -> !VOID

Tcl

set i 1024
while {$i > 0} {
    puts $i
    set i [expr {$i / 2}]
}

Plain TeX

\newcount\rosetta
\rosetta=1024
\loop
    \the\rosetta\endgraf
    \divide\rosetta by 2
    \ifnum\rosetta > 0
\repeat
\end

TorqueScript

This has to make use of mFloor because torque has automatic type shuffling, causing an infiniteloop.

%num = 1024;
while(%num > 0)
{
    echo(%num);
    %num = mFloor(%num / 2);
}

Transact-SQL

DECLARE @i INT = 1024;
WHILE @i >0
BEGIN
    PRINT @i;
    SET @i = @i / 2;
END;

Trith

1024 [dup print 2 / floor] [dup 0 >] while drop
1024 [dup print 1 shr] [dup 0 >] while drop

TUSCRIPT

$$ MODE TUSCRIPT
i=1024
LOOP
   PRINT i
   i=i/2
   IF (i==0) EXIT
ENDLOOP
Output:
1024
512
256
128
64
32
16
8
4
2
1 

Unicon

See Icon.

Uniface

variables
	numeric I
endvariables

I = 1024
while (I > 0)
	putmess I
	I = (I/2)[trunc]
endwhile

UNIX Shell

Works with: Bourne Again SHell
x=1024
while [[ $x -gt 0 ]]; do 
  echo $x
  x=$(( $x/2 ))
done

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

Ursa

decl int n
set n 1024

while (> n 0)
    out n endl console
    set n (int (/ n 2))
end while

Ursala

Unbounded iteration is expressed with the -> operator. An expression (p-> f) x, where p is a predicate and f is a function, evaluates to x, f(x), or f(f(x)), etc. as far as necessary to falsify p.

Printing an intermediate result on each iteration is a bigger problem because side effects are awkward. Instead, the function g in this example iteratively constructs a list of results, which is displayed on termination.

The argument to g is the unit list <1024>. The predicate p is ~&h, the function that tests whether the head of a list is non-null (equivalent to non-zero). The iterated function f is that which conses the truncated half of the head of its argument with a copy of the whole argument. The main program takes care of list reversal and formatting.

#import nat

g = ~&h-> ^C/half@h ~&

#show+

main = %nP*=tx g <1024>
Output:
1024
512
256
128
64
32
16
8
4
2
1

Explicit iteration has its uses but there are always alternatives. The same output is produced by the following main program using bit manipulation.

main = %nP*=tK33 1024

V

1024 [0 >] [
   dup puts
   2 / >int
] while

Vala

int i = 1024;
while (i > 0) {
  stdout.printf("%d\n", i);
  i /= 2;
}

Vedit macro language

#1 = 1024
while (#1 > 0) {
    Num_Type(#1)
    #1 /= 2
}

or with for loop:

for (#1 = 1024; #1 > 0; #1 /= 2) {
    Num_Type(#1)
}

Verbexx

//  Basic @LOOP while: verb

@LOOP init:{@VAR n = 1024} while:(n > 0) next:{n /= 2}
{
     @SAY n; 
};


Verilog

module main;
  integer i;
  
  initial begin
      i = 1024;
 
      while( i > 0) begin
        $display(i);
        i = i / 2;
      end
      $finish ;
    end
endmodule


Vim Script

let i = 1024
while i > 0
    echo i
    let i = i / 2
endwhile

V (Vlang)

fn main() {
    mut i := 1024
    for i > 0 {
        println(i)
        i /= 2
    }
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

Wart

i <- 1024
while (i > 0)
  prn i
  i <- (int i/2)

Whitespace

Pseudo-assembly equivalent:

push 1024

0:
    dup onum push 10 ochr
    push 2 div dup
    push 0 swap sub
        jn 0
        pop exit

Wren

var i = 1024
while (i > 0) {
    System.print(i)
    i = (i / 2).floor
}
Output:
1024
512
256
128
64
32
16
8
4
2
1

X86 Assembly

; NASM 64 bit X86-64 assembly on Linux

global main
extern printf

segment .data

printffmt db `%ld\n`,0

segment .text

main:	                     
    push rbp                    
    mov rbp,rsp 
    
; used rbx and r12 because printf preserves these values
    
    mov rbx,1024                 ; start with 1024
    mov r12,2                    ; load 2 as divisor

.toploop                         ; top of while loop    
    cmp rbx,0                    ; compare to 0
    jle .done                    ; exit 0 or less
    
    lea rdi,[printffmt]          ; print number in rsi
    mov rsi,rbx                  ; mov to rsi as argument
    call printf

; calculate n/2 and save
    xor rdx,rdx                  ; clear rdx for division
    mov rax,rbx                  ; mov number to rax for division
    idiv r12                     ; divide by 2
    mov rbx,rax                  ; save n/2

    jmp .toploop                 ; next loop

.done
    xor rax,rax                  ; return code 0
    leave                        ; fix stack
    ret                          ; return

XLISP

The specification calls for an integer value and for the loop to run WHILE that value is greater than zero. In a dynamically typed language like XLISP, variables cannot be declared as integer or real; but the same result is obtained by looping WHILE the value of the variable i is greater than or equal to one.

(DEFINE I 1024)

(WHILE (>= I 1)
    (PRINT I)
    (DEFINE I (/ I 2)))

XPL0

code CrLf=9, IntOut=11;
int I;
[I:= 1024;
while I>0 do
        [IntOut(0, I);  CrLf(0);
        I:= I>>1;       \(same as I/2 for positive I)
        ];
]

Z80 Assembly

Works with: CP/M 3.1 version YAZE-AG-2.51.2 Z80 emulator
Works with: ZSM4 macro assembler version YAZE-AG-2.51.2 Z80 emulator

Use the /S8 switch on the ZSM4 assembler for 8 significant characters for labels and names

	;
	; while loop, dividing 1024 repeatedly by 2, using Z80 assembly language
	;
	; Runs under CP/M 3.1 on YAZE-AG-2.51.2 Z80 emulator
	; Assembled with zsm4 on same emulator/OS, uses macro capabilities of said assembler
	; Created with vim under Windows
	;
	; Thanks to https://wikiti.brandonw.net for the idea for the conversion routine hl -> decimal ASCII
	;
	;
	; 2023-05-19 Xorph
	;

	;
	; Useful definitions
	;

	bdos	equ 05h		; Call to CP/M BDOS function
	strdel	equ 6eh		; Set string delimiter
	wrtstr	equ 09h		; Write string to console

	nul	equ 00h		; ASCII control characters
	cr	equ 0dh
	lf	equ 0ah

	cnull	equ '0'		; ASCII character constants

	;
	; Macros for BDOS calls
	;

setdel 	macro	char		; Set string delimiter to char
	ld	c,strdel
	ld	e,char
	call	bdos
	endm

print 	macro	msg		; Output string to console
	ld	c,wrtstr
	ld	de,msg
	call	bdos
	endm

newline	macro			; Print newline
	ld	c,wrtstr
	ld	de,crlf
	call	bdos
	endm

	;
	; =====================
	; Start of main program
	; =====================
	;

	cseg

	setdel	nul		; Set string delimiter to 00h
	ld	ix,value	; Register ix points to memory location of counter

while:
	ld	a,(ix)		; Z80 has no 16 bit compare, so we check the value byte by byte for 0
	or	a		; In contrast to other CPUs, loading a register does NOT set the flags
	jr	nz,docalc	; or-ing the accumulator with itself sets the flags and is faster than "cp 0"
	ld	a,(ix+1)
	or	a
	jr	z,endprog	; If both bytes are 0, end program - this jump could be optimized away
				; and replaced with a direct "ret z", but we want to simulate a "real"
				; while loop, so we continue (jump to) after the last loop statement

docalc:
	ld	hl,(value)	; Print the current value, followed by newline
	ld	iy,buffer	; Register iy points to memory location for current value as text for printout
	call	dispHL		; dispHL modifies iy, so it must be reset to the buffer on every iteration

	print	buffer
	newline

	srl	(ix+1)		; Neither has the Z80 a 16 bit shift operation for dividing by 2...
	rr	(ix)		; Shift the MSB of value right and then rotate the LSB with carry to the right

	jr	while		; Next iteration

endprog:
	ret			; Return to CP/M

	;
	; ===================
	; End of main program
	; ===================
	;

	;
	; Helper routines - notice that the Z80 does not have a divide instruction
	; Notice further that CP/M does not have any support for pretty-printing
	; formatted numbers and stuff like that. So we have to do all this by hand...
	;

	;
	; Converts the value (unsigned int) in register hl to its decimal representation
	; Register iy has memory address of target for converted value
	; String is terminated with nul character (\0)
	;

dispHL:
	ld	b,1		; Flag for leading '0'
	irp	x,<-10000,-1000,-100,-10,-1>
	ld	de,x		; Subtract powers of 10 and determine digit
	call	calcdig
	endm

	ld	a,nul		; Terminate result string with nul
	ld	(iy+0),a

	ret			; End of conversion routine

calcdig:
	ld	a,cnull-1	; Determine the digit character
incrdig:
	inc	a		; Start with '0'
	add	hl,de		; As long as subtraction is possible, increment digit character
	jr	c,incrdig

	sbc	hl,de		; If negative, undo last subtraction and continue with remainder
	cp	cnull		; Check for leading '0', these are ignored
	jr	nz,adddig
	bit	0,b		; Use bit instruction for check if flag set, register a contains digit
	ret	nz		; If '0' found and flag set, it is a leading '0' and we return
adddig:
	ld	b,0		; Reset flag for leading '0', we are now outputting digits
	ld	(iy+0),a	; Store character in memory and set iy to next location
	inc	iy

	ret			; End of conversion helper routine

	;
	; ================
	; Data definitions
	; ================
	;

	dseg

value:	defw	1024d		; Starting value for loop, 16 bit little endian
crlf:	defb	cr,lf,nul	; Generic newline
buffer:	defs	10		; Buffer for conversion of number to text
Output:
E>whilelp
1024
512
256
128
64
32
16
8
4
2
1

Zig

const std = @import("std");

pub fn main() void {
    var i: u11 = 1024;
    while (i > 0) : (i /= 2)
        std.debug.print("{}\n", .{i});
}

zkl

n:=1024; while(n>0){println(n); n/=2;}
Output:
1024
512
256
128
64
32
16
8
4
2
1