Loops/While: Difference between revisions

18,758 bytes added ,  2 months ago
m
added output
(Loops/While in Asymptote)
m (added output)
 
(37 intermediate revisions by 23 users not shown)
Line 10:
Print the value (with a newline) and divide it by two each time through the loop.
 
 
;Related tasks:
Line 20 ⟶ 19:
*   [[Loops/For]]
*   [[Loops/For with a specified step]]
*   [[Loops/Foreach]]bas
*   [[Loops/Increment loop index within loop body]]
*   [[Loops/Infinite]]
Line 31 ⟶ 30:
 
=={{header|0815}}==
<langsyntaxhighlight lang="0815"><:400:~}:_:%<:a:~$=<:2:=/^:_:</langsyntaxhighlight>
 
=={{header|11l}}==
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V n = 1024
L n > 0
print(n)
n I/= 2</langsyntaxhighlight>
 
=={{header|360 Assembly}}==
;Basic
Using binary arithmetic. Convert results to EBCDIC printable output.
<langsyntaxhighlight lang="360asm">* While 27/06/2016
WHILELOO CSECT program's control section
USING WHILELOO,12 set base register
Line 62 ⟶ 61:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
END WHILELOO</langsyntaxhighlight>
{{out}} (+ sign indicates "problem state" (non system key) issued WTO's
<pre style="height:16ex">
Line 78 ⟶ 77:
</pre>
;Structured Macros
<langsyntaxhighlight lang="360asm">* While 27/06/2016
WHILELOO CSECT
USING WHILELOO,12 set base register
Line 95 ⟶ 94:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
END WHILELOO</langsyntaxhighlight>
{{out}}
Same as above
Line 101 ⟶ 100:
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR LoopsWhile). Specific OS/hardware routines for printing are left unimplemented.
<langsyntaxhighlight lang="6502asm">LoopsWhile: PHA ;push accumulator onto stack
 
LDA #$00 ;the 6502 is an 8-bit processor
Line 117 ⟶ 116:
 
EndLoop: PLA ;restore accumulator from stack
RTS ;return from subroutine</langsyntaxhighlight>
 
=={{header|68000 Assembly}}==
Printing routines are left out since this is not the focus of the task.
<syntaxhighlight lang="68000devpac">main:
MOVE.W #1024,D0
 
WhileLoop:
jsr PrintHexWord
LSR.W #1,D0
BNE WhileLoop</syntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopwhile64.s */
Line 176 ⟶ 186:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 192 ⟶ 202:
</pre>
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
CARD i=[1024]
 
Line 200 ⟶ 210:
i=i/2
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/While.png Screenshot from Atari 8-bit computer]
Line 218 ⟶ 228:
 
=={{header|ActionScript}}==
<langsyntaxhighlight lang="actionscript">var i:int = 1024;
while (i > 0) {
trace(i);
i /= 2;
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">declare
I : Integer := 1024;
begin
Line 232 ⟶ 242:
I := I / 2;
end loop;
end;</langsyntaxhighlight>
 
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena">scope
local i := 1024;
while i > 0 do
Line 242 ⟶ 252:
i := i \ 2
od
epocs</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer i;
 
i = 1024;
Line 251 ⟶ 261:
o_plan(i, "\n");
i /= 2;
}</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
The Loops/While structure was in the Algol 60 report of January 1963.
<langsyntaxhighlight lang="algol60">begin
comment Loops/While - algol60 - 21/10/2014;
integer i;
for i:=1024,i div 2 while i>0 do outinteger(1,i)
end
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 272 ⟶ 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]}}
<langsyntaxhighlight lang="algol68">INT i := 1024;
WHILE i > 0 DO
print(i);
i := i OVER 2
OD</langsyntaxhighlight>
{{Out}}
<pre>
Line 283 ⟶ 293:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
integer i;
i := 1024;
Line 291 ⟶ 301:
i := i div 2
end
end.</langsyntaxhighlight>
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algol">begin
integer i;
i := 1024;
Line 301 ⟶ 311:
i := i / 2;
end;
end</langsyntaxhighlight>
 
=={{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}}==
Line 307 ⟶ 354:
Both arguments to this message must be blocks (aka anonymous functions or thunks).
 
<langsyntaxhighlight lang="ambienttalk">// print 1024 512 etc
def i := 1024;
while: { i > 0 } do: {
system.print(" "+i);
i := i/2;
}</langsyntaxhighlight>
 
=={{header|AmigaE}}==
<langsyntaxhighlight lang="amigae">PROC main()
DEF i = 1024
WHILE i > 0
Line 321 ⟶ 368:
i := i / 2
ENDWHILE
ENDPROC</langsyntaxhighlight>
 
=={{header|AppleScript}}==
AppleScript does not natively support a standard out.
Use the Script Editor's Event Log as the output.
<langsyntaxhighlight AppleScriptlang="applescript ">set i to 1024
repeat while i > 0
log i
set i to i / 2
end repeat</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program loopwhile.s */
Line 455 ⟶ 502:
 
 
</syntaxhighlight>
</lang>
 
=={{header|ArnoldC}}==
<langsyntaxhighlight lang="arnoldc">IT'S SHOWTIME
HEY CHRISTMAS TREE n
YOU SET US UP 1024
Line 468 ⟶ 515:
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">i: 1024
while [i>0] [
print i
i: i/2
]</langsyntaxhighlight>
 
{{out}}
Line 493 ⟶ 540:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">int i = 1024;
 
while(i > 0) {
Line 502 ⟶ 549:
//# 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!</langsyntaxhighlight>
 
 
Line 508 ⟶ 555:
With compile-time proof of termination of the loop (which is really a tail recursion).
 
<langsyntaxhighlight lang="ats">#include "share/atspre_staload.hats"
 
fn
Line 527 ⟶ 574:
implement
main0 () =
loop_while ()</langsyntaxhighlight>
 
{{out}}
Line 543 ⟶ 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.
<langsyntaxhighlight lang="ats">#include "share/atspre_staload.hats"
 
fn
Line 559 ⟶ 606:
implement
main0 () =
loop_while ()</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">i = 1024
While (i > 0)
{
Line 568 ⟶ 615:
i := Floor(i / 2)
}
MsgBox % output</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">BEGIN {
v = 1024
while(v > 0) {
Line 577 ⟶ 624:
v = int(v/2)
}
}</langsyntaxhighlight>
 
=={{header|Axe}}==
<langsyntaxhighlight lang="axe">1024→A
While A>0
Disp A▶Dec,i
A/2→A
End</langsyntaxhighlight>
 
=={{header|Bait}}==
<syntaxhighlight lang="bait">
fun main() {
mut i := 1024
for i > 0 {
println(i)
i = i / 2
}
}
</syntaxhighlight>
 
=={{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
100 LET iI = i / 21024
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}}===
<langsyntaxhighlight Applesoftlang="applesoft BASICbasic"> 10 I% = 1024
20 IF I% > 0 THEN PRINT I%:I% = I% / 2: GOTO 20</langsyntaxhighlight>
 
==={{header|ASIC}}===
<syntaxhighlight lang="basic">
Look also [[#BASIC|BASIC]].
<lang basic>
REM Loops/While
 
Line 611 ⟶ 687:
 
END
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 628 ⟶ 704:
 
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">
Look also [[#BASIC|BASIC]].
<lang freebasic>
i = 1024
WHILE i > 0
PRINT i
i = i / 2
WEND</langsyntaxhighlight>
 
==={{header|Ballerina}}===
<syntaxhighlight lang="ballerina">int i = 1024;
while i > 0 {
io:println(i);
i = i / 2;
}
</syntaxhighlight>
 
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">i = 1024
 
while i > 0
Line 644 ⟶ 727:
end while
 
end</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> i% = 1024
WHILE i%
PRINT i%
i% DIV= 2
ENDWHILE</langsyntaxhighlight>
 
==={{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}}===
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.
<langsyntaxhighlight lang="gwbasic">10 N% = 1024
20 IF N% = 0 THEN 60
30 PRINT N%
40 N% = N%/2
50 GOTO 20
60 END</langsyntaxhighlight>
{{out}}
<pre>
Line 679 ⟶ 777:
 
==={{header|Creative Basic}}===
<langsyntaxhighlight Creativelang="creative Basicbasic">DEF X:INT
 
X=1024
Line 705 ⟶ 803:
 
'Since this is, in fact, a Creative Basic console program.
END</langsyntaxhighlight>
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}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 LET I=1024
110 DO WHILE I>0
120 PRINT I
130 LET I=IP(I/2)
140 LOOP</langsyntaxhighlight>
 
==={{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}}===
Minimal BASIC have no <code>while</code> construct. Equivalent using conditional jump:
{{works with|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<lang gwbasic>
<syntaxhighlight lang="gwbasic">10 REM Loops/While
20 LET I = 1024
40 IF I <= 0 THEN 80
Line 725 ⟶ 952:
60 LET I = INT(I/2)
70 GOTO 40
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}}===
See [[#Minimal BASIC|Minimal BASIC]].
 
If the repeated sequence is short then the loop can be written in one line.
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Loops/While
20 LET I=1024
30 IF I>0 THEN PRINT I:I=INT(I/2):GOTO 30
40 END
</syntaxhighlight>
 
==={{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}}===
Tiny BASIC have no <code>while</code> construct. Equivalent using conditional jump:
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">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</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">LET i = 1024
 
DO WHILE i > 0
Line 735 ⟶ 1,103:
LET i = INT(i / 2)
LOOP
END</langsyntaxhighlight>
 
==={{Headerheader|Tiny BASICVBA}}===
<syntaxhighlight lang="vb">Public Sub LoopsWhile()
<lang Tiny BASIC> REM TinyBasic does not have a do .. while construct. Equivalent using conditional jump:
Dim value As Integer
value = 1024
Do While value > 0
Debug.Print value
value = value / 2
Loop
End Sub</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
LET i = 0
<syntaxhighlight lang="vbnet">Dim x = 1024
10 LET i = i + 1
Do
PRINT i
Console.WriteLine(x)
IF (i / 6) * 6 <> i THEN GOTO 10
END</lang>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}}==
<langsyntaxhighlight lang="bc">i = 1024
while (i > 0) {
i
i /= 2
}</langsyntaxhighlight>
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">84*:*> :v
^/2,*25.:_@</langsyntaxhighlight>
 
=={{header|blz}}==
<langsyntaxhighlight 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
print(num)
num = num / 2
end</langsyntaxhighlight>
 
=={{header|BQN}}==
Line 768 ⟶ 1,170:
BQNcrate's while idiom is the closest equivalent of a while loop in the language.
 
<langsyntaxhighlight lang="bqn">_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
 
(⌊∘÷⟜2 •Show) _while_ (>⟜0) 1024</langsyntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">1024:?n & whl'(!n:>0 & out$!n & div$(!n.2):?n)</langsyntaxhighlight>
 
=={{header|Brat}}==
Converts to integers so output is a little bit shorter and neater.
 
<langsyntaxhighlight lang="brat">i = 1024
while { i > 0 } {
p i
i = (i / 2).to_i
}</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">int i = 1024;
while(i > 0) {
printf("%d\n", i);
i /= 2;
}</langsyntaxhighlight>
In for loop fashion:
<langsyntaxhighlight lang="c">int i;
for(i = 1024;i > 0; i/=2){
printf("%d\n", i);
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">int i = 1024;
while(i > 0){
System.Console.WriteLine(i);
i /= 2;
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">int i = 1024;
while(i > 0){
std::cout << i << std::endl;
i /= 2;
}</langsyntaxhighlight>
Alternatively, it can be done with <code>for</code>:
<langsyntaxhighlight lang="cpp">for(int i = 1024; i > 0; i /= 2)
std::cout << i << std::endl;</langsyntaxhighlight>
 
Instead of <code>i /= 2</code> one can also use the bit shift operator <code>i >>= 1</code> on integer variables.
 
Indeed, in C++,
<langsyntaxhighlight lang="cpp">for(init; cond; update){
statement;
}</langsyntaxhighlight>
is equivalent to
<langsyntaxhighlight lang="cpp">{
init;
while(cond){
Line 826 ⟶ 1,228:
update;
}
}</langsyntaxhighlight>
 
=={{header|Caché ObjectScript}}==
<langsyntaxhighlight Cachélang="caché ObjectScriptobjectscript">WHILELOOP
set x = 1024
while (x > 0) {
Line 836 ⟶ 1,238:
}
quit</langsyntaxhighlight>
 
{{out}}<pre>SAMPLES>DO ^WHILELOOP
Line 854 ⟶ 1,256:
 
=={{header|Chapel}}==
<langsyntaxhighlight lang="chapel">var val = 1024;
while val > 0 {
writeln(val);
val /= 2;
}</langsyntaxhighlight>
 
=={{header|ChucK}}==
<syntaxhighlight lang="text">
1024 => int value;
 
Line 869 ⟶ 1,271:
value / 2 => value;
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(def i (ref 1024))
 
(while (> @i 0)
(println @i)
(dosync (ref-set i (quot @i 2))))</langsyntaxhighlight>
 
2 ways without mutability:
 
<langsyntaxhighlight Clojurelang="clojure">(loop [i 1024]
(when (pos? i)
(println i)
Line 887 ⟶ 1,289:
 
(doseq [i (take-while pos? (iterate #(quot % 2) 1024))]
(println i))</langsyntaxhighlight>
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">start_up = proc ()
po: stream := stream$primary_output()
n: int := 1024
Line 897 ⟶ 1,299:
n := n/2
end
end start_up</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. Loop-While.
 
Line 915 ⟶ 1,317:
 
GOBACK
.</langsyntaxhighlight>
 
=={{header|ColdFusion}}==
Line 921 ⟶ 1,323:
 
With tags:
<langsyntaxhighlight lang="cfm"><cfset i = 1024 /><cfloop condition="i GT 0"> #i#< br />
<cfset i /= 2 />
</cfloop></langsyntaxhighlight>
With script:
<langsyntaxhighlight lang="cfm"><cfscript> i = 1024;
while( i > 0 )
{
writeOutput( i + "< br/ >" );
}
</cfscript></langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(let ((i 1024))
(loop while (plusp i) do
(print i)
Line 947 ⟶ 1,349:
(print *i*)
(setf *i* (floor *i* 2)))
</syntaxhighlight>
</lang>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
var n: uint16 := 1024;
Line 957 ⟶ 1,359:
print_nl();
n := n/2;
end loop;</langsyntaxhighlight>
 
=={{header|Crack}}==
<langsyntaxhighlight lang="crack">i = 1024;
while( i > 0 ) {
cout ` $i\n`;
i = i/2;
}</langsyntaxhighlight>
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">i = 1024
while i > 0
puts i
i //= 2
end</langsyntaxhighlight>
 
<code>until ''condition''</code> is the negated version, equivalent to <code>while !(''condition'')</code>.
 
<langsyntaxhighlight lang="crystal">i = 1024
until i <= 0
puts i
i //= 2
end</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 991 ⟶ 1,393:
i >>= 1;
}
}</langsyntaxhighlight>
{{out}}
<pre>1024
Line 1,006 ⟶ 1,408:
 
=={{header|Dao}}==
<langsyntaxhighlight lang="dao">i = 1024;
while( i > 0 ) i = i / 2;</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight Dclang="dc">[ q ] sQ [ d 0!<Q p 2 / lW x ] sW 1024 lW x</langsyntaxhighlight>
 
=={{header|DCL}}==
Line 1,017 ⟶ 1,437:
so must make do with IF/THEN/ELSE and GOTO statements.
 
<langsyntaxhighlight DCLlang="dcl">$ i = 1024
$Loop:
$ IF ( i .LE. 0 ) THEN GOTO LoopEnd
Line 1,026 ⟶ 1,446:
$ i = i / 2
$ GOTO Loop
$LoopEnd:</langsyntaxhighlight>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">var
<lang Delphi>var
i : Integer;
begin
Line 1,040 ⟶ 1,460:
i := i div 2;
end;
end;</langsyntaxhighlight>
 
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">proc nonrec main() void:
word i;
i := 1024;
Line 1,050 ⟶ 1,470:
i := i >> 1
od
corp</langsyntaxhighlight>
{{out}}
<pre>1024
Line 1,065 ⟶ 1,485:
 
=={{header|Dragon}}==
<langsyntaxhighlight lang="dragon">i = 1024
while(i > 0){
showln i
i >>= 1 //also acceptable: i /= 2
}</langsyntaxhighlight>
 
=={{header|DUP}}==
 
<langsyntaxhighlight lang="dup">1024[$][$.10,2/\%]# {Short form}</langsyntaxhighlight>
 
Explanation:
<langsyntaxhighlight lang="dup">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}</langsyntaxhighlight>
 
Alternative, if the interpreter allows using the shift operator:
 
<langsyntaxhighlight lang="dup">1024[$][$.10,1»]#</langsyntaxhighlight>
 
Output:
 
<langsyntaxhighlight lang="dup">1024
512
256
Line 1,099 ⟶ 1,519:
4
2
1</langsyntaxhighlight>
 
=={{header|DWScript}}==
 
<langsyntaxhighlight Delphilang="delphi">var i := 1024;
 
while i > 0 do begin
PrintLn(i);
i := i div 2;
end;</langsyntaxhighlight>
 
=={{header|Dyalect}}==
Line 1,114 ⟶ 1,534:
{{trans|Swift}}
 
<langsyntaxhighlight Dyalectlang="dyalect">var i = 1024
while i > 0 {
print(i)
i /= 2
}</langsyntaxhighlight>
 
=={{header|E}}==
 
<langsyntaxhighlight lang="e">var i := 1024
while (i > 0) {
println(i)
i //= 2
}</langsyntaxhighlight>
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">i = 1024
while i > 0
print i
i = i div 2
.</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="lisp">
(set! n 1024)
(while (> n 0) (write n) (set! n (quotient n 2)))
1024 512 256 128 64 32 16 8 4 2 1
</syntaxhighlight>
</lang>
 
=={{header|EGL}}==
 
<langsyntaxhighlight EGLlang="egl">x int = 1024;
while ( x > 0 )
SysLib.writeStdout( x );
x = MathLib.floor( x / 2 );
end</langsyntaxhighlight>
 
=={{header|Elena}}==
ELENA 46.x:
<langsyntaxhighlight lang="elena">public program()
{
int i := 1024;
while (i > 0)
{
console.writeLine:(i);
i /= 2
}
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Loops do
def while(0), do: :ok
def while(n) do
Line 1,173 ⟶ 1,593:
end
 
Loops.while(1024)</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(let ((i 1024))
(while (> i 0)
(message "%d" i)
(setq i (/ i 2))))</langsyntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
int i = 1024
while i > 0
writeLine(i)
i /= 2
end
</syntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">-module(while).
-export([loop/0]).
 
Line 1,193 ⟶ 1,622:
loop(N) when N >0 ->
io:format("~w~n", [N]),
loop(N div 2).</langsyntaxhighlight>
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
I%=1024
WHILE I%>0 DO ! you can leave out >0
Line 1,202 ⟶ 1,631:
I%=I% DIV 2 ! I%=INT(I%/2) for C-64 version
END WHILE
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight Euphorialang="euphoria">integer i
i = 1024
 
Line 1,211 ⟶ 1,640:
printf(1, "%g\n", {i})
i = floor(i/2) --Euphoria does NOT use integer division. 1/2 = 0.5
end while</langsyntaxhighlight>
Even without the <code>floor()</code> the code will in fact end. But it's FAR beyond 1.
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let rec loop n = if n > 0 then printf "%d " n; loop (n / 2)
loop 1024</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">1024 [ dup 0 > ] [ dup . 2 /i ] while drop</langsyntaxhighlight>
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">1024[$0>][$."
"2/]#%</langsyntaxhighlight>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">class Main
{
public static Void main ()
Line 1,238 ⟶ 1,667:
}
}
}</langsyntaxhighlight>
 
=={{header|Fennel}}==
<langsyntaxhighlight lang="fennel">(var n 1024)
(while (> i 0)
(print i)
(set i (// n 2)))</langsyntaxhighlight>
=={{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.
<langsyntaxhighlight lang="fermat">n:=1024;
while n>2/3 do !!n;n:=n/2; od;</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: halving ( n -- )
begin dup 0 >
while cr dup . 2/
repeat drop ;
1024 halving</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">INTEGER :: i = 1024
DO WHILE (i > 0)
WRITE(*,*) i
i = i / 2
END DO</langsyntaxhighlight>
 
{{works with|Fortran|77 and later}}
<langsyntaxhighlight lang="fortran"> PROGRAM LOOPWHILE
INTEGER I
 
Line 1,282 ⟶ 1,711:
ENDIF
STOP
END</langsyntaxhighlight>
 
{{works with|Fortran|IV and 66 and later}}
<langsyntaxhighlight lang="fortran"> PROGRAM LOOPWHILE
INTEGER I
C FORTRAN 66 does not have IF block.
Line 1,296 ⟶ 1,725:
20 CONTINUE
STOP
END</langsyntaxhighlight>
 
=={{header|Fortress}}==
<langsyntaxhighlight lang="fortress">
component loops_while
export Executable
Line 1,309 ⟶ 1,738:
end
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,324 ⟶ 1,753:
2
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>
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">i=1024
while i>0
{
i = i/1
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="gap">n := 1024;
while n > 0 do
Print(n, "\n");
n := QuoInt(n, 2);
od;</langsyntaxhighlight>
 
=={{header|GML}}==
<langsyntaxhighlight GMLlang="gml">i = 1024
while(i > 0)
{
show_message(string(i))
i /= 2
}</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">i := 1024
for i > 0 {
fmt.Printf("%d\n", i)
i /= 2
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">int i = 1024
while (i > 0) {
println i
i /= 2
}</langsyntaxhighlight>
 
{{Out}}
Line 1,446 ⟶ 1,806:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (when)
 
main = loop 1024
where loop n = when (n > 0)
(do print n
loop (n `div` 2))</langsyntaxhighlight>
 
You can use whileM_ function from monad-loops package that operates on monads:
 
<langsyntaxhighlight lang="haskell">import Data.IORef
import Control.Monad.Loops
 
Line 1,464 ⟶ 1,824:
(do n <- readIORef r
print n
modifyIORef r (`div` 2))</langsyntaxhighlight>
 
With MonadComprehensions extension you can write it a little bit more readable:
<langsyntaxhighlight lang="haskell">{-# LANGUAGE MonadComprehensions #-}
import Data.IORef
import Control.Monad.Loops
Line 1,477 ⟶ 1,837:
n <- readIORef r
print n
modifyIORef r (`div` 2)</langsyntaxhighlight>
 
=={{header|Haxe}}==
Using shift right.
<langsyntaxhighlight lang="haxe">var i = 1024;
while (i > 0) {
Sys.println(i);
i >>= 1;
}</langsyntaxhighlight>
 
Using integer division.
<langsyntaxhighlight lang="haxe">var i = 1024;
while (i > 0) {
Sys.println(i);
i = Std.int(i / 2);
}</langsyntaxhighlight>
 
{{out}}
Line 1,512 ⟶ 1,872:
 
=={{header|hexiscript}}==
<langsyntaxhighlight lang="hexiscript">let i 1024
while i > 0
println i
let i (i / 2)
endwhile</langsyntaxhighlight>
 
=={{header|HolyC}}==
<langsyntaxhighlight lang="holyc">U16 i = 1024;
while (i > 0) {
Print("%d\n", i);
i /= 2;
}</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight lang="icon">procedure main()
local i
i := 1024
while write(0 < (i := i / 2))
end</langsyntaxhighlight>
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">let N be 1024;
while N > 0:
say "[N][line break]";
let N be N / 2;</langsyntaxhighlight>
 
=={{headerHeader|IWBASICInsitux}}==
<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</langpre>
Note: Spacing is not an issue. I just find the code to be more readable with spaces.
 
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
 
<langsyntaxhighlight lang="j">,. <.@-:^:*^:a: 1024</langsyntaxhighlight>
 
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).
 
<langsyntaxhighlight lang="j">monad define 1024
while. 0 < y do.
smoutput y
Line 1,579 ⟶ 1,939:
end.
i.0 0
)</langsyntaxhighlight>
 
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}}==
<langsyntaxhighlight lang="java5">int i = 1024;
while(i > 0){
System.out.println(i);
i >>= 1; //also acceptable: i /= 2;
}</langsyntaxhighlight>
With a for loop:
<langsyntaxhighlight lang="java5">for(int i = 1024; i > 0;i /= 2 /*or i>>= 1*/){
System.out.println(i);
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">var n = 1024;
while (n > 0) {
print(n);
n /= 2;
}</langsyntaxhighlight>
 
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,608 ⟶ 1,968:
:#A conditional function, corresponding to the While test
 
<langsyntaxhighlight JavaScriptlang="javascript">function loopWhile(varValue, fnDelta, fnTest) {
'use strict';
var d = fnDelta(varValue);
Line 1,627 ⟶ 1,987:
}
).join('\n')
);</langsyntaxhighlight>
 
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
128
Line 1,640 ⟶ 2,000:
4
2
1</langsyntaxhighlight>
 
=={{header|Joy}}==
<langsyntaxhighlight lang="joy">DEFINE putln == put '\n putch.
1024 [] [dup putln 2 /] while.</syntaxhighlight>
 
1024 [] [dup putln 2 /] while.</lang>
 
=={{header|jq}}==
'''Using recurse/1'''<langsyntaxhighlight lang="jq"># To avoid printing 0, test if the input is greater than 1
1024 | recurse( if . > 1 then ./2 | floor else empty end)</langsyntaxhighlight>
'''Using recurse/2''' (requires jq >1.4)
<langsyntaxhighlight lang="jq">1024 | recurse( ./2 | floor; . > 0)</langsyntaxhighlight>
'''Using a filter'''
<langsyntaxhighlight lang="jq">def task: if . > 0 then ., (./2 | floor | task) else empty end;
1024|task</langsyntaxhighlight>
'''Using while/2'''
 
If your jq does not include while/2 as a builtin, here is its definition:
<langsyntaxhighlight lang="jq">def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
_while;</langsyntaxhighlight>
For example:
<langsyntaxhighlight lang="jq">1024|while(. > 0; ./2|floor)</langsyntaxhighlight>
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Loops/While in Jsish */
var i = 1024;
Line 1,685 ⟶ 2,044:
1
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,692 ⟶ 2,051:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
<lang Julia>
n = 1024
 
Line 1,699 ⟶ 2,058:
n >>= 1
end
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,718 ⟶ 2,077:
Implementation of the task using anonymous function is
given below
<syntaxhighlight lang="k">
<lang K>
{while[x>0; \echo x; x%:2]} 1024
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
Line 1,731 ⟶ 2,090:
value /= 2
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,753 ⟶ 2,112:
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{def loops_while
{lambda {:i}
Line 1,763 ⟶ 2,122:
{loops_while 1024}
-> 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
{lambda {:i}
Line 1,798 ⟶ 2,145:
2
1
</syntaxhighlight>
</lang>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$n = 1024
while($n > 0) {
fn.println($n)
$n //= 2
}
</syntaxhighlight>
 
=={{header|langurLang5}}==
{{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 {
writeln .i
.i \= 2
}</langsyntaxhighlight>
 
{{works with|langur|< 0.8}}
<lang langur>var .i = 1024
for .i > 0 {
writeln .i
.i \= 2
}</lang>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(i = 1024)
while(#i > 0) => {^
#i + '\r'
#i /= 2
^}</langsyntaxhighlight>
 
=={{header|Liberty BASICLDPL}}==
<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}}==
<langsyntaxhighlight lang="tcl">set num 1024; while {$num > 0} {print $num; set num [expr $num \ 2]}</langsyntaxhighlight>
 
Backslash is integer division, otherwise LIL would allow the division to go floating point.
 
=={{header|Lingo}}==
<langsyntaxhighlight lang="lingo">n = 1024
repeat while n>0
put n
n = n/2 -- integer division implicitely returns floor: 1/2 -> 0
end repeat</langsyntaxhighlight>
 
=={{header|Lisaac}}==
<langsyntaxhighlight Lisaaclang="lisaac">+ i : INTEGER;
i := 1024;
{ i > 0 }.while_do {
Line 1,853 ⟶ 2,214:
i := i / 2;
};</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">put 1024 into n
repeat while n > 0
put n & cr
divide n by 2
end repeat</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">make "n 1024
while [:n > 0] [print :n make "n :n / 2]</langsyntaxhighlight>
 
=={{header|LOLCODE}}==
Line 1,870 ⟶ 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.
 
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.3
 
I HAS A n ITZ 1024
Line 1,879 ⟶ 2,240:
IM OUTTA YR loop
 
KTHXBYE</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">n = 1024
while n>0 do
print(n)
n = math.floor(n/2)
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Checkit {
Def long A=1024
Line 1,898 ⟶ 2,259:
}
Checkit
</syntaxhighlight>
</lang>
One line
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Online { A=1024&: While A>0 {Print A: A/=2}} : OnLine
</syntaxhighlight>
</lang>
 
=={{header|m4}}==
This ‘loop’ is really a tail recursion, which m4 implementations generally do ''not'' optimize.
<langsyntaxhighlight lang="m4">divert(-1)
 
define(`loop',
Line 1,913 ⟶ 2,274:
 
divert`'dnl
loop(1024)dnl</langsyntaxhighlight>
{{out}}
$ m4 loops_while.m4
Line 1,929 ⟶ 2,290:
 
=={{header|Make}}==
<langsyntaxhighlight lang="make">NEXT=`expr $* / 2`
MAX=10
 
Line 1,940 ⟶ 2,301:
 
%-echo:
@echo $*</langsyntaxhighlight>
 
Invoking it
<langsyntaxhighlight lang="make">|make -f while.mk MAX=1024</langsyntaxhighlight>
 
=={{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 (/).
<langsyntaxhighlight Maplelang="maple">> n := 1024: while n > 0 do print(n); n := iquo(n,2) end:
1024
512
Line 1,958 ⟶ 2,319:
4
2
1</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight Mathematicalang="mathematica">i = 1024;
While[i > 0,
Print[i];
i = Floor[i/2];
]</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight Matlablang="matlab">i = 1024;
while (i > 0)
disp(i);
i = floor(i/2);
end</langsyntaxhighlight>
 
A vectorized version of the code is
 
<langsyntaxhighlight Matlablang="matlab"> printf('%d\n', 2.^[log2(1024):-1:0]);</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight 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 */
for n: 1024 next quotient(n, 2) while n > 0 do print(n);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">a = 1024
while a > 0 do
(
print a
a /= 2
)</langsyntaxhighlight>
 
=={{header|Metafont}}==
Line 1,998 ⟶ 2,359:
Metafont has no <tt>while</tt> loop, but it can be "simulated" easily.
 
<langsyntaxhighlight lang="metafont">a := 1024;
forever: exitif not (a > 0);
show a;
a := a div 2;
endfor</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
<lang microsoftsmallbasic>
i = 1024
While i > 0
TextWindow.WriteLine(i)
i = Math.Floor(i / 2)
EndWhile
</lang>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">1024 :n (n 0 >) (n puts 2 div @n) while</langsyntaxhighlight>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">i = 1024
while i > 0
print i
i = floor(i/2)
end while</langsyntaxhighlight>
 
{{out}}
Line 2,041 ⟶ 2,393:
=={{header|MIRC Scripting Language}}==
 
<langsyntaxhighlight lang="mirc">alias while_loop {
var %n = 10
while (%n >= 0) {
Line 2,047 ⟶ 2,399:
dec %n
}
}</langsyntaxhighlight>
 
=={{header|MIXAL}}==
<syntaxhighlight lang="mixal">
<lang MIXAL>
******************************************
* X = M / N WHILE X > 0
Line 2,080 ⟶ 2,432:
HLT
END START
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 0 2 4 П0 ИП0 /-/ x<0 15 ИП0
2 / П0 БП 05 С/П</langsyntaxhighlight>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE DivBy2;
IMPORT InOut;
 
Line 2,099 ⟶ 2,451:
i := i DIV 2
END
END DivBy2.</langsyntaxhighlight>
 
=={{header|Modula-3}}==
The usual module code and imports are omitted.
<langsyntaxhighlight lang="modula3">PROCEDURE DivBy2() =
VAR i: INTEGER := 1024;
BEGIN
Line 2,111 ⟶ 2,463:
i := i DIV 2;
END;
END DivBy2;</langsyntaxhighlight>
 
=={{header|Monte}}==
 
<syntaxhighlight lang="monte">
<lang Monte>
var i := 1024
while (i > 0):
traceln(i)
i //= 2
</syntaxhighlight>
</lang>
 
=={{header|MOO}}==
<langsyntaxhighlight lang="moo">i = 1024;
while (i > 0)
player:tell(i);
i /= 2;
endwhile</langsyntaxhighlight>
 
=={{header|Morfa}}==
<langsyntaxhighlight lang="morfa">
import morfa.io.print;
 
Line 2,139 ⟶ 2,491:
i /= 2;
}
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight lang="nanoquery">$n = 1024
while ($n > 0)
println $n
$n = $n/2
end while</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="neko">
<lang Neko>
var i = 1024
 
Line 2,156 ⟶ 2,508:
i = $idiv(i, 2)
}
</syntaxhighlight>
</lang>
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">mutable x = 1024;
while (x > 0)
{
WriteLine($"$x");
x /= 2;
}</langsyntaxhighlight>
Or, with immutable types, after Haskell:
<langsyntaxhighlight Nemerlelang="nemerle">// within another function, eg Main()
def loop(n : int) : void
{
Line 2,176 ⟶ 2,528:
}
loop(1024)</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
 
Line 2,189 ⟶ 2,541:
say x_.right(6)
x_ = x_ % 2 -- integer division
end</langsyntaxhighlight>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(let (i 1024)
(while (> i 0)
(println i)
(setq i (/ i 2))))</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">var n: int = 1024
while n > 0:
echo(n)
n = n div 2</langsyntaxhighlight>
 
=={{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}}==
The usual module code and imports are ommited.
<langsyntaxhighlight lang="oberon2">PROCEDURE DivBy2*();
VAR i: INTEGER;
BEGIN
Line 2,221 ⟶ 2,566:
i := i DIV 2;
END;
END DivBy2;</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">i := 1024;
while(i > 0) {
i->PrintLine();
i /= 2;
};</langsyntaxhighlight>
 
=={{header|ObjectIcon}}==
Line 2,234 ⟶ 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''.
 
<langsyntaxhighlight lang="objecticon">import io
 
procedure main()
Line 2,240 ⟶ 2,585:
n := 1024
while n := write(0 < n) / 2
end</langsyntaxhighlight>
 
{{out}}
Line 2,257 ⟶ 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.
<langsyntaxhighlight lang="objecticon">import io
 
procedure main()
Line 2,267 ⟶ 2,612:
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
end</langsyntaxhighlight>
 
To show what I mean, let’s do some (merely illustrative) loop-unrolling where the output gets written.
<langsyntaxhighlight lang="objecticon">import io
 
procedure main()
Line 2,287 ⟶ 2,632:
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
end</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let n = ref 1024;;
while !n > 0 do
Printf.printf "%d\n" !n;
n := !n / 2
done;;</langsyntaxhighlight>
 
But it is more common to write it in a tail-recursive functional style:
<langsyntaxhighlight lang="ocaml">let rec loop n =
if n > 0 then begin
Printf.printf "%d\n" n;
loop (n / 2)
end
in loop 1024</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">i = 1024;
while (i > 0)
disp(i)
i = floor(i/2);
endwhile</langsyntaxhighlight>
 
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}}==
 
<langsyntaxhighlight Oforthlang="oforth">1024 while ( dup ) [ dup println 2 / ]</langsyntaxhighlight>
 
=={{header|OOC}}==
<langsyntaxhighlight lang="ooc">
main: func {
value := 1024
Line 2,326 ⟶ 2,687:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
Oz' for-loop can be used in a C-like manner:
<langsyntaxhighlight lang="oz">for I in 1024; I>0; I div 2 do
{Show I}
end</langsyntaxhighlight>
 
Alternatively, we can use the <code>while</code> feature of the for-loop with a mutable variable:
<langsyntaxhighlight lang="oz">declare
I = {NewCell 1024}
in
Line 2,341 ⟶ 2,702:
{Show @I}
I := @I div 2
end</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="panda">fun half(a) type integer->integer a.divide(2)
1024.trans(func:half).gt(0) nl
</syntaxhighlight>
</lang>
 
=={{header|Panoramic}}==
<langsyntaxhighlight Panoramiclang="panoramic">dim x%:rem an integer
 
x%=1024
Line 2,363 ⟶ 2,724:
rem output starts with 1024 and ends with 1.
 
terminate</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">n=1024;
while(n,
print(n);
n/=2
);</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">program divby2(output);
 
var
Line 2,385 ⟶ 2,746:
i := i div 2
end
end.</langsyntaxhighlight>
 
=={{header|PeopleCode}}==
<syntaxhighlight lang="peoplecode">
<lang PeopleCode>
Local string &CRLF;
Local number &LoopNumber;
Line 2,398 ⟶ 2,759:
&LoopNumber = &LoopNumber / 2;
End-While;
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my $n = 1024;
while($n){
print "$n\n";
$n = int $n / 2;
}</langsyntaxhighlight>
 
or written as a for-loop and using the bit-shift operator
 
<langsyntaxhighlight lang="perl">for(my $n = 1024; $n > 0; $n >>= 1){
print "$n\n";
}</langsyntaxhighlight>
 
<code>until (''condition'')</code> is equivalent to <code>while (not ''condition'')</code>.
 
<langsyntaxhighlight lang="perl">my $n = 1024;
until($n == 0){
print "$n\n";
$n = int $n / 2;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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: #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,428 ⟶ 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: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
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}}==
 
<langsyntaxhighlight lang="phl">var i = 1024;
while (i > 0) {
printf("%i\n", i);
i = i/2;
}</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$i = 1024;
while ($i > 0) {
echo "$i\n";
$i >>= 1;
}</langsyntaxhighlight>
 
=={{header|Picat}}==
While loop
<langsyntaxhighlight Picatlang="picat">go =>
N = 1024,
while (N > 0)
println(N),
N := N // 2
end.</langsyntaxhighlight>
 
Recursion
{{trans|Prolog}}
<langsyntaxhighlight Picatlang="picat">go2 =>
while_loop(1024).
 
Line 2,463 ⟶ 2,846:
while_loop(N) =>
println(N),
while_loop(N//2).</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let N 1024
(while (gt0 N)
(println N)
(setq N (/ N 2)) ) )</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">int main(){
int i = 1024;
while(i > 0){
Line 2,479 ⟶ 2,862:
i = i / 2;
}
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight PLlang="pl/Ii">declare i fixed binary initial (1024);
 
do while (i>0);
put skip list (i);
i = i / 2;
end;</langsyntaxhighlight>
 
=={{header|PL/SQL}}==
{{works with|Oracle}}
<langsyntaxhighlight lang="plsql">
set serveroutput on
declare
Line 2,502 ⟶ 2,912:
end;
/
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Show the halvings of 1024.
Line 2,516 ⟶ 2,926:
Write the string to the console.
Divide the number by 2.
Repeat.</langsyntaxhighlight>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">lvars i = 1024;
while i > 0 do
printf(i, '%p\n');
i div 2 -> i;
endwhile;</langsyntaxhighlight>
 
=={{header|PostScript}}==
PostScript has no real <code>while</code> loop,
but it can easily be created with an endless loop and a check at the beginning:
<langsyntaxhighlight lang="postscript">1024
{
dup 0 le % check whether still greater than 0
Line 2,536 ⟶ 2,946:
2 idiv % divide by two
}
loop</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">[int]$i = 1024
while ($i -gt 0) {
$i
$i /= 2
}</langsyntaxhighlight>
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">while(0) :- !.
while(X) :-
writeln(X),
X1 is X // 2,
while(X1).</langsyntaxhighlight>
 
Start the calculation at a top-level like this:
 
<syntaxhighlight lang ="prolog">?- while(1024).</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="python">n = 1024
while n > 0:
print n
n //= 2</langsyntaxhighlight>
 
=={{header|QB64}}==
<lang qb64>Dim n As Integer
n = 1024
While n > 0
Print n
n = n \ 2
Wend</lang>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">1024
 
<lang Quackery>1024
[ dup 0 > while
dup echo cr 2 /
again ]
drop</langsyntaxhighlight>
 
{{Out}}
Line 2,609 ⟶ 2,996:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">i <- 1024L
while(i > 0)
{
print(i)
i <- i %/% 2
}</langsyntaxhighlight>
 
=={{header|Racket}}==
===Loop/When===
<langsyntaxhighlight lang="racket">#lang racket
(let loop ([n 1024])
(when (positive? n)
(displayln n)
(loop (quotient n 2))))</langsyntaxhighlight>
 
===Macro===
<langsyntaxhighlight lang="racket">#lang racket
(define-syntax-rule (while condition body ...)
(let loop ()
Line 2,635 ⟶ 3,022:
(while (positive? n)
(displayln n)
(set! n (sub1 n)))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 2,641 ⟶ 3,028:
 
Here is a straightforward translation of the task description:
<syntaxhighlight lang="raku" perl6line>my $n = 1024; while $n > 0 { say $n; $n div= 2 }</langsyntaxhighlight>
 
The same thing with a C-style loop and a bitwise shift operator:
<syntaxhighlight lang="raku" perl6line>loop (my $n = 1024; $n > 0; $n +>= 1) { say $n }</langsyntaxhighlight>
 
And here's how you'd <em>really</em> write it, using a sequence operator that intuits the division for you:
 
<syntaxhighlight lang="raku" perl6line>.say for 1024, 512, 256 ... 1</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">REBOL [
Title: "Loop/While"
URL: http://rosettacode.org/wiki/Loop/While
Line 2,660 ⟶ 3,047:
print value
value: to-integer value / 2
]</langsyntaxhighlight>
 
 
=={{header|ReScript}}==
<langsyntaxhighlight ReScriptlang="rescript">let n = ref(1024)
while n.contents > 0 {
Js.log(n.contents)
n := n.contents / 2
}</langsyntaxhighlight>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">1024 [ cr &putn sip 2 / dup ] while</langsyntaxhighlight>
 
=={{header|REXX}}==
===version 1, simple===
<langsyntaxhighlight lang="rexx">/*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.*/
Line 2,681 ⟶ 3,068:
j=j%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,702 ⟶ 3,089:
::::: '''DO WHILE x\==0'''
but that wouldn't be compliant with the wording of the task.
<langsyntaxhighlight lang="rexx">/*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.*/
Line 2,708 ⟶ 3,095:
x=x%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
{{out}}
<pre>
Line 2,725 ⟶ 3,112:
 
===version 3, faster WHILE comparison===
<langsyntaxhighlight lang="rexx">/*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. */
Line 2,731 ⟶ 3,118:
x=x%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
'''output''' is the same as version 2.
<br>
 
===version 4, index reduction===
<langsyntaxhighlight lang="rexx">/*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. */
Line 2,742 ⟶ 3,129:
j=j%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</langsyntaxhighlight>
'''output''' is the same as version 2.
<br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
i = 1024
while i > 0
Line 2,753 ⟶ 3,140:
i = floor(i / 2)
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}}==
<langsyntaxhighlight lang="ruby">i = 1024
while i > 0 do
puts i
i /= 2
end</langsyntaxhighlight>
The above can be written in one statement:
<langsyntaxhighlight lang="ruby">puts i = 1024
puts i /= 2 while i > 0</langsyntaxhighlight>
 
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>.
 
<langsyntaxhighlight lang="ruby">i = 1024
until i <= 0 do
puts i
i /= 2
end</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<lang runbasic>i = 1024
while i > 0
print i
i = int(i / 2)
wend
end</lang>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut n: i32 = 1024;
while n > 0 {
Line 2,788 ⟶ 3,184:
n /= 2;
}
}</langsyntaxhighlight>
 
=={{header|SAS}}==
<langsyntaxhighlight lang="sas">data _null_;
n=1024;
do while(n>0);
Line 2,797 ⟶ 3,193:
n=int(n/2);
end;
run;</langsyntaxhighlight>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class MAIN is
main is
i ::= 1024;
Line 2,808 ⟶ 3,204:
end;
end;
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
===Imperative===
<langsyntaxhighlight lang="scala">var i = 1024
while (i > 0) {
println(i)
i /= 2
}</langsyntaxhighlight>
 
===Tail recursive===
<langsyntaxhighlight lang="scala"> @tailrec
def loop(iter: Int) {
if (iter > 0) {
Line 2,827 ⟶ 3,223:
}
}
loop(1024)</langsyntaxhighlight>
 
===Iterator===
<langsyntaxhighlight lang="scala"> 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(_))</langsyntaxhighlight>
 
===Stream===
Finite stream (1024..0) filtered by takeWhile (1024..1).
<langsyntaxhighlight lang="scala"> def loop(i: Int): Stream[Int] = i #:: (if (i > 0) loop(i / 2) else Stream.empty)
loop(1024).takeWhile(_ > 0).foreach(println(_))</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(do ((n 1024 (quotient n 2)))
((<= n 0))
(display n)
(newline))</langsyntaxhighlight>
 
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">i=1024
while i>0
printf("%4d\n",i)
i=int(i/2)
end</langsyntaxhighlight>
{{out}}
<pre>1024
Line 2,869 ⟶ 3,265:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,879 ⟶ 3,275:
i := i div 2
end while;
end func;</langsyntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">put 1024 into x
log x
Repeat until x = 1
divide x by 2
log x
End repeat</langsyntaxhighlight>
 
=={{header|SETL}}==
<langsyntaxhighlight lang="ada">n := 1024;
while n > 0 loop
print( n );
n := n div 2;
end loop;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var i = 1024
while (i > 0) {
say i
i //= 2
}</langsyntaxhighlight>
 
=={{header|Simula}}==
{{works with|SIMULA-67}}
<langsyntaxhighlight lang="simula">begin
integer i;
i:=1024;
Line 2,913 ⟶ 3,323:
i:=i//2-1
end
end</langsyntaxhighlight>
{{out}}
<pre>
 1024  511  254  126   62   30   14    6    2 
</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}}==
<langsyntaxhighlight lang="slate">#n := 1024.
[n isPositive] whileTrue:
[inform: number printString.
n := n // 2]</langsyntaxhighlight>
 
=={{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).
 
<langsyntaxhighlight lang="smalltalk ">[s atEnd] whileFalse: [s next. ...].
[foo notNil] whileTrue: [s next. ...].
[...] doWhile: [ ... someBooleanExpression ].
[...] doUntil: [ ... someBooleanExpression ].
[:exit | ... cold ifTrue:[exit value]. ...] loopWithExit</langsyntaxhighlight>
 
Examples:
<langsyntaxhighlight lang="smalltalk">number := 1024.
[ number > 0 ] whileTrue:
[ Transcript print: number; nl.
number := number // 2 ]</langsyntaxhighlight>
 
<langsyntaxhighlight lang="smalltalk">number := 1024.
[ number <= 0 ] whileFalse:
[ Transcript print: number; nl.
number := number // 2 ]</langsyntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">var i = 1024;
while i > 0 {
print(i);
i /= 2;
}</langsyntaxhighlight>
 
=={{header|Spin}}==
Line 2,967 ⟶ 3,367:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<langsyntaxhighlight lang="spin">con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,985 ⟶ 3,385:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,992 ⟶ 3,392:
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">n = 1024
>
#.output(n)
n /= 2
< n!<1</langsyntaxhighlight>
{{out}}
<pre>
Line 3,015 ⟶ 3,415:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<langsyntaxhighlight lang="sql pl">
--#SET TERMINATOR @
 
Line 3,028 ⟶ 3,428:
END WHILE Loop;
END @
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,053 ⟶ 3,453:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">val n = ref 1024;
while !n > 0 do (
print (Int.toString (!n) ^ "\n");
n := !n div 2
)</langsyntaxhighlight>
 
But it is more common to write it in a tail-recursive functional style:
<langsyntaxhighlight lang="sml">let
fun loop n =
if n > 0 then (
Line 3,068 ⟶ 3,468:
in
loop 1024
end</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">local n=1024
while `n'>0 {
display `n'
local n=floor(`n'/2)
}</langsyntaxhighlight>
 
=={{header|Suneido}}==
<langsyntaxhighlight Suneidolang="suneido">i = 1024
while (i > 0)
{
Print(i)
i = (i / 2).Floor()
}</langsyntaxhighlight>
{{Out}}
<pre>1024
Line 3,098 ⟶ 3,498:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">var i = 1024
while i > 0 {
println(i)
i /= 2
}</langsyntaxhighlight>
 
=={{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.
<langsyntaxhighlight lang="tailspin">
1024 -> \(
<0~..> '$;$#10;' -> !OUT::write
$ ~/ 2 -> #
\) -> !VOID
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">set i 1024
while {$i > 0} {
puts $i
set i [expr {$i / 2}]
}</langsyntaxhighlight>
 
=={{header|TeX}}==
 
<langsyntaxhighlight TeXlang="tex">\newcount\rosetta
\rosetta=1024
\loop
Line 3,129 ⟶ 3,529:
\ifnum\rosetta > 0
\repeat
\end</langsyntaxhighlight>
 
=={{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}}==
This has to make use of mFloor because torque has automatic type shuffling,
causing an infiniteloop.
<langsyntaxhighlight Torquelang="torque">%num = 1024;
while(%num > 0)
{
echo(%num);
%num = mFloor(%num / 2);
}</langsyntaxhighlight>
 
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
<lang Transact-SQL>
DECLARE @i INT = 1024;
WHILE @i >0
Line 3,167 ⟶ 3,549:
SET @i = @i / 2;
END;
</syntaxhighlight>
</lang>
 
=={{header|Trith}}==
<langsyntaxhighlight lang="trith">1024 [dup print 2 / floor] [dup 0 >] while drop</langsyntaxhighlight>
<langsyntaxhighlight lang="trith">1024 [dup print 1 shr] [dup 0 >] while drop</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
i=1024
LOOP
Line 3,180 ⟶ 3,562:
i=i/2
IF (i==0) EXIT
ENDLOOP</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,201 ⟶ 3,583:
=={{header|Uniface}}==
 
<langsyntaxhighlight Unifacelang="uniface">variables
numeric I
endvariables
Line 3,209 ⟶ 3,591:
putmess I
I = (I/2)[trunc]
endwhile</langsyntaxhighlight>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
<langsyntaxhighlight lang="bash">x=1024
while [[ $x -gt 0 ]]; do
echo $x
x=$(( $x/2 ))
done</langsyntaxhighlight>
 
=={{header|UnixPipes}}==
<langsyntaxhighlight 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
done</langsyntaxhighlight>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl int n
set n 1024
 
Line 3,231 ⟶ 3,613:
out n endl console
set n (int (/ n 2))
end while</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 3,249 ⟶ 3,631:
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.
<langsyntaxhighlight Ursalalang="ursala">#import nat
 
g = ~&h-> ^C/half@h ~&
Line 3,255 ⟶ 3,637:
#show+
 
main = %nP*=tx g <1024></langsyntaxhighlight>
{{Out}}
<pre>
Line 3,273 ⟶ 3,655:
The same output is produced by the following main program
using bit manipulation.
<langsyntaxhighlight Ursalalang="ursala">main = %nP*=tK33 1024</langsyntaxhighlight>
 
=={{header|V}}==
<langsyntaxhighlight lang="v">1024 [0 >] [
dup puts
2 / >int
] while</langsyntaxhighlight>
 
=={{header|Vala}}==
<langsyntaxhighlight lang="vala">int i = 1024;
while (i > 0) {
stdout.printf("%d\n", i);
i /= 2;
}</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="vedit">#1 = 1024
while (#1 > 0) {
Num_Type(#1)
#1 /= 2
}</langsyntaxhighlight>
or with for loop:
<langsyntaxhighlight lang="vedit">for (#1 = 1024; #1 > 0; #1 /= 2) {
Num_Type(#1)
}</langsyntaxhighlight>
 
=={{header|Verbexx}}==
<langsyntaxhighlight lang="verbexx">// Basic @LOOP while: verb
 
@LOOP init:{@VAR n = 1024} while:(n > 0) next:{n /= 2}
{
@SAY n;
};</langsyntaxhighlight>
 
 
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
<lang Verilog>
module main;
integer i;
Line 3,333 ⟶ 3,705:
end
endmodule
</syntaxhighlight>
</lang>
 
 
=={{header|Vim Script}}==
<langsyntaxhighlight lang="vim">let i = 1024
while i > 0
echo i
let i = i / 2
endwhile</langsyntaxhighlight>
 
=={{header|VisualV Basic .NET(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
for i > 0 {
Line 3,357 ⟶ 3,722:
i /= 2
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,375 ⟶ 3,740:
 
=={{header|Wart}}==
<langsyntaxhighlight lang="wart">i <- 1024
while (i > 0)
prn i
i <- (int i/2)</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight Whitespacelang="whitespace">
 
Line 3,405 ⟶ 3,762:
 
 
</syntaxhighlight>
</lang>
Pseudo-assembly equivalent:
<langsyntaxhighlight lang="asm">push 1024
 
0:
Line 3,414 ⟶ 3,771:
push 0 swap sub
jn 0
pop exit</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var i = 1024
while (i > 0) {
System.print(i)
i = (i / 2).floor
}</langsyntaxhighlight>
 
{{out}}
Line 3,439 ⟶ 3,796:
 
=={{header|X86 Assembly}}==
<langsyntaxhighlight lang="asm">
; NASM 64 bit X86-64 assembly on Linux
 
Line 3,480 ⟶ 3,837:
leave ; fix stack
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}}==
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.
<langsyntaxhighlight lang="xlisp">(DEFINE I 1024)
 
(WHILE (>= I 1)
(PRINT I)
(DEFINE I (/ I 2)))</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code CrLf=9, IntOut=11;
int I;
[I:= 1024;
Line 3,508 ⟶ 3,855:
I:= I>>1; \(same as I/2 for positive I)
];
]</langsyntaxhighlight>
 
=={{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}}==
<syntaxhighlight lang="zig">
<lang Zig>
const std = @import("std");
 
Line 3,531 ⟶ 4,037:
std.debug.print("{}\n", .{i});
}
</syntaxhighlight>
</lang>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">n:=1024; while(n>0){println(n); n/=2;}</langsyntaxhighlight>
{{out}}
<pre>
57

edits