Loops/Downward for: Difference between revisions
Added BASIC |
|||
Line 8: | Line 8: | ||
end loop; |
end loop; |
||
</ada> |
</ada> |
||
=={{header|BASIC}}== |
|||
{{works with|QuickBasic|4.5}} |
|||
<qbasic>for i = 10 to 0 step -1 |
|||
print i |
|||
next i</qbasic> |
|||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
Revision as of 21:35, 12 December 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>
BASIC
<qbasic>for i = 10 to 0 step -1
print i
next i</qbasic>
Befunge
55+>:.:v ^ -1_@
C
<c> int i; for(i = 10; i >= 0; --i)
printf("%d\n",i);
</c>
C++
<cpp> for(int i = 10; i >= 0; --i)
std::cout << i << "\n";
</cpp>
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>
Common Lisp
<lisp>(loop for i from 10 downto 1 do
(print i))</lisp>
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>
dc
does not use GNU extensions
[]s. is a comment
c clears the stack
[~...]p s. to print strings
l<register>x executes the macro
uses the macro f - [p] to print, this can be replaced by any complex expressions.
c [macro s(swap) - (a b : b a)]s. [Sa Sb La Lb] ss [macro d(2dup) - (a b : a b a b)]s. [Sa d Sb La d Lb lsx] sd [macro m(for) - ]s. [lfx 1 - ldx !<m ] sm 0 10 ldx [p] sf !<m q
Using it
|dc < ./for.dc 10 9 ... 0
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
Haskell
import Control.Monad forM_ [10,9..0] print
IDL
Using a loop (with an "increment of minus one" ):
for i=10,0,-1 do print,i
But in IDL one would rarely use loops (for anything) since practically everything can be done with vectors/arrays.
The "IDL way of doing things" for the countdown requested in the task would probably be this:
print,10-indgen(11)
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.
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
Oberon-2
FOR i := 10 TO 0 BY -1 DO Out.Int(i,0); END;
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>
PHP
<php>for ($i = 10; $i >= 0; $i--)
echo "$i\n";</php>
or <php>foreach (range(10, 0) as $i)
echo "$i\n";</php>
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>
Ruby
10.downto(1) do |i|
puts i
end
Scheme
<scheme>(do ((i 10 (- i 1)))
((< i 0)) (display i) (newline))</scheme>
SNUSP
++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi \n counter #\?>.</ \ @@@+@+++++# itoa loop
UnixPipes
yes \ |cat -n |head -n 10 | tac
V
10 [0 >] [dup puts pred] while