Loops/While: Difference between revisions
Content added Content deleted
(→{{header|Ruby}}: added until) |
m (Fixed lang tags.) |
||
Line 2: | Line 2: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
<lang actionscript> |
<lang actionscript>var i:int = 1024; |
||
var i:int = 1024; |
|||
while (i > 0) { |
while (i > 0) { |
||
trace(i); |
trace(i); |
||
i /= 2; |
i /= 2; |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada> |
<lang ada>declare |
||
declare |
|||
I : Integer := 1024; |
I : Integer := 1024; |
||
begin |
begin |
||
Line 19: | Line 16: | ||
I := I / 2; |
I := I / 2; |
||
end loop; |
end loop; |
||
end; |
end;</lang> |
||
</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 26: | Line 22: | ||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
<lang algol68>INT i := 1024; |
|||
<pre> |
|||
INT i := 1024; |
|||
WHILE i > 0 DO |
WHILE i > 0 DO |
||
print((i)); |
print((i)); |
||
i := i OVER 2 |
i := i OVER 2 |
||
OD |
OD</lang> |
||
</pre> |
|||
Output: |
Output: |
||
<lang algol68>+1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1</lang> |
|||
<pre> |
|||
+1024 +512 +256 +128 +64 +32 +16 +8 +4 +2 +1 |
|||
</pre> |
|||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
Line 48: | Line 40: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey> |
<lang AutoHotkey>i = 1024 |
||
i = 1024 |
|||
While (i > 0) |
While (i > 0) |
||
{ |
{ |
||
Line 55: | Line 46: | ||
i := Floor(i / 2) |
i := Floor(i / 2) |
||
} |
} |
||
MsgBox % output |
MsgBox % output</lang> |
||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 76: | Line 66: | ||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
<lang befunge>84*:*> :v |
|||
^/2.:_@</lang> |
|||
Line 99: | Line 89: | ||
}</lang> |
}</lang> |
||
Alternatively, it can be done with <code>for</code>: |
Alternatively, it can be done with <code>for</code>: |
||
<lang cpp> |
<lang cpp>for (int i = 1024; i>0; i /= 2) |
||
std::cout << i << std::endl;</lang> |
|||
for (int i = 1024; i>0; i /= 2) |
|||
std::cout << i << std::endl; |
|||
</lang> |
|||
Indeed, in C++, |
Indeed, in C++, |
||
<lang cpp> |
<lang cpp>for (init; cond; update) |
||
statement;</lang> |
|||
for (init; cond; update) |
|||
statement; |
|||
</lang> |
|||
is equivalent to |
is equivalent to |
||
<lang cpp> |
<lang cpp>{ |
||
{ |
|||
init; |
init; |
||
while (cond) |
while (cond) |
||
Line 117: | Line 102: | ||
update; |
update; |
||
} |
} |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
<lang |
<lang csharp>int i = 1024; |
||
while(i > 0){ |
while(i > 0){ |
||
System.Console.WriteLine(i); |
System.Console.WriteLine(i); |
||
Line 131: | Line 115: | ||
With tags: |
With tags: |
||
<lang cfm><cfset i = 1024 /> |
|||
<cfloop condition="i GT 0"> |
|||
#i#< br /> |
|||
<cfset i /= 2 /> |
|||
</cfloop></lang> |
|||
With script: |
With script: |
||
<lang cfm><cfscript> |
|||
i = 1024; |
|||
while( i > 0 ) |
|||
{ |
|||
writeOutput( i + "< br/ >" ); |
|||
} |
|||
</cfscript></lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 171: | Line 155: | ||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
<lang false>1024[$0>][$." |
|||
"2/]#%</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth> |
<lang forth>: halving ( n -- ) |
||
begin dup 0 > |
|||
: halving ( n -- ) |
|||
while cr dup . 2/ |
|||
repeat drop ; |
|||
while cr dup . 2/ |
|||
1024 halving</lang> |
|||
repeat drop ; |
|||
1024 halving |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor>1024 [ dup 0 > ] [ dup . 2 /i ] [ drop ] while</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
<lang fortran> |
<lang fortran>INTEGER :: i = 1024 |
||
DO WHILE (i > 0) |
|||
WRITE(*,*) i |
|||
DO WHILE (i > 0) |
|||
i = i / 2 |
|||
END DO</lang> |
|||
i = i / 2 |
|||
END DO |
|||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 224: | Line 204: | ||
=={{header|Icon}}== |
=={{header|Icon}}== |
||
<lang icon> |
<lang icon>procedure main() |
||
local i |
|||
procedure main() |
|||
i := 1024 |
|||
i := |
while write(0 < (i := i / 2)) |
||
end</lang> |
|||
while write(0 < (i := i / 2)) |
|||
end |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
||
<lang j> |
<lang j>,. <.@-:^:*^:a: 1024</lang> |
||
,. <.@-:^:*^:a: 1024 |
|||
</lang> |
|||
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided). |
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided). |
||
<lang j> |
<lang j>3 : 0 ] 1024 |
||
3 : 0 ] 1024 |
|||
while. 0 < y do. |
|||
y 1!:2 ] 2 |
|||
y =. <. -: y |
|||
end. |
|||
i. 0 0 |
|||
)</lang> |
|||
) |
|||
</lang> |
|||
Though it's rare to see J code like this. |
Though it's rare to see J code like this. |
||
Line 274: | Line 248: | ||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
<lang joy> |
<lang joy>DEFINE putln == put '\n putch. |
||
DEFINE putln == put '\n putch. |
|||
1024 [0 >] [dup putln 2 /] while. |
1024 [0 >] [dup putln 2 /] while.</lang> |
||
</lang> |
|||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
<lang Lisaac> |
<lang Lisaac>+ i : INTEGER; |
||
+ i : INTEGER; |
|||
i := 1024; |
i := 1024; |
||
{ i > 0 }.while_do { |
{ i > 0 }.while_do { |
||
Line 288: | Line 259: | ||
'\n'.print; |
'\n'.print; |
||
i := i / 2; |
i := i / 2; |
||
}; |
};</lang> |
||
</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo>make "n 1024 |
|||
while [:n > 0] [print :n make "n :n / 2]</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function: |
Mathematica does not support integer-rounding, it would result in getting fractions: 1/2, 1/4 , 1/8 and so on; the loop would take infinite time without using the Floor function: |
||
<lang Mathematica> |
<lang Mathematica>i = 1024; |
||
While[i > 0, |
|||
i = 1024; |
|||
Print[i]; |
|||
i = Floor[i/2]; |
|||
]</lang> |
|||
i = Floor[i/2]; |
|||
] |
|||
</lang> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<lang maxscript>a = 1024 |
|||
<pre> |
|||
a = 1024 |
|||
while a > 0 do |
while a > 0 do |
||
( |
( |
||
print a |
print a |
||
a /= 2 |
a /= 2 |
||
)</lang> |
|||
) |
|||
</pre> |
|||
=={{header|Make}}== |
=={{header|Make}}== |
||
<lang make>NEXT=`expr $* / 2` |
|||
MAX=10 |
|||
all: $(MAX)-n; |
|||
0-n:; |
|||
%-n: %-echo |
|||
@-make -f while.mk $(NEXT)-n MAX=$(MAX) |
|||
%-echo: |
|||
@echo $*</lang> |
|||
Invoking it |
Invoking it |
||
<lang make>|make -f while.mk MAX=1024</lang> |
|||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 355: | Line 321: | ||
=={{header|MOO}}== |
=={{header|MOO}}== |
||
<lang moo> |
<lang moo>i = 1024; |
||
i = 1024; |
|||
while (i > 0) |
while (i > 0) |
||
player:tell(i); |
player:tell(i); |
||
Line 400: | Line 365: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<lang pascal> |
<lang pascal>program divby2(output); |
||
program divby2(output); |
|||
var |
var |
||
Line 413: | Line 377: | ||
i := i div 2 |
i := i div 2 |
||
end |
end |
||
end. |
end.</lang> |
||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Line 437: | Line 400: | ||
while $n = $n div 2 { |
while $n = $n div 2 { |
||
say $n; |
say $n; |
||
}</lang> |
|||
} |
|||
</lang> |
|||
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>. |
<code>until ''condition''</code> is equivalent to <code>while not ''condition''</code>. |
||
Line 455: | Line 417: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
<lang pop11>lvars i = 1024; |
|||
<pre> |
|||
lvars i = 1024; |
|||
while i > 0 do |
while i > 0 do |
||
printf(i, '%p\n'); |
printf(i, '%p\n'); |
||
i div 2 -> i; |
i div 2 -> i; |
||
endwhile; |
endwhile;</lang> |
||
</pre> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Line 487: | Line 447: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<lang R> |
<lang R>i <- 1024L |
||
i <- 1024L |
|||
while(i > 0) |
while(i > 0) |
||
{ |
{ |
||
print(i) |
print(i) |
||
i <- i %/% 2 |
i <- i %/% 2 |
||
}</lang> |
|||
} |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Line 515: | Line 473: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang scheme> |
<lang scheme>(do ((n 1024 (quotient n 2))) |
||
(do ((n 1024 (quotient n 2))) |
|||
((<= n 0)) |
((<= n 0)) |
||
(display n) |
(display n) |
||
Line 522: | Line 479: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
<lang slate> |
<lang slate>[| n | n: 1024. |
||
[| n | n: 1024. |
|||
[n isPositive] whileTrue: |
[n isPositive] whileTrue: |
||
[inform: number printString. |
[inform: number printString. |
||
n: n // 2]] do |
n: n // 2]] do</lang> |
||
</lang> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 562: | Line 517: | ||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
< |
<lang ti89b>Local i |
||
1024 → i |
1024 → i |
||
While i > 0 |
While i > 0 |
||
Disp i |
Disp i |
||
intDiv(i, 2) → i |
intDiv(i, 2) → i |
||
EndWhile</ |
EndWhile</lang> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 578: | Line 533: | ||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
<lang |
<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</lang> |
|||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 598: | Line 553: | ||
whole argument. The main program takes care of list reversal and |
whole argument. The main program takes care of list reversal and |
||
formatting. |
formatting. |
||
<lang Ursala> |
<lang Ursala>#import nat |
||
#import nat |
|||
g = ~&h-> ^C/half@h ~& |
g = ~&h-> ^C/half@h ~& |
||
Line 605: | Line 559: | ||
#show+ |
#show+ |
||
main = %nP*=tx g <1024> |
main = %nP*=tx g <1024></lang> |
||
</lang> |
|||
output: |
output: |
||
<pre> |
<pre> |
||
Line 624: | Line 577: | ||
The same output is produced by the following main program |
The same output is produced by the following main program |
||
using bit manipulation. |
using bit manipulation. |
||
<lang Ursala> |
<lang Ursala>main = %nP*=tK33 1024</lang> |
||
main = %nP*=tK33 1024 |
|||
</lang> |
|||
=={{header|V}}== |
=={{header|V}}== |
||
<lang v> |
<lang v>1024 [0 >] [ |
||
dup puts |
|||
2 / >int |
|||
] while</lang> |
|||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
Line 639: | Line 590: | ||
Num_Type(#1) |
Num_Type(#1) |
||
#1 /= 2 |
#1 /= 2 |
||
}</lang> |
|||
} |
|||
</lang> |
|||
or with for loop: |
or with for loop: |
||
<lang vedit>for (#1 = 1024; #1 > 0; #1 /= 2) { |
<lang vedit>for (#1 = 1024; #1 > 0; #1 /= 2) { |