Loops/While: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Joy}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 31:
=={{header|0815}}==
<
=={{header|11l}}==
{{trans|Python}}
<
L n > 0
print(n)
n I/= 2</
=={{header|360 Assembly}}==
;Basic
Using binary arithmetic. Convert results to EBCDIC printable output.
<
WHILELOO CSECT program's control section
USING WHILELOO,12 set base register
Line 62:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
END WHILELOO</
{{out}} (+ sign indicates "problem state" (non system key) issued WTO's
<pre style="height:16ex">
Line 78:
</pre>
;Structured Macros
<
WHILELOO CSECT
USING WHILELOO,12 set base register
Line 95:
WTOLEN DC AL2(8),H'0' length of wto buffer (4+1)
WTOTXT DC CL4' ' wto text
END WHILELOO</
{{out}}
Same as above
Line 101:
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR LoopsWhile). Specific OS/hardware routines for printing are left unimplemented.
<
LDA #$00 ;the 6502 is an 8-bit processor
Line 117:
EndLoop: PLA ;restore accumulator from stack
RTS ;return from subroutine</
=={{header|68000 Assembly}}==
Printing routines are left out since this is not the focus of the task.
<
MOVE.W #1024,D0
Line 127:
jsr PrintHexWord
LSR.W #1,D0
BNE WhileLoop</
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopwhile64.s */
Line 187:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 203:
</pre>
=={{header|Action!}}==
<
CARD i=[1024]
Line 211:
i=i/2
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/While.png Screenshot from Atari 8-bit computer]
Line 229:
=={{header|ActionScript}}==
<
while (i > 0) {
trace(i);
i /= 2;
}</
=={{header|Ada}}==
<
I : Integer := 1024;
begin
Line 243:
I := I / 2;
end loop;
end;</
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<
local i := 1024;
while i > 0 do
Line 253:
i := i \ 2
od
epocs</
=={{header|Aime}}==
<
i = 1024;
Line 262:
o_plan(i, "\n");
i /= 2;
}</
=={{header|ALGOL 60}}==
The Loops/While structure was in the Algol 60 report of January 1963.
<
comment Loops/While - algol60 - 21/10/2014;
integer i;
for i:=1024,i div 2 while i>0 do outinteger(1,i)
end
</syntaxhighlight>
{{Out}}
<pre>
Line 283:
{{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]}}
<
WHILE i > 0 DO
print(i);
i := i OVER 2
OD</
{{Out}}
<pre>
Line 294:
=={{header|ALGOL W}}==
<
integer i;
i := 1024;
Line 302:
i := i div 2
end
end.</
=={{header|ALGOL-M}}==
<
integer i;
i := 1024;
Line 312:
i := i / 2;
end;
end</
=={{header|AmbientTalk}}==
Line 318:
Both arguments to this message must be blocks (aka anonymous functions or thunks).
<
def i := 1024;
while: { i > 0 } do: {
system.print(" "+i);
i := i/2;
}</
=={{header|AmigaE}}==
<
DEF i = 1024
WHILE i > 0
Line 332:
i := i / 2
ENDWHILE
ENDPROC</
=={{header|AppleScript}}==
AppleScript does not natively support a standard out.
Use the Script Editor's Event Log as the output.
<
repeat while i > 0
log i
set i to i / 2
end repeat</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program loopwhile.s */
Line 466:
</syntaxhighlight>
=={{header|ArnoldC}}==
<
HEY CHRISTMAS TREE n
YOU SET US UP 1024
Line 479:
ENOUGH TALK
CHILL
YOU HAVE BEEN TERMINATED</
=={{header|Arturo}}==
<
while [i>0] [
print i
i: i/2
]</
{{out}}
Line 504:
=={{header|Asymptote}}==
<
while(i > 0) {
Line 513:
//# 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!</
Line 519:
With compile-time proof of termination of the loop (which is really a tail recursion).
<
fn
Line 538:
implement
main0 () =
loop_while ()</
{{out}}
Line 554:
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.
<
fn
Line 570:
implement
main0 () =
loop_while ()</
=={{header|AutoHotkey}}==
<
While (i > 0)
{
Line 579:
i := Floor(i / 2)
}
MsgBox % output</
=={{header|AWK}}==
<
v = 1024
while(v > 0) {
Line 588:
v = int(v/2)
}
}</
=={{header|Axe}}==
<
While A>0
Disp A▶Dec,i
A/2→A
End</
=={{header|BASIC}}==
{{works with|QuickBasic|4.5}}
{{works with|ASIC}}
<
while i > 0
print i
i = i / 2
wend</
==={{header|Applesoft BASIC}}===
<
20 IF I% > 0 THEN PRINT I%:I% = I% / 2: GOTO 20</
==={{header|ASIC}}===
Look also [[#BASIC|BASIC]].
<
REM Loops/While
Line 622:
END
</syntaxhighlight>
{{out}}
<pre>
Line 640:
==={{header|BaCon}}===
Look also [[#BASIC|BASIC]].
<
i = 1024
WHILE i > 0
PRINT i
i = i / 2
WEND</
==={{header|BASIC256}}===
<
while i > 0
Line 655:
end while
end</
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<
WHILE i%
PRINT i%
i% DIV= 2
ENDWHILE</
==={{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.
<
20 IF N% = 0 THEN 60
30 PRINT N%
40 N% = N%/2
50 GOTO 20
60 END</
{{out}}
<pre>
Line 690:
==={{header|Creative Basic}}===
<
X=1024
Line 716:
'Since this is, in fact, a Creative Basic console program.
END</
Note: Spacing is not an issue. I just find the code to be more readable with spaces.
==={{header|IS-BASIC}}===
<
110 DO WHILE I>0
120 PRINT I
130 LET I=IP(I/2)
140 LOOP</
==={{header|Minimal BASIC}}===
Line 730:
{{works with|Commodore BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM Loops/While
20 LET I = 1024
Line 738:
70 GOTO 40
80 END
</syntaxhighlight>
==={{header|Nascom BASIC}}===
Line 745:
If the repeated sequence is short then the loop can be written in one line.
{{works with|Nascom ROM BASIC|4.7}}
<
10 REM Loops/While
20 LET I=1024
30 IF I>0 THEN PRINT I:I=INT(I/2):GOTO 30
40 END
</syntaxhighlight>
==={{Header|Tiny BASIC}}===
Tiny BASIC have no <code>while</code> construct. Equivalent using conditional jump:
<
LET I = 1024
10 IF I <= 0 THEN GOTO 20
Line 760:
LET I = I / 2
GOTO 10
20 END</
{{out}}
<pre>
Line 777:
==={{header|True BASIC}}===
<
DO WHILE i > 0
Line 783:
LET i = INT(i / 2)
LOOP
END</
=={{header|bc}}==
<
while (i > 0) {
i
i /= 2
}</
=={{header|Befunge}}==
<
^/2,*25.:_@</
=={{header|blz}}==
<
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</
=={{header|BQN}}==
Line 807:
BQNcrate's while idiom is the closest equivalent of a while loop in the language.
<
(⌊∘÷⟜2 •Show) _while_ (>⟜0) 1024</
=={{header|Bracmat}}==
<
=={{header|Brat}}==
Converts to integers so output is a little bit shorter and neater.
<
while { i > 0 } {
p i
i = (i / 2).to_i
}</
=={{header|C}}==
<
while(i > 0) {
printf("%d\n", i);
i /= 2;
}</
In for loop fashion:
<
for(i = 1024;i > 0; i/=2){
printf("%d\n", i);
}</
=={{header|C sharp|C#}}==
<
while(i > 0){
System.Console.WriteLine(i);
i /= 2;
}</
=={{header|C++}}==
<
while(i > 0){
std::cout << i << std::endl;
i /= 2;
}</
Alternatively, it can be done with <code>for</code>:
<
std::cout << i << std::endl;</
Instead of <code>i /= 2</code> one can also use the bit shift operator <code>i >>= 1</code> on integer variables.
Indeed, in C++,
<
statement;
}</
is equivalent to
<
init;
while(cond){
Line 865:
update;
}
}</
=={{header|Caché ObjectScript}}==
<
set x = 1024
while (x > 0) {
Line 875:
}
quit</
{{out}}<pre>SAMPLES>DO ^WHILELOOP
Line 893:
=={{header|Chapel}}==
<
while val > 0 {
writeln(val);
val /= 2;
}</
=={{header|ChucK}}==
<syntaxhighlight lang="text">
1024 => int value;
Line 908:
value / 2 => value;
}
</syntaxhighlight>
=={{header|Clojure}}==
<
(while (> @i 0)
(println @i)
(dosync (ref-set i (quot @i 2))))</
2 ways without mutability:
<
(when (pos? i)
(println i)
Line 926:
(doseq [i (take-while pos? (iterate #(quot % 2) 1024))]
(println i))</
=={{header|CLU}}==
<
po: stream := stream$primary_output()
n: int := 1024
Line 936:
n := n/2
end
end start_up</
=={{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.
<
PROGRAM-ID. Loop-While.
Line 954:
GOBACK
.</
=={{header|ColdFusion}}==
Line 960:
With tags:
<
<cfset i /= 2 />
</cfloop></
With script:
<
while( i > 0 )
{
writeOutput( i + "< br/ >" );
}
</cfscript></
=={{header|Common Lisp}}==
<
(loop while (plusp i) do
(print i)
Line 986:
(print *i*)
(setf *i* (floor *i* 2)))
</syntaxhighlight>
=={{header|Cowgol}}==
<
var n: uint16 := 1024;
Line 996:
print_nl();
n := n/2;
end loop;</
=={{header|Crack}}==
<
while( i > 0 ) {
cout ` $i\n`;
i = i/2;
}</
=={{header|Crystal}}==
<
while i > 0
puts i
i //= 2
end</
<code>until ''condition''</code> is the negated version, equivalent to <code>while !(''condition'')</code>.
<
until i <= 0
puts i
i //= 2
end</
=={{header|D}}==
<
void main() {
Line 1,030:
i >>= 1;
}
}</
{{out}}
<pre>1024
Line 1,045:
=={{header|Dao}}==
<
while( i > 0 ) i = i / 2;</
=={{header|Dc}}==
<
=={{header|DCL}}==
Line 1,056:
so must make do with IF/THEN/ELSE and GOTO statements.
<
$Loop:
$ IF ( i .LE. 0 ) THEN GOTO LoopEnd
Line 1,065:
$ i = i / 2
$ GOTO Loop
$LoopEnd:</
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">var
i : Integer;
begin
Line 1,079:
i := i div 2;
end;
end;</
=={{header|Draco}}==
<
word i;
i := 1024;
Line 1,089:
i := i >> 1
od
corp</
{{out}}
<pre>1024
Line 1,104:
=={{header|Dragon}}==
<
while(i > 0){
showln i
i >>= 1 //also acceptable: i /= 2
}</
=={{header|DUP}}==
<
Explanation:
<
[ ][ ]# {while[condition>0][do]}
$ {DUP}
$. {DUP, print top of stack to STDOUT}
10, {print newline}
2/\% {2 DIV/MOD SWAP POP}</
Alternative, if the interpreter allows using the shift operator:
<
Output:
<
512
256
Line 1,138:
4
2
1</
=={{header|DWScript}}==
<
while i > 0 do begin
PrintLn(i);
i := i div 2;
end;</
=={{header|Dyalect}}==
Line 1,153:
{{trans|Swift}}
<
while i > 0 {
print(i)
i /= 2
}</
=={{header|E}}==
<
while (i > 0) {
println(i)
i //= 2
}</
=={{header|EasyLang}}==
<syntaxhighlight lang="text">i = 1024
while i > 0
print i
i = i div 2
.</
=={{header|EchoLisp}}==
<
(set! n 1024)
(while (> n 0) (write n) (set! n (quotient n 2)))
1024 512 256 128 64 32 16 8 4 2 1
</syntaxhighlight>
=={{header|EGL}}==
<
while ( x > 0 )
SysLib.writeStdout( x );
x = MathLib.floor( x / 2 );
end</
=={{header|Elena}}==
ELENA 4.x:
<
{
int i := 1024;
Line 1,201:
i /= 2
}
}</
=={{header|Elixir}}==
<
def while(0), do: :ok
def while(n) do
Line 1,212:
end
Loops.while(1024)</
=={{header|Emacs Lisp}}==
<
(while (> i 0)
(message "%d" i)
(setq i (/ i 2))))</
=={{header|Erlang}}==
<
-export([loop/0]).
Line 1,232:
loop(N) when N >0 ->
io:format("~w~n", [N]),
loop(N div 2).</
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
I%=1024
WHILE I%>0 DO ! you can leave out >0
Line 1,241:
I%=I% DIV 2 ! I%=INT(I%/2) for C-64 version
END WHILE
</syntaxhighlight>
=={{header|Euphoria}}==
<
i = 1024
Line 1,250:
printf(1, "%g\n", {i})
i = floor(i/2) --Euphoria does NOT use integer division. 1/2 = 0.5
end while</
Even without the <code>floor()</code> the code will in fact end. But it's FAR beyond 1.
=={{header|F_Sharp|F#}}==
<
loop 1024</
=={{header|Factor}}==
<
=={{header|FALSE}}==
<
"2/]#%</
=={{header|Fantom}}==
<
{
public static Void main ()
Line 1,277:
}
}
}</
=={{header|Fennel}}==
<
(while (> i 0)
(print i)
(set i (// n 2)))</
=={{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.
<
while n>2/3 do !!n;n:=n/2; od;</
=={{header|Forth}}==
<
begin dup 0 >
while cr dup . 2/
repeat drop ;
1024 halving</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
DO WHILE (i > 0)
WRITE(*,*) i
i = i / 2
END DO</
{{works with|Fortran|77 and later}}
<
INTEGER I
Line 1,321:
ENDIF
STOP
END</
{{works with|Fortran|IV and 66 and later}}
<
INTEGER I
C FORTRAN 66 does not have IF block.
Line 1,335:
20 CONTINUE
STOP
END</
=={{header|Fortress}}==
<
component loops_while
export Executable
Line 1,348:
end
end
</syntaxhighlight>
{{out}}
Line 1,366:
=={{header|FreeBASIC}}==
<
Dim i As Integer = 1024
Line 1,375:
Wend
Sleep</
{{out}}
Line 1,393:
=={{header|Frink}}==
<
while i>0
{
i = i/1
}</
=={{header|FutureBasic}}==
<
long i = 1024
Line 1,409:
wend
HandleEvents</
Output:
<pre>
Line 1,427:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=4e992013e4e7dc69a82477299a5ce23a Click this link to run this code]'''
<
Dim siCount As Short = 1024
Line 1,435:
Wend
End</
Output:
<pre>
Line 1,442:
=={{header|GAP}}==
<
while n > 0 do
Print(n, "\n");
n := QuoInt(n, 2);
od;</
=={{header|GML}}==
<
while(i > 0)
{
show_message(string(i))
i /= 2
}</
=={{header|Go}}==
<
for i > 0 {
fmt.Printf("%d\n", i)
i /= 2
}</
=={{header|Groovy}}==
Solution:
<
while (i > 0) {
println i
i /= 2
}</
{{Out}}
Line 1,485:
=={{header|Haskell}}==
<
main = loop 1024
where loop n = when (n > 0)
(do print n
loop (n `div` 2))</
You can use whileM_ function from monad-loops package that operates on monads:
<
import Control.Monad.Loops
Line 1,503:
(do n <- readIORef r
print n
modifyIORef r (`div` 2))</
With MonadComprehensions extension you can write it a little bit more readable:
<
import Data.IORef
import Control.Monad.Loops
Line 1,516:
n <- readIORef r
print n
modifyIORef r (`div` 2)</
=={{header|Haxe}}==
Using shift right.
<
while (i > 0) {
Sys.println(i);
i >>= 1;
}</
Using integer division.
<
while (i > 0) {
Sys.println(i);
i = Std.int(i / 2);
}</
{{out}}
Line 1,551:
=={{header|hexiscript}}==
<
while i > 0
println i
let i (i / 2)
endwhile</
=={{header|HolyC}}==
<
while (i > 0) {
Print("%d\n", i);
i /= 2;
}</
=={{header|Icon}} and {{header|Unicon}}==
<
local i
i := 1024
while write(0 < (i := i / 2))
end</
=={{header|Inform 7}}==
<
while N > 0:
say "[N][line break]";
let N be N / 2;</
=={{header|IWBASIC}}==
<syntaxhighlight lang="iwbasic">
DEF X:INT
Line 1,602:
'Since this is, in fact, an IWBASIC console program, which compiles and runs.
END</
Note: Spacing is not an issue. I just find the code to be more readable with spaces.
Line 1,608:
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
<
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).
<
while. 0 < y do.
smoutput y
Line 1,618:
end.
i.0 0
)</
Note: this defines an anonymous function (monad define, and the subsequent lines) and passes it the argument 1024, which means it will be executed as soon as the full definition is available.
=={{header|Java}}==
<
while(i > 0){
System.out.println(i);
i >>= 1; //also acceptable: i /= 2;
}</
With a for loop:
<
System.out.println(i);
}</
=={{header|JavaScript}}==
<
while (n > 0) {
print(n);
n /= 2;
}</
In a functional idiom of JavaScript, however, we can not use a While '''statement''' to achieve this task, as statements return no value, mutate state, and can not be composed within other functional expressions.
Line 1,647:
:#A conditional function, corresponding to the While test
<
'use strict';
var d = fnDelta(varValue);
Line 1,666:
}
).join('\n')
);</
If we assume integer division here (Math.floor(x/2)) rather than the floating point division (x/2) used in the imperative example, we obtain the output:
<syntaxhighlight lang="javascript">512
256
128
Line 1,679:
4
2
1</
=={{header|Joy}}==
<syntaxhighlight lang=
1024 [] [dup putln 2 /] while.</syntaxhighlight>
=={{header|jq}}==
'''Using recurse/1'''<
1024 | recurse( if . > 1 then ./2 | floor else empty end)</
'''Using recurse/2''' (requires jq >1.4)
<
'''Using a filter'''
<
1024|task</
'''Using while/2'''
If your jq does not include while/2 as a builtin, here is its definition:
<
def _while: if cond then ., (update | _while) else empty end;
_while;</
For example:
<
=={{header|Jsish}}==
<
/* Loops/While in Jsish */
var i = 1024;
Line 1,723:
1
=!EXPECTEND!=
*/</
{{out}}
Line 1,730:
=={{header|Julia}}==
<syntaxhighlight lang="julia">
n = 1024
Line 1,737:
n >>= 1
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,756:
Implementation of the task using anonymous function is
given below
<syntaxhighlight lang="k">
{while[x>0; \echo x; x%:2]} 1024
</syntaxhighlight>
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,769:
value /= 2
}
}</
{{out}}
Line 1,791:
=={{header|Lambdatalk}}==
<
{def loops_while
{lambda {:i}
Line 1,801:
{loops_while 1024}
-> 1024 512 256 128 64 32 16 8 4 2 1 (end of loop)
</syntaxhighlight>
=={{header|Lang5}}==
{{trans|Factor}}
<
: dip swap '_ set execute _ ; : dupd 'dup dip ;
: 2dip swap '_x set swap '_y set execute _y _x ;
Line 1,813:
loop ;
1024 "dup 0 >" "dup . 2 /i" while</
=={{header|Lambdatalk}}==
<
{def while
{lambda {:i}
Line 1,836:
2
1
</syntaxhighlight>
=={{header|langur}}==
Line 1,842:
{{works with|langur|0.8}}
<
while .i > 0 {
writeln .i
.i \= 2
}</
{{works with|langur|< 0.8}}
<
for .i > 0 {
writeln .i
.i \= 2
}</
=={{header|Lasso}}==
<
while(#i > 0) => {^
#i + '\r'
#i /= 2
^}</
=={{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.
<
while i > 0
print i
i = int( i / 2)
wend
end</
=={{header|LIL}}==
<
Backslash is integer division, otherwise LIL would allow the division to go floating point.
=={{header|Lingo}}==
<
repeat while n>0
put n
n = n/2 -- integer division implicitely returns floor: 1/2 -> 0
end repeat</
=={{header|Lisaac}}==
<
i := 1024;
{ i > 0 }.while_do {
Line 1,891:
i := i / 2;
};</
=={{header|LiveCode}}==
<
repeat while n > 0
put n & cr
divide n by 2
end repeat</
=={{header|Logo}}==
<
while [:n > 0] [print :n make "n :n / 2]</
=={{header|LOLCODE}}==
Line 1,908:
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.
<
I HAS A n ITZ 1024
Line 1,917:
IM OUTTA YR loop
KTHXBYE</
=={{header|Lua}}==
<
while n>0 do
print(n)
n = math.floor(n/2)
end</
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Def long A=1024
Line 1,936:
}
Checkit
</syntaxhighlight>
One line
<syntaxhighlight lang="m2000 interpreter">
Module Online { A=1024&: While A>0 {Print A: A/=2}} : OnLine
</syntaxhighlight>
=={{header|m4}}==
This ‘loop’ is really a tail recursion, which m4 implementations generally do ''not'' optimize.
<
define(`loop',
Line 1,951:
divert`'dnl
loop(1024)dnl</
{{out}}
$ m4 loops_while.m4
Line 1,967:
=={{header|Make}}==
<
MAX=10
Line 1,978:
%-echo:
@echo $*</
Invoking it
<
=={{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 (/).
<
1024
512
Line 1,996:
4
2
1</
=={{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:
<
While[i > 0,
Print[i];
i = Floor[i/2];
]</
=={{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.
<
while (i > 0)
disp(i);
i = floor(i/2);
end</
A vectorized version of the code is
<
=={{header|Maxima}}==
<
/* 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);</
=={{header|MAXScript}}==
<
while a > 0 do
(
print a
a /= 2
)</
=={{header|Metafont}}==
Line 2,036:
Metafont has no <tt>while</tt> loop, but it can be "simulated" easily.
<
forever: exitif not (a > 0);
show a;
a := a div 2;
endfor</
=={{header|Microsoft Small Basic}}==
<
i = 1024
While i > 0
Line 2,049:
i = Math.Floor(i / 2)
EndWhile
</syntaxhighlight>
=={{header|min}}==
{{works with|min|0.19.3}}
<
=={{header|MiniScript}}==
<
while i > 0
print i
i = floor(i/2)
end while</
{{out}}
Line 2,079:
=={{header|MIRC Scripting Language}}==
<
var %n = 10
while (%n >= 0) {
Line 2,085:
dec %n
}
}</
=={{header|MIXAL}}==
<syntaxhighlight lang="mixal">
******************************************
* X = M / N WHILE X > 0
Line 2,118:
HLT
END START
</syntaxhighlight>
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 0 2 4 П0 ИП0 /-/ x<0 15 ИП0
2 / П0 БП 05 С/П</
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 2,137:
i := i DIV 2
END
END DivBy2.</
=={{header|Modula-3}}==
The usual module code and imports are omitted.
<
VAR i: INTEGER := 1024;
BEGIN
Line 2,149:
i := i DIV 2;
END;
END DivBy2;</
=={{header|Monte}}==
<syntaxhighlight lang="monte">
var i := 1024
while (i > 0):
traceln(i)
i //= 2
</syntaxhighlight>
=={{header|MOO}}==
<
while (i > 0)
player:tell(i);
i /= 2;
endwhile</
=={{header|Morfa}}==
<
import morfa.io.print;
Line 2,177:
i /= 2;
}
</syntaxhighlight>
=={{header|Nanoquery}}==
<
while ($n > 0)
println $n
$n = $n/2
end while</
=={{header|Neko}}==
<syntaxhighlight lang="neko">
var i = 1024
Line 2,194:
i = $idiv(i, 2)
}
</syntaxhighlight>
=={{header|Nemerle}}==
<
while (x > 0)
{
WriteLine($"$x");
x /= 2;
}</
Or, with immutable types, after Haskell:
<
def loop(n : int) : void
{
Line 2,214:
}
loop(1024)</
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 2,227:
say x_.right(6)
x_ = x_ % 2 -- integer division
end</
=={{header|NewLISP}}==
<
(while (> i 0)
(println i)
(setq i (/ i 2))))</
=={{header|Nim}}==
<
while n > 0:
echo(n)
n = n div 2</
=={{header|NS-HUBASIC}}==
<
20 IF I=0 THEN END
30 PRINT I
40 I=I/2
50 GOTO 20</
=={{header|Oberon-2}}==
The usual module code and imports are ommited.
<
VAR i: INTEGER;
BEGIN
Line 2,259:
i := i DIV 2;
END;
END DivBy2;</
=={{header|Objeck}}==
<
while(i > 0) {
i->PrintLine();
i /= 2;
};</
=={{header|ObjectIcon}}==
Line 2,272:
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''.
<
procedure main()
Line 2,278:
n := 1024
while n := write(0 < n) / 2
end</
{{out}}
Line 2,295:
The following also works, and shows a way to avoid having to code the method of writing the output into the loop itself.
<
procedure main()
Line 2,305:
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
end</
To show what I mean, let’s do some (merely illustrative) loop-unrolling where the output gets written.
<
procedure main()
Line 2,325:
procedure generator(n)
while 0 < n do { suspend n; n /:= 2 }
end</
=={{header|OCaml}}==
<
while !n > 0 do
Printf.printf "%d\n" !n;
n := !n / 2
done;;</
But it is more common to write it in a tail-recursive functional style:
<
if n > 0 then begin
Printf.printf "%d\n" n;
loop (n / 2)
end
in loop 1024</
=={{header|Octave}}==
<
while (i > 0)
disp(i)
i = floor(i/2);
endwhile</
The usage of the type int32 is not convenient, since the math is done floating point, then rounding to integer, so that 1/2 will be always 1 and never 0.
Line 2,353:
=={{header|Oforth}}==
<
=={{header|OOC}}==
<
main: func {
value := 1024
Line 2,364:
}
}
</syntaxhighlight>
=={{header|Oz}}==
Oz' for-loop can be used in a C-like manner:
<
{Show I}
end</
Alternatively, we can use the <code>while</code> feature of the for-loop with a mutable variable:
<
I = {NewCell 1024}
in
Line 2,379:
{Show @I}
I := @I div 2
end</
=={{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.
<
1024.trans(func:half).gt(0) nl
</syntaxhighlight>
=={{header|Panoramic}}==
<
x%=1024
Line 2,401:
rem output starts with 1024 and ends with 1.
terminate</
=={{header|PARI/GP}}==
<
while(n,
print(n);
n/=2
);</
=={{header|Pascal}}==
<
var
Line 2,423:
i := i div 2
end
end.</
=={{header|PeopleCode}}==
<syntaxhighlight lang="peoplecode">
Local string &CRLF;
Local number &LoopNumber;
Line 2,436:
&LoopNumber = &LoopNumber / 2;
End-While;
</syntaxhighlight>
=={{header|Perl}}==
<
while($n){
print "$n\n";
$n = int $n / 2;
}</
or written as a for-loop and using the bit-shift operator
<
print "$n\n";
}</
<code>until (''condition'')</code> is equivalent to <code>while (not ''condition'')</code>.
<
until($n == 0){
print "$n\n";
$n = int $n / 2;
}</
=={{header|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,466:
<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>
<!--</
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|PHL}}==
<
while (i > 0) {
printf("%i\n", i);
i = i/2;
}</
=={{header|PHP}}==
<
while ($i > 0) {
echo "$i\n";
$i >>= 1;
}</
=={{header|Picat}}==
While loop
<
N = 1024,
while (N > 0)
println(N),
N := N // 2
end.</
Recursion
{{trans|Prolog}}
<
while_loop(1024).
Line 2,501:
while_loop(N) =>
println(N),
while_loop(N//2).</
=={{header|PicoLisp}}==
<
(while (gt0 N)
(println N)
(setq N (/ N 2)) ) )</
=={{header|Pike}}==
<
int i = 1024;
while(i > 0){
Line 2,517:
i = i / 2;
}
}</
=={{header|PL/I}}==
<
do while (i>0);
put skip list (i);
i = i / 2;
end;</
=={{header|PL/SQL}}==
{{works with|Oracle}}
<
set serveroutput on
declare
Line 2,540:
end;
/
</syntaxhighlight>
=={{header|Plain English}}==
<
Start up.
Show the halvings of 1024.
Line 2,554:
Write the string to the console.
Divide the number by 2.
Repeat.</
=={{header|Pop11}}==
<
while i > 0 do
printf(i, '%p\n');
i div 2 -> i;
endwhile;</
=={{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:
<
{
dup 0 le % check whether still greater than 0
Line 2,574:
2 idiv % divide by two
}
loop</
=={{header|PowerShell}}==
<
while ($i -gt 0) {
$i
$i /= 2
}</
=={{header|Prolog}}==
<
while(X) :-
writeln(X),
X1 is X // 2,
while(X1).</
Start the calculation at a top-level like this:
<syntaxhighlight lang
=={{header|PureBasic}}==
<
x.i = 1024
Line 2,606:
Input()
CloseConsole()
EndIf</
=={{header|Python}}==
<
while n > 0:
print n
n //= 2</
=={{header|QB64}}==
<
n = 1024
While n > 0
Print n
n = n \ 2
Wend</
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">1024
[ dup 0 > while
dup echo cr 2 /
again ]
drop</
{{Out}}
Line 2,647:
=={{header|R}}==
<
while(i > 0)
{
print(i)
i <- i %/% 2
}</
=={{header|Racket}}==
===Loop/When===
<
(let loop ([n 1024])
(when (positive? n)
(displayln n)
(loop (quotient n 2))))</
===Macro===
<
(define-syntax-rule (while condition body ...)
(let loop ()
Line 2,673:
(while (positive? n)
(displayln n)
(set! n (sub1 n)))</
=={{header|Raku}}==
Line 2,679:
Here is a straightforward translation of the task description:
<syntaxhighlight lang="raku"
The same thing with a C-style loop and a bitwise shift operator:
<syntaxhighlight lang="raku"
And here's how you'd <em>really</em> write it, using a sequence operator that intuits the division for you:
<syntaxhighlight lang="raku"
=={{header|REBOL}}==
<
Title: "Loop/While"
URL: http://rosettacode.org/wiki/Loop/While
Line 2,698:
print value
value: to-integer value / 2
]</
=={{header|ReScript}}==
<
while n.contents > 0 {
Js.log(n.contents)
n := n.contents / 2
}</
=={{header|Retro}}==
<
=={{header|REXX}}==
===version 1, simple===
<
j=1024 /*define the initial value of J.*/
do while j>0 /*test if made at the top of DO.*/
Line 2,719:
j=j%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</
{{out}}
<pre>
Line 2,740:
::::: '''DO WHILE x\==0'''
but that wouldn't be compliant with the wording of the task.
<
x=1024 /*define the initial value of X.*/
do while x>0 /*test if made at the top of DO.*/
Line 2,746:
x=x%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</
{{out}}
<pre>
Line 2,763:
===version 3, faster WHILE comparison===
<
x=1024 /*define the initial value of X.*/
do while x>>0 /*this is an exact comparison. */
Line 2,769:
x=x%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</
'''output''' is the same as version 2.
<br>
===version 4, index reduction===
<
/* [↓] note: BY defaults to 1*/
do j=1024 by 0 while j>>0 /*this is an exact comparison. */
Line 2,780:
j=j%2 /*in REXX, % is integer division.*/
end
/*stick a fork in it, we're done.*/</
'''output''' is the same as version 2.
<br><br>
=={{header|Ring}}==
<
i = 1024
while i > 0
Line 2,791:
i = floor(i / 2)
end
</syntaxhighlight>
=={{header|Ruby}}==
<
while i > 0 do
puts i
i /= 2
end</
The above can be written in one statement:
<
puts i /= 2 while i > 0</
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>.
<
until i <= 0 do
puts i
i /= 2
end</
=={{header|Run BASIC}}==
<
while i > 0
print i
i = int(i / 2)
wend
end</
=={{header|Rust}}==
<
let mut n: i32 = 1024;
while n > 0 {
Line 2,826:
n /= 2;
}
}</
=={{header|SAS}}==
<
n=1024;
do while(n>0);
Line 2,835:
n=int(n/2);
end;
run;</
=={{header|Sather}}==
<
main is
i ::= 1024;
Line 2,846:
end;
end;
end;</
=={{header|Scala}}==
{{libheader|Scala}}
===Imperative===
<
while (i > 0) {
println(i)
i /= 2
}</
===Tail recursive===
<
def loop(iter: Int) {
if (iter > 0) {
Line 2,865:
}
}
loop(1024)</
===Iterator===
<
var i = 1024
def hasNext = i > 0
def next(): Int = { val tmp = i; i = i / 2; tmp }
}
loop.foreach(println(_))</
===Stream===
Finite stream (1024..0) filtered by takeWhile (1024..1).
<
loop(1024).takeWhile(_ > 0).foreach(println(_))</
=={{header|Scheme}}==
<
((<= n 0))
(display n)
(newline))</
=={{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</
{{out}}
<pre>1024
Line 2,907:
=={{header|Seed7}}==
<
const proc: main is func
Line 2,917:
i := i div 2
end while;
end func;</
=={{header|SenseTalk}}==
<
log x
Repeat until x = 1
divide x by 2
log x
End repeat</
=={{header|SETL}}==
<
while n > 0 loop
print( n );
n := n div 2;
end loop;</
=={{header|Sidef}}==
<
while (i > 0) {
say i
i //= 2
}</
=={{header|Simula}}==
{{works with|SIMULA-67}}
<
integer i;
i:=1024;
Line 2,951:
i:=i//2-1
end
end</
{{out}}
<pre>
Line 2,961:
Since we have no integer type, we floor the result of the division each time.
<
20 IF I=0 THEN GOTO 60
30 PRINT I
40 LET I=INT (I/2)
50 GOTO 20</
=={{header|Slate}}==
<
[n isPositive] whileTrue:
[inform: number printString.
n := n // 2]</
=={{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).
<
[foo notNil] whileTrue: [s next. ...].
[...] doWhile: [ ... someBooleanExpression ].
[...] doUntil: [ ... someBooleanExpression ].
[:exit | ... cold ifTrue:[exit value]. ...] loopWithExit</
Examples:
<
[ number > 0 ] whileTrue:
[ Transcript print: number; nl.
number := number // 2 ]</
<
[ number <= 0 ] whileFalse:
[ Transcript print: number; nl.
number := number // 2 ]</
=={{header|Sparkling}}==
<
while i > 0 {
print(i);
i /= 2;
}</
=={{header|Spin}}==
Line 3,005:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 3,023:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>
Line 3,030:
=={{header|SPL}}==
<
>
#.output(n)
n /= 2
< n!<1</
{{out}}
<pre>
Line 3,053:
{{works with|Db2 LUW}} version 9.7 or higher.
With SQL PL:
<
--#SET TERMINATOR @
Line 3,066:
END WHILE Loop;
END @
</syntaxhighlight>
Output:
<pre>
Line 3,091:
=={{header|Standard ML}}==
<
while !n > 0 do (
print (Int.toString (!n) ^ "\n");
n := !n div 2
)</
But it is more common to write it in a tail-recursive functional style:
<
fun loop n =
if n > 0 then (
Line 3,106:
in
loop 1024
end</
=={{header|Stata}}==
<
while `n'>0 {
display `n'
local n=floor(`n'/2)
}</
=={{header|Suneido}}==
<
while (i > 0)
{
Print(i)
i = (i / 2).Floor()
}</
{{Out}}
<pre>1024
Line 3,136:
=={{header|Swift}}==
<
while i > 0 {
println(i)
i /= 2
}</
=={{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.
<
1024 -> \(
<0~..> '$;$#10;' -> !OUT::write
$ ~/ 2 -> #
\) -> !VOID
</syntaxhighlight>
=={{header|Tcl}}==
<
while {$i > 0} {
puts $i
set i [expr {$i / 2}]
}</
=={{header|TeX}}==
<
\rosetta=1024
\loop
Line 3,167:
\ifnum\rosetta > 0
\repeat
\end</
=={{header|TI-83 BASIC}}==
<
While I>0
Disp I
I/2→I
End
</syntaxhighlight>
=={{header|TI-89 BASIC}}==
<
1024 → i
While i > 0
Disp i
intDiv(i, 2) → i
EndWhile</
=={{header|TorqueScript}}==
This has to make use of mFloor because torque has automatic type shuffling,
causing an infiniteloop.
<
while(%num > 0)
{
echo(%num);
%num = mFloor(%num / 2);
}</
=={{header|Transact-SQL}}==
<syntaxhighlight lang="transact-sql">
DECLARE @i INT = 1024;
WHILE @i >0
Line 3,205:
SET @i = @i / 2;
END;
</syntaxhighlight>
=={{header|Trith}}==
<
<
=={{header|TUSCRIPT}}==
<
i=1024
LOOP
Line 3,218:
i=i/2
IF (i==0) EXIT
ENDLOOP</
{{Out}}
<pre>
Line 3,239:
=={{header|Uniface}}==
<
numeric I
endvariables
Line 3,247:
putmess I
I = (I/2)[trunc]
endwhile</
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
<
while [[ $x -gt 0 ]]; do
echo $x
x=$(( $x/2 ))
done</
=={{header|UnixPipes}}==
<
test $a -gt 0 && (expr $a / 2 >> p.res ; echo $a) || exit 0
done</
=={{header|Ursa}}==
<
set n 1024
Line 3,269:
out n endl console
set n (int (/ n 2))
end while</
=={{header|Ursala}}==
Line 3,287:
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.
<
g = ~&h-> ^C/half@h ~&
Line 3,293:
#show+
main = %nP*=tx g <1024></
{{Out}}
<pre>
Line 3,311:
The same output is produced by the following main program
using bit manipulation.
<
=={{header|V}}==
<
dup puts
2 / >int
] while</
=={{header|Vala}}==
<
while (i > 0) {
stdout.printf("%d\n", i);
i /= 2;
}</
=={{header|VBA}}==
<
Dim value As Integer
value = 1024
Line 3,334:
value = value / 2
Loop
End Sub</
=={{header|Vedit macro language}}==
<
while (#1 > 0) {
Num_Type(#1)
#1 /= 2
}</
or with for loop:
<
Num_Type(#1)
}</
=={{header|Verbexx}}==
<
@LOOP init:{@VAR n = 1024} while:(n > 0) next:{n /= 2}
{
@SAY n;
};</
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
module main;
integer i;
Line 3,371:
end
endmodule
</syntaxhighlight>
=={{header|Vim Script}}==
<
while i > 0
echo i
let i = i / 2
endwhile</
=={{header|Visual Basic .NET}}==
<
Do
Console.WriteLine(x)
x = x \ 2
Loop While x > 0</
=={{header|Vlang}}==
<
mut i := 1024
for i > 0 {
Line 3,395:
i /= 2
}
}</
{{out}}
Line 3,413:
=={{header|Wart}}==
<
while (i > 0)
prn i
i <- (int i/2)</
=={{header|Wee Basic}}==
<
while number>0.5
print 1 number
let number=number/2
wend
end</
=={{header|Whitespace}}==
<
Line 3,443:
</syntaxhighlight>
Pseudo-assembly equivalent:
<
0:
Line 3,452:
push 0 swap sub
jn 0
pop exit</
=={{header|Wren}}==
<
while (i > 0) {
System.print(i)
i = (i / 2).floor
}</
{{out}}
Line 3,477:
=={{header|X86 Assembly}}==
<
; NASM 64 bit X86-64 assembly on Linux
Line 3,518:
leave ; fix stack
ret ; return
</syntaxhighlight>
=={{header|XBasic}}==
{{works with|Windows XBasic}}
<
i% = 1024
DO WHILE i% > 0
Line 3,528:
i% = i% / 2
LOOP
</syntaxhighlight>
=={{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.
<
(WHILE (>= I 1)
(PRINT I)
(DEFINE I (/ I 2)))</
=={{header|XPL0}}==
<
int I;
[I:= 1024;
Line 3,546:
I:= I>>1; \(same as I/2 for positive I)
];
]</
=={{header|Yabasic}}==
<
while i > 0
Line 3,557:
wend
end</
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
Line 3,569:
std.debug.print("{}\n", .{i});
}
</syntaxhighlight>
=={{header|zkl}}==
<
{{out}}
<pre>
|