Loops/While: Difference between revisions

DIalects of BASIC moved to the BASIC section.
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(DIalects of BASIC moved to the BASIC section.)
Line 10:
Print the value (with a newline) and divide it by two each time through the loop.
 
 
;Related tasks:
Line 718 ⟶ 717:
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|IS-BASIC}}===
Line 725 ⟶ 793:
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 751 ⟶ 867:
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|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|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}}===
Line 784 ⟶ 963:
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,363 ⟶ 1,585:
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,593:
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,576 ⟶ 1,729:
say "[N][line break]";
let N be N / 2;</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|J}}==
Line 1,861 ⟶ 1,986:
#i /= 2
^}</syntaxhighlight>
 
=={{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.
<syntaxhighlight lang="lb">i = 1024
while i > 0
print i
i = int( i / 2)
wend
end</syntaxhighlight>
 
=={{header|LIL}}==
Line 2,041 ⟶ 2,156:
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,346:
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,609 ⟶ 2,708:
 
<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,714:
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,825 ⟶ 2,901:
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,972 ⟶ 3,040:
 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,242:
\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,381:
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,396 ⟶ 3,426:
let i = i / 2
endwhile</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vbnet">Dim x = 1024
Do
Console.WriteLine(x)
x = x \ 2
Loop While x > 0</syntaxhighlight>
 
=={{header|V (Vlang)}}==
Line 3,433 ⟶ 3,456:
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,534 ⟶ 3,549:
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,563 ⟶ 3,568:
];
]</syntaxhighlight>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">i = 1024
while i > 0
Print i
i = int(i / 2)
wend
 
end</syntaxhighlight>
 
 
=={{header|Zig}}==
Line 3,586 ⟶ 3,579:
}
</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">n:=1024; while(n>0){println(n); n/=2;}</syntaxhighlight>
511

edits