Loops/Downward for: Difference between revisions
Content deleted Content added
Loops/Downward for in Asymptote |
Langurmonkey (talk | contribs) |
||
(34 intermediate revisions by 18 users not shown) | |||
Line 25:
=={{header|11l}}==
<
print(i)</
=={{header|360 Assembly}}==
Use of BXLE and BCT opcodes.
<
LOOPDOWN CSECT
USING LOOPDOWN,R12
Line 55:
BUFFER DC CL80' '
YREGS
END LOOPDOWN</
=={{header|6502 Assembly}}==
Code is called as a subroutine (i.e. JSR Start).
Printing routines are only partially coded here, specific OS/hardware routines for printing are left unimplemented.
<
;value contained in the Accumulator
Send = $9000 ;routine not implemented here
Line 97:
ORA #$30
JSR Send ;routine not implemented here
RTS </
=={{header|68000 Assembly}}==
Code is called as a subroutine, i.e. "JSR ForLoop." OS/Hardware specific printing subroutines are unimplemented here.
<
MOVE.W #10,D0
loop:
JSR Print_D0_As_Ascii ;some routine that converts the digits of D0 into ascii characters and prints them to screen.
DBRA D0,loop ;repeat until D0.W = $FFFF
rts</
=={{header|8080 Assembly}}==
This assumes the CP/M operating system.
<syntaxhighlight lang="asm">
;-------------------------------------------------------
; some useful equates
;-------------------------------------------------------
bdos equ 5h ; location ofjump to BDOS entry point
wboot equ 0 ; BDOS warm boot function
conout equ 2 ; write character to console
;-------------------------------------------------------
; main code
;-------------------------------------------------------
org 100h
lxi sp,stack ; set up a stack
;
lxi h,10 ; starting value for countdown
loop: call putdec ; print it
mvi a,' ' ; space between numbers
call putchr
dcx h ; decrease count by 1
mov a,h ; are we done (HL = 0)?
ora l
jnz loop ; no, so continue with next number
jmp wboot ; otherwise exit to operating system
;-------------------------------------------------------
; console output of char in A register
; preserves BC, DE, HL
;-------------------------------------------------------
putchr: push h
push d
push b
mov e,a
mvi c,conout
call bdos
pop b
pop d
pop h
ret
;---------------------------------------------------------
; Decimal output to console routine
; HL holds 16-bit unsigned binary number to print
; Preserves BC, DE, HL
;---------------------------------------------------------
putdec: push b
push d
push h
lxi b,-10
lxi d,-1
putdec2:
dad b
inx d
jc putdec2
lxi b,10
dad b
xchg
mov a,h
ora l
cnz putdec ; recursive call!
mov a,e
adi '0' ; make printable
call putchr
pop h
pop d
pop b
ret
;----------------------------------------------------------
; data area
;----------------------------------------------------------
stack equ $+128 ; 64-level stack to support recursion
;
end
</syntaxhighlight>
{{out}}
<pre>
10 9 8 7 6 5 4 3 2 1
</pre>
=={{header|8086 Assembly}}==
It is typically much easier for assembly languages to loop downwards than forwards, as they can do so without using a redundant equality check. The 8086's <code>LOOP</code> instruction will loop a section of code, using the <code>CX</code> register as the loop counter.
<
.stack 1024 ;reserve 1 KB for the stack pointer.
Line 169 ⟶ 247:
ret
end start ;EOF</
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopdownward64.s */
Line 224 ⟶ 302:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Ada}}==
<
Put_Line(Integer'Image(I));
end loop;</
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<
print( i )
od</
=={{header|ALGOL 60}}==
{{works with|A60}}
'''Based on the 1962 Revised Report on ALGOL''':
'''begin'''
'''integer''' i;
'''for''' i:=10 '''step''' -1 '''until''' 0 '''do'''
outinteger(1,i)
'''end'''
{{works with|ALGOL 60|OS/360}}
<
'INTEGER' I;
'FOR' I := 10 'STEP' -1 'UNTIL' 0 'DO'
OUTINTEGER(1,I)
'END'</
=={{header|ALGOL 68}}==
Line 254 ⟶ 333:
{{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}}
<
print((i,new line))
OD</
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]].
<
print((i,new line))
OD</
=={{header|ALGOL-M}}==
Sadly, ALGOL-M's FOR statement does not allow a negative value
for STEP, so in order to count down we either have to use a WHILE loop
or move the subtraction into the body of the FOR loop
<syntaxhighlight lang="ALGOL">
begin
integer i;
write("First approach :");
i := 10;
while (i >= 0) do
begin
writeon(i);
i := i - 1;
end;
write("Second approach:");
for i := 0 step 1 until 10 do
writeon(10-i);
end
</syntaxhighlight>
{{out}}
<pre>
First approach : 10 9 8 7 6 5 4 3 2 1 0
Second approach: 10 9 8 7 6 5 4 3 2 1 0
</pre>
=={{header|ALGOL W}}==
<
for i := 10 step -1 until 0 do
begin
write( i )
end
end.</
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
#include <flow.h>
Line 283 ⟶ 391:
BACK-IF-NOT-ZERO(i--, ciclo abajo)
END
</syntaxhighlight>
{{out}}
<pre>
Line 299 ⟶ 407:
</pre>
=={{header|AmigaE}}==
<
DEF i
FOR i := 10 TO 0 STEP -1
WriteF('\d\n', i)
ENDFOR
ENDPROC</
=={{header|AppleScript}}==
<
log i
end repeat</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 428 ⟶ 536:
</syntaxhighlight>
=={{header|Arturo}}==
<
print i
]</
{{out}}
Line 451 ⟶ 559:
=={{header|Asymptote}}==
Asymptote's control structures are similar to those in C, C++, or Java
<
write(i);
}</
=={{header|AutoHotkey}}==
<
While (x >= 0)
{
Line 463 ⟶ 571:
}
MsgBox % output
</syntaxhighlight>
=={{header|Avail}}==
<
Note that <code>10 to 0 by -1</code> section isn't a fixed part of the loop syntax, but a call to the <code>_to_by_</code> method, which returns a tuple of integers in a range separated by a particular step size, in this case returning <code><10, 9, 8, 7, 6, 5, 4, 3, 2, 1></code>.
=={{header|AWK}}==
<
for(i=10; i>=0; i--) {
print i
}
}</
=={{header|Axe}}==
Axe does not support for loops with step sizes other than 1.
<
Disp 10-I▶Dec,i
End</
=={{header|Bait}}==
<syntaxhighlight lang="bait">
fun main() {
for i := 10; i >= 0; i -= 1 {
println(i)
}
}
</syntaxhighlight>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
==={{header|BaCon}}===
<
FOR i = 10 DOWNTO 0 : PRINT i : NEXT</
==={{header|BASIC256}}===
{{works with|QBasic}}
<
print i; " ";
next i
print
end</
==={{header|BBC BASIC}}===
<
PRINT i%
NEXT</
==={{header|Commodore BASIC}}===
<
20 PRINT I
30 NEXT</
==={{header|Chipmunk Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|FBSL}}===
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE
FOR DIM i = 10 DOWNTO 0
PRINT i
NEXT
PAUSE</syntaxhighlight>
==={{header|FreeBASIC}}===
<
For i As Integer = 10 To 0 Step -1
Line 515 ⟶ 644:
Next
Print
Sleep</
{{out}}
<pre> 10 9 8 7 6 5 4 3 2 1 0</pre>
==={{header|FutureBasic}}===
<
window 1, @"Countdown", ( 0, 0, 400, 300 )
Line 531 ⟶ 658:
next
HandleEvents</syntaxhighlight>
Output:
<pre> 10
9
8
Line 545 ⟶ 670:
2
1
0</pre>
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=b236db5bdb1087fa90e934a5a8210e1f Click this link to run this code]'''
<
Dim siCount As Short
Line 557 ⟶ 681:
Next
End</
Output:
<pre>10 9 8 7 6 5 4 3 2 1 0</pre>
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="gwbasic">10 FOR I% = 10 TO 0 STEP -1
20 PRINT I%
30 NEXT I%</syntaxhighlight>
==={{header|IS-BASIC}}===
<
110 PRINT I
120 NEXT</
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="lb">for i = 10 to 0 step -1
print i
next i
end</
==={{header|Microsoft Small Basic}}===
<
For i = 10 To 0 Step -1
TextWindow.WriteLine(i)
EndFor</
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|NS-HUBASIC}}===
<
20 PRINT I
30 NEXT</
==={{header|PureBasic}}===
<
Debug i
Next</
==={{header|QB64}}===
''CBTJD'': 2020/03/14
<
PRINT n
NEXT</
==={{header|
<
print i
next i</
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<
print i
next i
end</
==={{header|TI-83 BASIC}}===
<syntaxhighlight lang="ti83b">:For(I,10,0,-1
:Disp I
:End</syntaxhighlight>
==={{header|TI-89 BASIC}}===
<
For i, 10, 0, –1
Disp i
EndFor</
==={{header|True BASIC}}===
{{works with|QBasic}}
<
PRINT i; " ";
NEXT i
PRINT
END</
==={{Header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">10 REM Loops/Downward for
20 LET I = 10
30 IF
40 PRINT I
50 LET I = I - 1
60 GOTO 30
70 END</syntaxhighlight>
==={{header|
<syntaxhighlight lang="vb">For i = 10 To 0 Step -1
Debug.Print i
Next i</syntaxhighlight>
==={{header|Visual Basic .NET}}===
<syntaxhighlight lang="vbnet">For i = 10 To 0 Step -1
Console.WriteLine(i)
Next</syntaxhighlight>
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<
DECLARE FUNCTION Entry()
Line 668 ⟶ 796:
NEXT i%
END FUNCTION
END PROGRAM</
==={{header|Yabasic}}===
{{works with|QBasic}}
<syntaxhighlight lang="yabasic">for i = 10 to 0 step -1
print i, " ";
next i
print
end</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
<
20 PRINT l
30 NEXT l</
=={{header|Batch File}}==
<
for /l %%D in (10,-1,0) do echo %%D</
=={{header|bc}}==
<
quit</
=={{header|Befunge}}==
<
@ ^ -1_</
=={{header|BQN}}==
Line 691 ⟶ 827:
Each (<code>¨</code>) is an operator in BQN that helps with emulating loops like for and foreach.
<syntaxhighlight lang
=={{header|Bracmat}}==
<
& whl'(out$!i&!i+-1:~<0:?i)</
=={{header|Brainf***}}==
<
<+.-. //print the digits 1 and 0
>++++++++++. //cell 1 now contains the carriage return; print it!
Line 704 ⟶ 840:
<<+++++++++>> //cell 0 now contains 57 the ASCII code for "9"
[<<.->.>-] //print and decrement the display digit; print a newline; decrement the loop variable
<<.>. //print the final 0 and a final newline</
=={{header|Brat}}==
<
=={{header|C}}==
<
for(i = 10; i >= 0; --i)
printf("%d\n",i);</
=={{header|C sharp|C#}}==
<
{
Console.WriteLine(i);
}</
=={{header|C++}}==
<
std::cout << i << "\n";</
=={{header|C3}}==
<syntaxhighlight lang="c3">for (int i = 10; i >= 0; i--)
{
io::printn(i);
}</syntaxhighlight>
=={{header|Ceylon}}==
<
print(i);
}</
=={{header|Chapel}}==
<
writeln(i);</
In case you wonder why it is not written as <tt>10..1 by -1</tt>: <tt>by</tt> is an operator that works on ranges, and it should work the same when the range was defined earlier, like in
<
for i in r by -1 do { ... }</
=={{header|Clipper}}==
<
? i
NEXT</
=={{header|Clojure}}==
<
=={{header|COBOL}}==
free-form
<
program-id. countdown.
environment division.
Line 763 ⟶ 905:
display counter-disp
end-perform
stop run.</
{{out}}
<pre>10
Line 780 ⟶ 922:
This could be written either in the array comprehension style,
or in "regular" for loop style.
<
console.log i for i in [10..0]
Line 788 ⟶ 930:
# More compact version of the above
for i in [10..0] then console.log i</
<pre>10
9
Line 804 ⟶ 946:
=={{header|ColdFusion}}==
With tags:
<
#i#
</cfloop></
With script:
<
for( i = 10; i <= 0; i-- )
{
writeOutput( i );
}
</cfscript></
=={{header|Common Lisp}}==
<
(print i))</
=== Using DO ===
<
(do ((n 10 (decf n))) ; Initialize to 0 and downward in every loop
((< n 0)) ; Break condition when negative
(print n)) ; On every loop print value
</syntaxhighlight>
=== Using Tagbody and Go ===
<
(let ((count 10)) ; Create local variable count = 10
(tagbody
Line 835 ⟶ 977:
(if (not (< count 0)) ; Ends loop when negative
(go dec)))) ; Loops back to tag dec
</syntaxhighlight>
=== Using Recursion ===
<
(defun down-to-0 (count)
(print count)
Line 844 ⟶ 986:
(down-to-0 (1- count))))
(down-to-0 10)
</syntaxhighlight>
{{out}}
Line 861 ⟶ 1,003:
</pre>
=={{header|Computer/zero Assembly}}==
<syntaxhighlight lang="6502asm">LDA 28
SUB 29
STA 31
STA 28
BRZ 6 ;branch on zero to STP
JMP 0
STP
...
org 28
byte 11
byte 1
byte 0</syntaxhighlight>
{{out}}[http://www.edmundgriffiths.com/czero.html?hexdump=3c9d5f5ca6c0e00000000000000000000000000000000000000000000b01000a Emulator with program loaded]
=={{header|Crystal}}==
<
puts i
}
</syntaxhighlight>
=={{header|D}}==
<
void main() {
Line 877 ⟶ 1,034:
foreach_reverse (i ; 0 .. 10 + 1)
writeln(i);
}</
{{out}}
<pre>10
Line 903 ⟶ 1,060:
0</pre>
=={{header|
<syntaxhighlight lang="dart">void main() {
for (var i = 10; i >= 0; --i) {
print(i);
}
}</syntaxhighlight>
=={{header|dc}}==
does not use GNU extensions
Line 917 ⟶ 1,080:
uses the macro f - [p] to print, this can be replaced by any complex expressions.
<
[macro s(swap) - (a b : b a)]s.
Line 929 ⟶ 1,092:
0 10 ldx [p] sf !<m
q</
Using it
<
10
9
...
0</
=={{header|Delphi}}==
Line 942 ⟶ 1,105:
=={{header|Draco}}==
<
byte i;
for i from 10 downto 0 do
write(i," ")
od
corp</
{{out}}
<pre>10 9 8 7 6 5 4 3 2 1 0</pre>
=={{header|DWScript}}==
<
PrintLn(i);</
=={{header|E}}==
<
=={{header|EasyLang}}==
<syntaxhighlight lang="text">for i = 10 downto 0
print i
.</
=={{header|EchoLisp}}==
<
(for ((longtemps-je-me-suis-couché-de-bonne-heure (in-range 10 -1 -1)))
(write longtemps-je-me-suis-couché-de-bonne-heure))
→ 10 9 8 7 6 5 4 3 2 1 0
</syntaxhighlight>
=={{header|EDSAC order code}}==
Including a full routine to print integers in decimal would probably be overkill; at least, it would obscure what is essentially a simple program. We therefore cheat slightly by printing "10\r\n" manually, and using the loop only to print "9\r\n" down to "0\r\n". Note that character codes are stored in the high 5 bits of the 17-bit EDSAC word: so we actually count down from 36,864 to 0 in steps of 4,096.
<
==========================
Line 1,027 ⟶ 1,190:
[ 20 ] OF [ character c = '9' ]
EZPF [ start when loaded ]</
=={{header|EGL}}==
<
SysLib.writeStdout( i );
end</
=={{header|Ela}}==
Line 1,038 ⟶ 1,201:
===Standard Approach===
<
each [] = do return ()
Line 1,045 ⟶ 1,208:
each xs
each [10,9..0] ::: IO</
===Alternative Approach===
<
countDown m n | n < m = do return ()
Line 1,056 ⟶ 1,219:
countDown m (n - 1)
_ = countDown 0 10 ::: IO</
=={{header|Elixir}}==
<
10
9
Line 1,071 ⟶ 1,234:
1
0
:ok</
=={{header|Erlang}}==
<
-module(downward_loop).
-export([main/0]).
Line 1,088 ⟶ 1,251:
io:format("~p~n",[N])
end.
</syntaxhighlight>
{{out}}
<pre>10
Line 1,104 ⟶ 1,267:
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
FOR I%=10 TO 0 STEP -1 DO
PRINT(I%)
END FOR
</syntaxhighlight>
=={{header|Euphoria}}==
<
? i
end for</
=={{header|F_Sharp|F#}}==
Using an enumerable expression:
<
printfn "%d" i</
Using the 'downto' keyword:
<
printfn "%d" i</
=={{header|Factor}}==
<
=={{header|FALSE}}==
<
=={{header|Fantom}}==
<
class DownwardFor
{
Line 1,143 ⟶ 1,306:
}
}
</syntaxhighlight>
=={{header|Fermat}}==
<
=={{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.
<
=={{header|Fortran}}==
{{Works with|Fortran|90 and later}}
<
WRITE(*, *) i
END DO</
{{works with|Fortran|77 and later}}
<
C Initialize the loop parameters.
INTEGER I, START, FINISH, STEP
Line 1,180 ⟶ 1,333:
STOP
END</
=={{header|Frink}}==
<
for i = 10 to 0 step -1
println[i]
</syntaxhighlight>
=={{header|GAP}}==
<
Print(i, "\n");
od;</
=={{header|GML}}==
<
show_message(string(i))</
=={{header|Go}}==
<
fmt.Println(i)
}</
::<
import "fmt"
Line 1,214 ⟶ 1,367:
}
fmt.Println("blast off")
}</
=={{header|Groovy}}==
<
println i
}</
=={{header|Harbour}}==
<
? i
NEXT</
=={{header|Haskell}}==
<
main :: IO ()
main = forM_ [10,9 .. 0] print</
=={{header|Haxe}}==
Line 1,236 ⟶ 1,389:
Haxe lacks a downward for-loop, but it is easy to create an iterator to serve that purpose.
<
var end:Int;
var step:Int;
Line 1,257 ⟶ 1,410:
}
}
}</
{{out}}
Line 1,263 ⟶ 1,416:
=={{header|hexiscript}}==
<
println i
endfor</
=={{header|HicEst}}==
<
WRITE() i
ENDDO</
=={{header|HolyC}}==
<
for (i = 10; i >= 0; --i)
Print("%d\n", i);</
=={{header|Icon}} and {{header|Unicon}}==
There are four looping controls 'every', 'repeat', 'until', and 'while' (see [[Icon%2BUnicon/Intro#Looping_Controls|Introduction to Icon and Unicon/Looping Controls]] for more information.) The closest to a 'for' loop is 'every'.
<
# things to do within the loop
}
</syntaxhighlight>
=={{header|IDL}}==
Line 1,288 ⟶ 1,441:
Using a loop (with an "increment of minus one" ):
<
But in IDL one would rarely use loops (for anything) since practically everything can be done with vectors/arrays.
Line 1,294 ⟶ 1,447:
The "IDL way of doing things" for the countdown requested in the task would probably be this:
<syntaxhighlight lang
=={{header|Inform 6}}==
<
print i, "^";</
=={{header|Io}}==
<
i println
)</
=={{header|J}}==
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
<syntaxhighlight lang=J> ,. i. -11
10
9
8
7
6
5
4
3
2
1
0
</syntaxhighlight>
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).
<
for_i. i. - y do.
smoutput i
end.
)</
Though it's rare to see J code like this.
Line 1,321 ⟶ 1,486:
That said, a convenient routine for generating intervals in J might be:
<
For example:
<
10 9 8 7 6 5 4 3 2 1 0</
(or <code>,.10 thru 0</code> if you want each number on a line by itself)
Line 1,333 ⟶ 1,498:
=={{header|Java}}==
<
for (int i = 10; i >= 0; i--) {
System.out.println(i);
}
</syntaxhighlight>
=={{header|JavaScript}}==
<
Alternatively, remaining for the moment within an imperative idiom of JavaScript, in which programs are composed of statements, we could trim the computational costs over longer reversed iterations by moving the mutation into the test, and dropping the third term of a for() statement:
<
and it sometimes might be more natural, especially at scales at which optimisation becomes an issue, to go one step further and express the same computation with the more economical while statement.
<
while (i--) console.log(i);</
In a functional idiom of JavaScript, however, we need an expression with a value (which can be composed within superordinate expressions), rather than a statement, which produces a side-effect but returns no information-bearing value.
Line 1,355 ⟶ 1,520:
If we have grown over-attached to the English morpheme 'for', we might think first of turning to '''Array.forEach()''', and write something like:
<
return Array.apply(null, Array(n - m + 1)).map(
function (x, i) {
Line 1,367 ⟶ 1,532:
console.log(x);
}
);</
Line 1,374 ⟶ 1,539:
We can get an expression (assuming that the range() function (above) is defined) but replacing Array.forEach with '''Array.map()'''
<
range(0, 10).reverse().map(
function (x) {
Line 1,380 ⟶ 1,545:
}
).join('\n')
);</
but in this case, we are simply mapping an identity function over the values, so the expression simplifies down to:
<
range(0, 10).reverse().join('\n')
);</
=={{header|jq}}==
If range/3 is available in your jq:
<syntaxhighlight lang
Otherwise:
range(-10;1) | -.
=={{header|Julia}}==
<
println(i)
end</
=={{header|Kotlin}}==
<
// version 1.3.61
Line 1,406 ⟶ 1,571:
(10 downTo 0).forEach { println(it) }
}
</syntaxhighlight>
=={{header|Lambdatalk}}==
<
{def downward_for
{lambda {:i}
Line 1,419 ⟶ 1,584:
{downward_for 10}
-> 10 9 8 7 6 5 4 3 2 1 0 (end of loop)
</syntaxhighlight>
=={{header|langur}}==
<syntaxhighlight lang="langur">
for i in 10..0 {
writeln i
}
</syntaxhighlight>
<syntaxhighlight lang
for i = 10;
writeln i
}
</syntaxhighlight>
=={{header|Lasso}}==
<
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
i is number
procedure:
for i from 10 to -1 step -1 do
display i lf
repeat</syntaxhighlight>
=={{header|Lhogho}}==
Slightly different syntax for <code>for</code> compared to Logo.
<
=={{header|Lingo}}==
<
put i
end repeat</
=={{header|Lisaac}}==
<
i.println;
};</
=={{header|LiveCode}}==
Livecode's repeat "for" variant does not have a "down to" form, in a function you would need to manually decrement a counter
<
repeat for 10 times
put x & return
add -1 to x
end repeat</
A more idiomatic approach using "with" variant of repeat which does have a "down to" form
<
put n
end repeat</
=={{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.
<
=={{header|Lua}}==
<
for i=10,0,-1 do
print(i)
end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
Line 1,481 ⟶ 1,657:
There is a slower For, the For Next style:
<syntaxhighlight lang="m2000 interpreter">
For i=1 to 10 step 2 : Print i : Next i
</syntaxhighlight>
We have to use Exit For to exit from that type of For.
Line 1,493 ⟶ 1,669:
<syntaxhighlight lang="m2000 interpreter">
Form 80, 50
Module Checkit {
Line 1,528 ⟶ 1,704:
}
CheckIt
</syntaxhighlight>
=={{header|M4}}==
<
`ifelse($#,0,``$0'',
`ifelse(eval($2 $3),1,
Line 1,537 ⟶ 1,713:
for(`x',`10',`>=0',`-1',`x
')</
=={{header|Maple}}==
Using an explicit loop:
<
Pushing the loop into the kernel:
<
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,551 ⟶ 1,727:
Using For:
<
Using Do:
<
Using Scan:
<
=={{header|MATLAB}} / {{header|Octave}}==
<
printf('%d\n',k)
end; </
A vectorized version of the code is
<
=={{header|Maxima}}==
<
=={{header|MAXScript}}==
<
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module loops_downward_for.
:- interface.
Line 1,587 ⟶ 1,763:
io.write_int(I, !IO), io.nl(!IO)
),
int.fold_down(Print, 1, 10, !IO).</
=={{header|Metafont}}==
<
end</
The basic set of macros for Metafont defines <tt>downto</tt>, so that we can write
<
=={{header|min}}==
{{works with|min|0.19.6}}
<
Or
<
=={{header|MiniScript}}==
<
print i
end for</
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 0 П0 ИП0 L0 03 С/П</
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 1,624 ⟶ 1,800:
InOut.WriteLn
END
END Downward.</
=={{header|Modula-3}}==
<
IO.PutInt(i);
END;</
=={{header|MUMPS}}==
<
NEW I FOR I=10:-1:1 WRITE I WRITE:I'=1 ", "
KILL I QUIT</
=={{header|N/t/roff}}==
<syntaxhighlight lang="nroff">
.nr a 11 1
.while (\na > 0) \{\
\n-a
.\}
</syntaxhighlight>
{{out}}
<pre>10 9 8 7 6 5 4 3 2 1 0
</pre>
=={{header|Nemerle}}==
<
<
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 1,650 ⟶ 1,837:
say i_.right(2)
end i_
</syntaxhighlight>
=={{header|NewLISP}}==
<
(println i))</
=={{header|Nim}}==
<
{{out}}
<pre>10
Line 1,670 ⟶ 1,857:
1
0</pre>
=={{header|Nu}}==
<syntaxhighlight lang="nu">
for i in 10..0 {print $i}
</syntaxhighlight>
=={{header|Oberon-2}}==
<
Out.Int(i,0);
END;</
=={{header|Objeck}}==
<
for(i := 10; i >= 0; i--;) {
i->PrintLine();
};
</syntaxhighlight>
=={{header|OCaml}}==
<
Printf.printf "%d\n" i
done</
=={{header|Octave}}==
<
% ...
endfor</
=={{header|Oforth}}==
<
=={{header|Oz}}==
<
{Show I}
end</
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
<
writeln(i);</
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
##
for var i:=10 downto 0 do
Print(i);
</syntaxhighlight>
{{out}}
<pre>
10 9 8 7 6 5 4 3 2 1 0
</pre>
=={{header|Peloton}}==
English fixed-length opcodes
<
Simplified Chinese variable-length opcodes
<
=={{header|Perl}}==
<
print "$_\n";
}</
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">10</span> <span style="color: #008080;">to</span> <span style="color: #000000;">0</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">i</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/Downward_for
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
( 10 0 -1 ) for ? endfor</syntaxhighlight>
=={{header|PHP}}==
<
echo "$i\n";</
or
<
echo "$i\n";</
=={{header|PicoLisp}}==
<
(println I) )</
or:
<
=={{header|Pike}}==
<
for(int i = 10; i >= 0; i--){
write(i + "\n");
}
}</
=={{header|PL/0}}==
<syntaxhighlight lang="pascal">
var i;
begin
i := 10;
while i > -1 do
begin
! i;
i := i - 1
end
end.
</syntaxhighlight>
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
do i = 10 to 0 by -1;
put skip list (i);
end;
</syntaxhighlight>
=={{header|Plain English}}==
One way might be:
<
Start up.
Put 11 into a counter.
Line 1,773 ⟶ 1,997:
Subtract 1 from the counter.
If the counter is less than the number, say yes.
Say no.</
=={{header|Pop11}}==
<
for i from 10 by -1 to 0 do
printf(i, '%p\n');
endfor;</
=={{header|PowerShell}}==
<
$i
}</
Alternatively, the range operator might be used as well which simply returns a contiguous range of integers:
<syntaxhighlight lang
=={{header|Prolog}}==
Although Prolog has a between(Lo,Hi,Value) iterator, there is no built in equivalent for iterating descending values. This is not a show stopper, as it's easy enough to write one.
<
rfor(Hi,Lo,Val) :- Hi > Lo, H is Hi - 1, !, rfor(H,Lo,Val).
reverse_iter :-
rfor(10,0,Val), write(Val), nl, fail.
reverse_iter.</
<pre>?- reverse_iter.
10
Line 1,812 ⟶ 2,036:
=={{header|Python}}==
<
print i</
=== List comprehension ===
<
<
pprint.pprint([i for i in xrange(10, -1, -1)])
</syntaxhighlight>
=={{header|Quackery}}==
<
{{Out}}
Line 1,831 ⟶ 2,055:
=={{header|R}}==
<
=={{header|Racket}}==
<
#lang racket
(for ([i (in-range 10 -1 -1)])
(displayln i))
</syntaxhighlight>
=={{header|Raku}}==
Line 1,845 ⟶ 2,069:
{{works with|Rakudo Star|2010.08}}
<syntaxhighlight lang="raku"
.say;
}</
=={{header|REBOL}}==
<
=={{header|Retro}}==
<syntaxhighlight lang
=={{header|REXX}}==
===version 1===
(equivalent to version 2 and version 3)
<
say j
end</
===version 2===
(equivalent to version 1 and version 3)
<
say j
end</
===version 3===
Line 1,872 ⟶ 2,096:
<br><br>Anybody who programs like this should be hunted down and shot like dogs!
<br><br>Hurrumph! Hurrumph!
<
say j
j=j+1 /*this increments the DO index. Do NOT program like this! */
end</
===version 4===
This example isn't compliant to the task,
but it shows that the increment/decrement can be a non-integer:
<
say j
end</
=={{header|Ring}}==
count from 10 to 0 by -1 step:
<
for i = 10 to 0 step -1 see i + nl next
</syntaxhighlight>
=={{header|RPL}}==
≪ 10 1 '''FOR''' n
n
-1 '''STEP'''
≫
=={{header|Ruby}}==
<
puts i
end</
=={{header|Rust}}==
<
for i in (0..=10).rev() {
println!("{}", i);
}
}</
=={{header|Salmon}}==
<
x!;</
=={{header|Sather}}==
<
main is
i:INT;
Line 1,914 ⟶ 2,144:
end;
end;
end;</
=={{header|S-BASIC}}==
<syntaxhighlight lang="BASIC">
var i = integer
for i = 10 to 1 step -1
print i;
next i
end
</syntaxhighlight>
{{out}}
<pre>
10 9 8 7 6 5 4 3 2 1
</pre>
=={{header|Scala}}==
<
//or
10 to 0 by -1 foreach println</
=={{header|Scheme}}==
<
((< i 0))
(display i)
(newline))</
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=10:-1:0
printf("%d\n",i)
end</
{{out}}
<pre style="height:20ex">
Line 1,948 ⟶ 2,192:
=={{header|Seed7}}==
<
writeln(i);
end for;</
=={{header|Sidef}}==
'''for(;;)''' loop:
<
say i
}</
'''for-in''' loop:
<
say i
}</
'''.each''' method:
<
say i
}</
=={{header|Simula}}==
<
Integer i;
for i := 10 step -1 until 0 do
Line 1,976 ⟶ 2,220:
OutImage
END
END</
=={{header|Slate}}==
<
=={{header|Smalltalk}}==
<
aNumber displayNl.
].
Line 1,988 ⟶ 2,232:
10 downTo: 0 do:[:eachNumber |
eachNumber displayNl.
]</
Both enumerate 10 to 0 inclusive.
Non-Smalltalkers might be confused when seeing:
<
eachNumber displayNl.
]</
which has the same effect, but a slightly different mechanism.
Line 2,004 ⟶ 2,248:
The nice thing with Intervals is that they can be concatenated with a <tt>","</tt> operator (like all collections); thus, I could also write:
<
eachNumber displayNl.
]</
to enumerate in a different order,
<br>or combine ranges with a constant array:
<
each displayNl.
]</
or with a computed array:
<
each displayNl.
]</
PS: there is also a reverse do, as in:
<
each displayNl.
]</
=={{header|SNOBOL4}}==
<
LOOP OUTPUT = COUNT
COUNT = COUNT - 1
GE(COUNT, 0) :S(LOOP)
END</
=={{header|SNUSP}}==
<
\n counter #\?>.</ \ @@@+@+++++# itoa
loop</
=={{header|Sparkling}}==
<
print(i);
}</
=={{header|Spin}}==
Line 2,044 ⟶ 2,288:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,060 ⟶ 2,304:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>10 9 8 7 6 5 4 3 2 1 0</pre>
=={{header|SPL}}==
<
#.output(i)
<</
=={{header|SSEM}}==
The SSEM can't print, so the results are stored in an array at addresses 22 to 31. Array access is done using self-modifying code: on each iteration we subtract the current value of <tt>n</tt> (stored at address 18) from the illegal instruction <tt>c to 32</tt>, yielding the actual instruction we use to store <tt>n</tt> into the array.
<
11001000000001100000000000000000 1. c to 19
11001000000000100000000000000000 2. -19 to c
Line 2,089 ⟶ 2,333:
11111111111111111111111111111111 16. -1
00000100000001100000000000000000 17. c to 32
01010000000000000000000000000000 18. 10</
=={{header|Stata}}==
See '''[https://www.stata.com/help.cgi?forvalues forvalues]''' and '''[https://www.stata.com/help.cgi?foreach foreach]''' in Stata help.
<
display `n'
}
Line 2,104 ⟶ 2,348:
foreach n of numlist 10/0 {
display `n'
}</
=={{header|Swift}}==
<
println(i)
}</
Alternately:
<
println(i)
}</
In Swift 1.2 Alternately:
<
println(i)
}</
Alternately (removed in Swift 3):
<
println(i)
}</
Swift 3:
<
print(i)
}</
=={{header|Tailspin}}==
Not really a for-loop, but it sends a stream of values where each gets treated the same way.
<
10..0:-1 -> '$;
' -> !OUT::write
</syntaxhighlight>
=={{header|Tcl}}==
<
puts $i
}
# puts "We have liftoff!"</
=={{header|Trith}}==
<
<
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
LOOP n=10,0,-1
PRINT n
ENDLOOP
</syntaxhighlight>
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<
while test $i -ge 0; do
echo $i
Line 2,166 ⟶ 2,410:
# or
seq 10 -1 0</
----
{{works with|bash}}
<
echo $Z
done
Line 2,179 ⟶ 2,423:
echo $Z
done
</syntaxhighlight>
=={{header|UnixPipes}}==
{{works with|OpenBSD|4.9}}
<
expr $n - 1
done | tail -r</
This pipe uses several nonstandard commands: <code>cat -n</code> and <code>tail -r</code> might not work with some systems.
Line 2,191 ⟶ 2,435:
=={{header|Ursa}}==
<
for (set i 10) (> i -1) (dec i)
out i endl console
end for</
=={{header|V}}==
<
[0 >]
[dup puts pred]
while</
=={{header|Vala}}==
<
stdout.printf("%d\n", i);</
=={{header|Vedit macro language}}==
<
Num_Type(#1)
}</
=={{header|Verilog}}==
<syntaxhighlight lang="verilog">
module main;
integer i;
Line 2,227 ⟶ 2,466:
end
endmodule
</syntaxhighlight>
=={{header|
<syntaxhighlight lang="v (vlang)">fn main() {
for i := 10; i >= 0; i-- {
print('$i ')
}
}</
{{out}}
Line 2,247 ⟶ 2,481:
=={{header|Wart}}==
<syntaxhighlight lang="text">for i 10 (i >= 0) --i
prn i</
=={{header|Wren}}==
<
System.print()</
{{out}}
Line 2,260 ⟶ 2,494:
=={{header|XPL0}}==
<
int I;
for I:= 10 downto 0 do
[IntOut(0, I); CrLf(0)]</
=={{header|Z80 Assembly}}==
Because of the way looping typically works in hardware, as well as 10 being two digits, it's more efficient to only print numbers 9 through 1 in the loop body, and do the rest outside it.
<syntaxhighlight lang="z80">org &1000
LD A,'1'
CALL &BB5A
LD A,'0'
CALL &BB5A
LD B,9
LD A,'9'
for:
CALL &BB5A
DEC A
DJNZ for
LD A,'0'
JP &BB5A ;its RET returns to BASIC for us.</syntaxhighlight>
{{out}}
<pre>Amstrad Microcomputer (v4)
(c) 1985 Amstrad plc
and Locomotive Software Ltd.
ParaDOS V1.2+ (c)2015 BitWise Systems.
BASIC 1.1
Ready
call &1000
109876543210
Ready</pre>
=={{header|zkl}}==
<
[10..0,-1].apply() //-->L(10,9,8,7,6,5,4,3,2,1,0)
// tail recursion
fcn(n){ n.println(); if(n==0)return(); return(self.fcn(n-1)) }(10)</
=={{header|Zig}}==
<
pub fn main() !void {
Line 2,280 ⟶ 2,546:
try std.io.getStdOut().writer().print("{d}\n", .{i});
}
}</
|