Loops/Downward for: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl 6}}: Whoops, this doesn't work with the latest release. Named a Git commit that it does work with.)
m (Fixed lang tags.)
Line 3: Line 3:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>
<lang ada>for I in reverse 0..10 loop
for I in reverse 0..10 loop
Put_Line(Integer'Image(I));
Put_Line(Integer'Image(I));
end loop;
end loop;</lang>
</lang>
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{works with|ALGOL 68|Standard - no extensions to language used}}
{{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>FOR i FROM 10 BY -1 TO 0 DO
<pre>
FOR i FROM 10 BY -1 TO 0 DO
print((i,new line))
print((i,new line))
OD
OD</lang>
</pre>
As a common extension the DOWNTO is sometimes included to optimise
As a common extension the DOWNTO is sometimes included to optimise
the loop termination logic. The DOWNTO is available in Marcel's
the loop termination logic. The DOWNTO is available in Marcel's
[[ALGOL 68G]] and [[Cambridge ALGOL 68C]].
[[ALGOL 68G]] and [[Cambridge ALGOL 68C]].
<lang algol68>FOR i FROM 10 DOWNTO 0 DO
<pre>
FOR i FROM 10 DOWNTO 0 DO
print((i,new line))
print((i,new line))
OD
OD</lang>
</pre>


=={{header|AmigaE}}==
=={{header|AmigaE}}==
Line 34: Line 28:
ENDPROC</lang>
ENDPROC</lang>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>
<lang AutoHotkey>x := 10
x := 10
While (x >= 0)
While (x >= 0)
{
{
Line 57: Line 50:


=={{header|Befunge}}==
=={{header|Befunge}}==
55+>:.:v
<lang befunge>55+>:.:v
^ -1_@
^ -1_@</lang>


=={{header|C}}==
=={{header|C}}==
<lang c>
<lang c>int i;
int i;
for(i = 10; i >= 0; --i)
for(i = 10; i >= 0; --i)
printf("%d\n",i);
printf("%d\n",i);</lang>
</lang>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>
<lang cpp>for(int i = 10; i >= 0; --i)
std::cout << i << "\n";</lang>
for(int i = 10; i >= 0; --i)
std::cout << i << "\n";
</lang>


=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
Line 82: Line 71:
=={{header|ColdFusion}}==
=={{header|ColdFusion}}==
With tags:
With tags:
<cfloop index = "i" from = "10" to = "0" step = "-1">
<lang cfm><cfloop index = "i" from = "10" to = "0" step = "-1">
#i#
#i#
</cfloop>
</cfloop></lang>
With script:
With script:
<cfscript>
<lang cfm><cfscript>
for( i = 10; i <= 0; i-- )
for( i = 10; i <= 0; i-- )
{
{
writeOutput( i );
writeOutput( i );
}
}
</cfscript>
</cfscript></lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 116: Line 105:
uses the macro f - [p] to print, this can be replaced by any complex expressions.
uses the macro f - [p] to print, this can be replaced by any complex expressions.


<lang dc>c
c

[macro s(swap) - (a b : b a)]s.
[macro s(swap) - (a b : b a)]s.
[Sa Sb La Lb] ss
[Sa Sb La Lb] ss

[macro d(2dup) - (a b : a b a b)]s.
[macro d(2dup) - (a b : a b a b)]s.
[Sa d Sb La d Lb lsx] sd
[Sa d Sb La d Lb lsx] sd

[macro m(for) - ]s.
[macro m(for) - ]s.
[lfx 1 - ldx !<m ] sm
[lfx 1 - ldx !<m ] sm

0 10 ldx [p] sf !<m
0 10 ldx [p] sf !<m
q</lang>
q


Using it
Using it
|dc < ./for.dc
<lang dc>|dc < ./for.dc
10
10
9
9
...
...
0</lang>
0


=={{header|E}}==
=={{header|E}}==
Line 142: Line 131:


=={{header|FALSE}}==
=={{header|FALSE}}==
10[$0>][$." "1-]#.
<lang false>10[$0>][$." "1-]#.</lang>


=={{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.
: loop-down 0 10 do i . -1 +loop ;
<lang forth>: loop-down 0 10 do i . -1 +loop ;</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{Works with|Fortran|90 and later}}
{{Works with|Fortran|90 and later}}
<lang fortran> DO i = 10, 0, -1
<lang fortran>DO i = 10, 0, -1
WRITE(*, *) i
WRITE(*, *) i
END DO</lang>
END DO</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
import Control.Monad
<lang haskell>import Control.Monad
forM_ [10,9..0] print
forM_ [10,9..0] print</lang>


=={{header|IDL}}==
=={{header|IDL}}==
Line 162: Line 151:
Using a loop (with an "increment of minus one" ):
Using a loop (with an "increment of minus one" ):


for i=10,0,-1 do print,i
<lang idl>for i=10,0,-1 do print,i</lang>


But in IDL one would rarely use loops (for anything) since practically everything can be done with vectors/arrays.
But in IDL one would rarely use loops (for anything) since practically everything can be done with vectors/arrays.
Line 168: Line 157:
The "IDL way of doing things" for the countdown requested in the task would probably be this:
The "IDL way of doing things" for the countdown requested in the task would probably be this:


print,10-indgen(11)
<lang idl>print,10-indgen(11)</lang>


=={{header|J}}==
=={{header|J}}==
Line 176: Line 165:


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>3 : 0 ] 11
<pre>
3 : 0 ] 11
for_i. i. - y do.
for_i. i. - y do.
i 1!:2 ]2
i 1!:2 ]2
end.
end.
i.0 0
i.0 0
)
)</lang>
</pre>


Though it's rare to see J code like this.
Though it's rare to see J code like this.
Line 197: Line 184:


=={{header|Lisaac}}==
=={{header|Lisaac}}==
<lang Lisaac>
<lang Lisaac>10.downto 0 do { i : INTEGER;
10.downto 0 do { i : INTEGER;
i.print;
i.print;
'\n'.print;
'\n'.print;
};
};</lang>
</lang>


=={{header|Logo}}==
=={{header|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.
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]
<lang logo>for [i 10 0] [print :i]</lang>


=={{header|M4}}==
=={{header|M4}}==
<lang M4>
<lang M4>define(`for',
define(`for',
`ifelse($#,0,``$0'',
`ifelse($#,0,``$0'',
`ifelse(eval($2 $3),1,
`ifelse(eval($2 $3),1,
Line 216: Line 200:


for(`x',`10',`>=0',`-1',`x
for(`x',`10',`>=0',`-1',`x
')
')</lang>
</lang>


=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 230: Line 213:


=={{header|MAXScript}}==
=={{header|MAXScript}}==
<lang maxscript>for i in 10 to 0 by -1 do print i</lang>
<pre>
for i in 10 to 0 by -1 do print i
</pre>




Line 266: Line 247:


=={{header|Pascal}}==
=={{header|Pascal}}==
<lang pascal>
<lang pascal>for i := 10 downto 0 do
writeln(i);</lang>
for i := 10 downto 0 do
writeln(i);
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
Line 291: Line 270:


=={{header|Pop11}}==
=={{header|Pop11}}==
<lang pop11>lvars i;
<pre>
lvars i;
for i from 10 by -1 to 0 do
for i from 10 by -1 to 0 do
printf(i, '%p\n');
printf(i, '%p\n');
endfor;
endfor;</lang>
</pre>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 306: Line 283:


=={{header|Python}}==
=={{header|Python}}==
<lang python>
<lang python>for i in xrange(10, -1, -1):
print i</lang>
for i in xrange(10, -1, -1):
print i
</lang>


=={{header|R}}==
=={{header|R}}==
Line 335: Line 310:


=={{header|SNUSP}}==
=={{header|SNUSP}}==
++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi
<lang snusp>++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi
\n counter #\?>.</ \ @@@+@+++++# itoa
\n counter #\?>.</ \ @@@+@+++++# itoa
loop
loop</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 347: Line 322:
=={{header|TI-89 BASIC}}==
=={{header|TI-89 BASIC}}==


<pre style="font-family:'TI Uni'">Local i
<lang ti89b>Local i
For i, 10, 0, –1
For i, 10, 0, –1
Disp i
Disp i
EndFor</pre>
EndFor</lang>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
yes \ |cat -n |head -n 10 | tac
<lang bash>yes \ |cat -n |head -n 10 | tac</lang>


=={{header|V}}==
=={{header|V}}==
10
<lang v>10
[0 >]
[0 >]
[dup puts pred]
[dup puts pred]
while
while</lang>


=={{header|Vedit macro language}}==
=={{header|Vedit macro language}}==

Revision as of 22:10, 20 November 2009

Task
Loops/Downward for
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

<lang ada>for I in reverse 0..10 loop

  Put_Line(Integer'Image(I));

end loop;</lang>

ALGOL 68

Works with: ALGOL 68 version Standard - no extensions to language used
Works with: ALGOL 68G version Any - tested with release mk15-0.8b.fc9.i386
Works with: ELLA ALGOL 68 version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386

<lang algol68>FOR i FROM 10 BY -1 TO 0 DO

   print((i,new line))

OD</lang> As a common extension the DOWNTO is sometimes included to optimise the loop termination logic. The DOWNTO is available in Marcel's ALGOL 68G and Cambridge ALGOL 68C. <lang algol68>FOR i FROM 10 DOWNTO 0 DO

   print((i,new line))

OD</lang>

AmigaE

<lang amigae>PROC main()

 DEF i
 FOR i := 10 TO 0 STEP -1
   WriteF('\d\n', i)
 ENDFOR

ENDPROC</lang>

AutoHotkey

<lang AutoHotkey>x := 10 While (x >= 0) {

 output = %output%`n%x%
 x--

} MsgBox % output }</lang>

AWK

<lang awk>BEGIN {

 for(i=10; i>=0; i--) {
    print i
 }

}</lang>

BASIC

<lang qbasic>for i = 10 to 0 step -1

  print i

next i</lang>

Befunge

<lang befunge>55+>:.:v

  ^ -1_@</lang>

C

<lang c>int i; for(i = 10; i >= 0; --i)

 printf("%d\n",i);</lang>

C++

<lang cpp>for(int i = 10; i >= 0; --i)

 std::cout << i << "\n";</lang>

C#

<lang csharp>for (int i = 10; i >= 0; i--) {

  Console.WriteLine(i);

}</lang>

ColdFusion

With tags: <lang cfm><cfloop index = "i" from = "10" to = "0" step = "-1">

 #i#

</cfloop></lang> With script: <lang cfm><cfscript>

 for( i = 10; i <= 0; i-- )
 {
   writeOutput( i );
 }

</cfscript></lang>

Common Lisp

<lang lisp>(loop for i from 10 downto 1 do

 (print i))</lang>

D

<lang d>for(int i = 10; i >= 0; --i) writefln(i)</lang> Foreach Range Statement since D2.003 <lang d>foreach_reverse(i ; 0..10+1) writefln(i) ;</lang>

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.

<lang dc>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</lang>

Using it <lang dc>|dc < ./for.dc 10 9 ... 0</lang>

E

<lang e>for i in (0..10).descending() { println(i) }</lang>

FALSE

<lang false>10[$0>][$." "1-]#.</lang>

Forth

Unlike the incrementing 10 0 DO-LOOP, this will print eleven numbers. The LOOP words detect crossing the floor of the end limit. <lang forth>: loop-down 0 10 do i . -1 +loop ;</lang>

Fortran

Works with: Fortran version 90 and later

<lang fortran>DO i = 10, 0, -1

 WRITE(*, *) i

END DO</lang>

Haskell

<lang haskell>import Control.Monad forM_ [10,9..0] print</lang>

IDL

Using a loop (with an "increment of minus one" ):

<lang idl>for i=10,0,-1 do print,i</lang>

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:

<lang idl>print,10-indgen(11)</lang>

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). <lang j>3 : 0 ] 11

       for_i. i. - y do.
           i 1!:2 ]2 
       end.
    i.0 0
  )</lang>

Though it's rare to see J code like this.


Java

<lang java>for(i = 10; i >= 0; --i){

  System.out.println(i);

}</lang>

JavaScript

<lang javascript>for (var i=10; i>=0; --i) print(i);</lang>

Lisaac

<lang Lisaac>10.downto 0 do { i : INTEGER;

 i.print;
 '\n'.print;

};</lang>

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. <lang logo>for [i 10 0] [print :i]</lang>

M4

<lang M4>define(`for',

  `ifelse($#,0,``$0,
  `ifelse(eval($2 $3),1,
  `pushdef(`$1',$2)$5`'popdef(`$1')$0(`$1',eval($2+$4),$3,$4,`$5')')')')dnl

for(`x',`10',`>=0',`-1',`x ')</lang>

Mathematica

Mathematica provides several ways to iterate over a range of numbers, small subtle differences are amongst them. 3 possible implementations are (exactly the same output):

Using For: <lang Mathematica>For[i = 10, i >= 0, i--, Print[i]]</lang> Using Do: <lang Mathematica>Do[Print[i], {i, 10, 0, -1}]</lang> Using Scan: <lang Mathematica>Scan[Print, Range[10, 0, -1]]</lang>

MAXScript

<lang maxscript>for i in 10 to 0 by -1 do print i</lang>


Metafont

<lang metafont>for i = 10 step -1 until 0: show i; endfor end</lang>

The basic set of macros for Metafont defines downto, so that we can write

<lang metafont>for i = 10 downto 0: show i; endfor end</lang>

Modula-3

<lang modula3>FOR i := 10 TO 0 BY -1 DO

 IO.PutInt(i);

END;</lang>

Oberon-2

<lang oberon2>FOR i := 10 TO 0 BY -1 DO

 Out.Int(i,0);

END;</lang>

OCaml

<lang ocaml>for i = 10 downto 0 do

 Printf.printf "%d\n" i

done</lang>

Octave

<lang octave>for i = 10:-1:0

 % ...

endfor</lang>

Pascal

<lang pascal>for i := 10 downto 0 do

 writeln(i);</lang>

Perl

<lang perl>foreach (reverse 0..10) {

 print "$_\n";

}</lang>

Perl 6

Works with: Rakudo version commit 9792a2e49e7e5cec9392e9b1c86208d935585835

<lang perl6>for 10 ... 0 {

   .say;

}</lang>

PHP

<lang php>for ($i = 10; $i >= 0; $i--)

 echo "$i\n";</lang>

or <lang php>foreach (range(10, 0) as $i)

 echo "$i\n";</lang>

Pop11

<lang pop11>lvars i; for i from 10 by -1 to 0 do

  printf(i, '%p\n');

endfor;</lang>

PowerShell

<lang powershell>for ($i = 10; $i -ge 0; $i--) {

   $i

}</lang> Alternatively, the range operator might be used as well which simply returns a contiguous range of integers: <lang powershell>10..0</lang>

Python

<lang python>for i in xrange(10, -1, -1):

   print i</lang>

R

<lang R>for(i in 10:0) {print(i)}</lang>

Ruby

<lang ruby>10.downto(0) do |i|

  puts i

end</lang>

Scheme

<lang scheme>(do ((i 10 (- i 1)))

   ((< i 0))
   (display i)
   (newline))</lang>

Slate

<lang slate>10 downTo: 1 do: [| :n | print: n]</lang>

Smalltalk

<lang smalltalk>10 to: 1 by: -1 do:[:aNumber |

 aNumber display.
 Character space display.

]</lang>

SNUSP

<lang snusp>++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi

   \n      counter  #\?>.</  \ @@@+@+++++#   itoa
                      loop</lang>

Tcl

<lang tcl>for {set i 10} {$i >= 0} {incr i -1} {

   puts $i

}

  1. puts "We have liftoff!"</lang>

TI-89 BASIC

<lang ti89b>Local i For i, 10, 0, –1

 Disp i

EndFor</lang>

UnixPipes

<lang bash>yes \ |cat -n |head -n 10 | tac</lang>

V

<lang v>10 [0 >]

 [dup puts pred]

while</lang>

Vedit macro language

<lang vedit>for (#1 = 10; #1 >= 0; #1--) {

   Num_Type(#1)

}</lang>

Visual Basic .NET

<lang vbnet>For i = 10 To 0 Step -1

   Console.WriteLine(i)

Next</lang>