Loops/Downward for: Difference between revisions
(+D) |
m (→{{header|D}}: var in _for_ loop need a declaration) |
||
Line 14: | Line 14: | ||
</cpp> |
</cpp> |
||
=={{header|D}}== |
=={{header|D}}== |
||
<d>for(i = 10; i >= 0; --i) writefln(i)</d> |
<d>for(int i = 10; i >= 0; --i) writefln(i)</d> |
||
[http://www.digitalmars.com/d/2.0/statement.html#ForeachRangeStatement Foreach Range Statement] since D2.003 |
[http://www.digitalmars.com/d/2.0/statement.html#ForeachRangeStatement Foreach Range Statement] since D2.003 |
||
<d>foreach_reverse(i ; 0..10+1) writefln(i) ;</d> |
<d>foreach_reverse(i ; 0..10+1) writefln(i) ;</d> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Unlike the incrementing 10 0 DO-LOOP, this will print eleven numbers. The LOOP words detect crossing the floor of the end limit. |
Unlike the incrementing 10 0 DO-LOOP, this will print eleven numbers. The LOOP words detect crossing the floor of the end limit. |
Revision as of 09:59, 6 June 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Write a for loop which writes a countdown from 10 to 0.
Ada
<ada> for I in reverse 0..10 loop
Put_Line(Integer'Image(I));
end loop; </ada>
C++
<cpp> for(int i = 10; i >= 0; --i)
std::cout << i << "\n";
</cpp>
D
<d>for(int i = 10; i >= 0; --i) writefln(i)</d> Foreach Range Statement since D2.003 <d>foreach_reverse(i ; 0..10+1) writefln(i) ;</d>
Forth
Unlike the incrementing 10 0 DO-LOOP, this will print eleven numbers. The LOOP words detect crossing the floor of the end limit.
: loop-down 0 10 do i . -1 +loop ;
Fortran
DO i = 10, 0, -1 WRITE(*, *) i END DO
Java
<java>for(i = 10; i >= 0; --i){
System.out.println(i);
}</java>
JavaScript
<javascript>for (var i=10; i>=0; --i) print(i);</javascript>
Logo
If the limit is less than the start, then FOR decrements the control variable. Otherwise, a fourth parameter could be given as a custom increment.
for [i 10 0] [print :i]
MAXScript
for i in 10 to 0 by -1 do print i
OCaml
<ocaml>for i = 10 downto 0 do
Printf.printf "%d\n" i
done</ocaml>
Pascal
<pascal> for i := 10 downto 0 do
writeln(i);
</pascal>
Perl
<perl>foreach (reverse 0..10) {
print "$_\n";
}</perl>
Pop11
lvars i; for i from 10 by -1 to 0 do printf(i, '%p\n'); endfor;
Python
<python> for i in xrange(10, -1, -1):
print i
</python>
UnixPipes
yes \ |cat -n |head -n 10 | tac