Loop/Downward For
From Rosetta Code
Programming Task
This is a programming task. It lays out a problem which Rosetta Code users are encouraged to solve, using languages they know.
Write a for loop which writes a countdown from 10 to 0.
Contents |
[edit] Ada
for I in reverse 0..10 loop Put_Line(Integer'Image(I)); end loop;
[edit] C++
for(int i = 10; i >= 0; --i) std::cout << i << "\n";
[edit] ColdFusion
With tags:
<cfloop index = "i" from = "10" to = "0" step = "-1"> #i# </cfloop>
With script:
<cfscript>
for( i = 10; i <= 0; i-- )
{
writeOutput( i );
}
</cfscript>
[edit] D
for(int i = 10; i >= 0; --i) writefln(i)
Foreach Range Statement since D2.003
foreach_reverse(i ; 0..10+1) writefln(i) ;
[edit] 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 ;
[edit] Fortran
Works with: Fortran version 90 and later
DO i = 10, 0, -1 WRITE(*, *) i END DO
[edit] Haskell
forM_ (reverse [0..10]) print
[edit] J
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
,. i. -11
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).
3 : 0 ] 11
for_i. i. - y do.
i 1!:2 ]2
end.
i.0 0
)
Though it's rare to see J code like this.
[edit] Java
for(i = 10; i >= 0; --i){ System.out.println(i); }
[edit] JavaScript
for (var i=10; i>=0; --i) print(i);
[edit] 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]
[edit] MAXScript
for i in 10 to 0 by -1 do print i
[edit] OCaml
for i = 10 downto 0 do Printf.printf "%d\n" i done
[edit] Pascal
for i := 10 downto 0 do writeln(i);
[edit] Perl
foreach (reverse 0..10) { print "$_\n"; }
[edit] Pop11
lvars i; for i from 10 by -1 to 0 do printf(i, '%p\n'); endfor;
[edit] Python
for i in xrange(10, -1, -1): print i
[edit] Scheme
(do ((i 10 (- i 1))) ((< i 0)) (display i) (newline))
[edit] UnixPipes
yes \ |cat -n |head -n 10 | tac

