Loops/While: Difference between revisions

9,071 bytes added ,  2 months ago
m
added output
m (added output)
 
(28 intermediate revisions by 18 users not shown)
Line 10:
Print the value (with a newline) and divide it by two each time through the loop.
 
 
;Related tasks:
Line 313 ⟶ 312:
end;
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}}==
Line 596 ⟶ 632:
A/2→A
End</syntaxhighlight>
 
=={{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}}===
<syntaxhighlight 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</syntaxhighlight>
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}}===
Line 611 ⟶ 677:
 
==={{header|ASIC}}===
Look also [[#BASIC|BASIC]].
<syntaxhighlight lang="basic">
REM Loops/While
Line 639 ⟶ 704:
 
==={{header|BaCon}}===
Look also [[#BASIC|BASIC]].
<syntaxhighlight lang="freebasic">
i = 1024
Line 646 ⟶ 710:
i = i / 2
WEND</syntaxhighlight>
 
==={{header|Ballerina}}===
<syntaxhighlight lang="ballerina">int i = 1024;
while i > 0 {
io:println(i);
i = i / 2;
}
</syntaxhighlight>
 
==={{header|BASIC256}}===
Line 664 ⟶ 736:
i% DIV= 2
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}}===
Line 718 ⟶ 805:
END</syntaxhighlight>
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}}===
Line 725 ⟶ 893:
130 LET I=IP(I/2)
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}}===
Line 730 ⟶ 946:
{{works with|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="gwbasic">10 REM Loops/While
10 REM Loops/While
20 LET I = 1024
40 IF I <= 0 THEN 80
Line 737 ⟶ 952:
60 LET I = INT(I/2)
70 GOTO 40
80 END</syntaxhighlight>
 
</syntaxhighlight>
==={{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}}===
Line 751 ⟶ 974:
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="tinybasic"> REM Loops/While
<syntaxhighlight lang="basic">10 REM Loops/While
LET I = 1024
1020 IFLET I <= 0 THEN GOTO 201024
30 IF I <= 0 THEN GOTO 70
PRINT I
40 PRINT I
LET I = I / 2
50 LET I = GOTOI 10/ 2
60 GOTO 30
20 END</syntaxhighlight>
70 END</syntaxhighlight>
{{out}}
<pre>
Line 784 ⟶ 1,104:
LOOP
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}}==
Line 1,047 ⟶ 1,410:
<syntaxhighlight lang="dao">i = 1024;
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}}==
Line 1,191 ⟶ 1,572:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">public program()
{
Line 1,197 ⟶ 1,578:
while (i > 0)
{
console.writeLine:(i);
i /= 2
Line 1,219 ⟶ 1,600:
(message "%d" i)
(setq i (/ i 2))))</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
int i = 1024
while i > 0
writeLine(i)
i /= 2
end
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 1,363 ⟶ 1,753:
2
1
</pre>
 
=={{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>
 
Line 1,398 ⟶ 1,761:
i = i/1
}</syntaxhighlight>
 
=={{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|GAP}}==
Line 1,577 ⟶ 1,898:
let N be N / 2;</syntaxhighlight>
 
=={{headerHeader|IWBASICInsitux}}==
<syntaxhighlight 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.
</pre>
END</syntaxhighlight>
Note: Spacing is not an issue. I just find the code to be more readable with spaces.
 
=={{header|J}}==
Line 1,803 ⟶ 2,124:
</syntaxhighlight>
 
Alternative solution:
=={{header|Lang5}}==
{{trans|Factor}}
<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>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def while
Line 1,837 ⟶ 2,146:
1
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$n = 1024
while($n > 0) {
fn.println($n)
$n //= 2
}
</syntaxhighlight>
 
=={{header|Lang5}}==
{{trans|Factor}}
<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>
 
=={{header|langur}}==
0.8 changed the keyword for a test only loop from for to while.
 
{{works with|langur|0.8}}
<syntaxhighlight lang="langur">var .i = 1024
while .i > 0 {
writeln .i
.i \= 2
}</syntaxhighlight>
 
{{works with|langur|< 0.8}}
<syntaxhighlight lang="langur">var .i = 1024
for .i > 0 {
writeln .i
.i \= 2
Line 1,862 ⟶ 2,183:
^}</syntaxhighlight>
 
=={{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.
 
<syntaxhighlight 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</syntaxhighlight>
floor n
repeat</syntaxhighlight>
 
=={{header|LIL}}==
Line 2,041 ⟶ 2,364:
a := a div 2;
endfor</syntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
<syntaxhighlight lang="microsoftsmallbasic">
i = 1024
While i > 0
TextWindow.WriteLine(i)
i = Math.Floor(i / 2)
EndWhile
</syntaxhighlight>
 
=={{header|min}}==
Line 2,240 ⟶ 2,554:
echo(n)
n = n div 2</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|Oberon-2}}==
Line 2,484 ⟶ 2,791:
<!--</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
 
=={{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}}==
Line 2,534 ⟶ 2,863:
}
}</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}}==
Line 2,609 ⟶ 2,965:
 
<syntaxhighlight lang="prolog">?- while(1024).</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|Python}}==
Line 2,629 ⟶ 2,971:
print n
n //= 2</syntaxhighlight>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">Dim n As Integer
n = 1024
While n > 0
Print n
n = n \ 2
Wend</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery">1024
[ dup 0 > while
Line 2,808 ⟶ 3,141:
end
</syntaxhighlight>
 
=={{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}}==
Line 2,825 ⟶ 3,175:
puts i
i /= 2
end</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">i = 1024
while i > 0
print i
i = int(i / 2)
wend
end</syntaxhighlight>
 
Line 2,934 ⟶ 3,276:
end while;
end func;</syntaxhighlight>
{{out}}
<pre>
1024
512
256
128
64
32
16
8
4
2
1
</pre>
 
=={{header|SenseTalk}}==
Line 2,972 ⟶ 3,328:
 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.
<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|Slate}}==
Line 3,184 ⟶ 3,530:
\repeat
\end</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|TorqueScript}}==
Line 3,341 ⟶ 3,669:
i /= 2;
}</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|Vedit macro language}}==
Line 3,397 ⟶ 3,715:
endwhile</syntaxhighlight>
 
=={{header|VisualV Basic .NET(Vlang)}}==
<syntaxhighlight lang="vbnetv (vlang)">Dim xfn =main() 1024{
Do
Console.WriteLine(x)
x = x \ 2
Loop While x > 0</syntaxhighlight>
 
=={{header|Vlang}}==
<syntaxhighlight lang="vlang">fn main() {
mut i := 1024
for i > 0 {
Line 3,433 ⟶ 3,744:
prn i
i <- (int i/2)</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|Whitespace}}==
Line 3,471 ⟶ 3,774:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var i = 1024
while (i > 0) {
System.print(i)
Line 3,534 ⟶ 3,837:
leave ; fix stack
ret ; return
</syntaxhighlight>
 
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">
i% = 1024
DO WHILE i% > 0
PRINT i%
i% = i% / 2
LOOP
</syntaxhighlight>
 
Line 3,564 ⟶ 3,857:
]</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
<syntaxhighlight lang="yabasic">i = 1024
;
 
while i > 0
bdos equ 05h ; Call to CP/M BDOS function
Print i
strdel equ 6eh ; Set string delimiter
i = int(i / 2)
wrtstr equ 09h ; Write string to console
wend
 
nul equ 00h ; ASCII control characters
end</syntaxhighlight>
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}}==
Line 3,586 ⟶ 4,038:
}
</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">n:=1024; while(n>0){println(n); n/=2;}</syntaxhighlight>
57

edits