Loops/Downward for: Difference between revisions
Add SmallBASIC |
|||
(371 intermediate revisions by more than 100 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Iteration}} |
{{task|Iteration}} |
||
[[Category:Simple]] |
|||
Write a for loop which writes a countdown from 10 to 0. |
|||
;Task: |
|||
Write a <big><big> ''for'' </big></big> loop which writes a countdown from '''10''' to '''0'''. |
|||
;Related tasks: |
|||
* [[Loop over multiple arrays simultaneously]] |
|||
* [[Loops/Break]] |
|||
* [[Loops/Continue]] |
|||
* [[Loops/Do-while]] |
|||
* [[Loops/Downward for]] |
|||
* [[Loops/For]] |
|||
* [[Loops/For with a specified step]] |
|||
* [[Loops/Foreach]] |
|||
* [[Loops/Increment loop index within loop body]] |
|||
* [[Loops/Infinite]] |
|||
* [[Loops/N plus one half]] |
|||
* [[Loops/Nested]] |
|||
* [[Loops/While]] |
|||
* [[Loops/with multiple ranges]] |
|||
* [[Loops/Wrong ranges]] |
|||
<br><br> |
|||
=={{header|11l}}== |
|||
<syntaxhighlight lang="11l">L(i) (10..0).step(-1) |
|||
print(i)</syntaxhighlight> |
|||
=={{header|360 Assembly}}== |
|||
Use of BXLE and BCT opcodes. |
|||
<syntaxhighlight lang="360asm">* Loops/Downward for 27/07/2015 |
|||
LOOPDOWN CSECT |
|||
USING LOOPDOWN,R12 |
|||
LR R12,R15 set base register |
|||
BEGIN EQU * |
|||
* fisrt loop with a BXLE BXLE: Branch on indeX Low or Equal |
|||
LH R2,=H'11' from 10 (R2=11) index |
|||
LH R4,=H'-1' step -1 (R4=-1) |
|||
LH R5,=H'-1' to 0 (R5=-1) |
|||
LOOPI BXLE R2,R4,ELOOPI R2=R2+R4 if R2<=R5 goto ELOOPI |
|||
XDECO R2,BUFFER edit R2 |
|||
XPRNT BUFFER,L'BUFFER print |
|||
B LOOPI |
|||
ELOOPI EQU * |
|||
* second loop with a BCT BCT: Branch on CounT |
|||
LA R2,10 index R2=10 |
|||
LA R3,11 counter R3=11 |
|||
LOOPJ XDECO R2,BUFFER edit R2 |
|||
XPRNT BUFFER,L'BUFFER print |
|||
BCTR R2,0 R2=R2-1 |
|||
ELOOPJ BCT R3,LOOPJ R3=R3-1 if R3<>0 goto LOOPI |
|||
RETURN XR R15,R15 set return code |
|||
BR R14 return to caller |
|||
BUFFER DC CL80' ' |
|||
YREGS |
|||
END LOOPDOWN</syntaxhighlight> |
|||
=={{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. |
|||
<syntaxhighlight lang="6502asm">;An OS/hardware specific routine that is setup to display the Ascii character |
|||
;value contained in the Accumulator |
|||
Send = $9000 ;routine not implemented here |
|||
PrintNewLine = $9050 ;routine not implemented here |
|||
*= $8000 ;set base address |
|||
Start PHA ;push Accumulator and Y register onto stack |
|||
TYA |
|||
PHA |
|||
LDY #10 ;set Y register to loop start value |
|||
TYA ;place loop value in the Accumulator |
|||
Loop JSR PrintTwoDigits |
|||
JSR PrintNewLine |
|||
DEY ;decrement loop value |
|||
BPL Loop ;continue loop if sign flag is clear |
|||
PLA ;pop Y register and Accumulator off of stack |
|||
TAY |
|||
PLA |
|||
RTS ;exit |
|||
;Print value in Accumulator as two hex digits |
|||
PrintTwoDigits |
|||
PHA |
|||
LSR |
|||
LSR |
|||
LSR |
|||
LSR |
|||
JSR PrintDigit |
|||
PLA |
|||
AND #$0F |
|||
JSR PrintDigit |
|||
RTS |
|||
;Convert value in Accumulator to an Ascii hex digit |
|||
PrintDigit |
|||
ORA #$30 |
|||
JSR Send ;routine not implemented here |
|||
RTS </syntaxhighlight> |
|||
=={{header|68000 Assembly}}== |
|||
Code is called as a subroutine, i.e. "JSR ForLoop." OS/Hardware specific printing subroutines are unimplemented here. |
|||
<syntaxhighlight lang="68000devpac">ForLoop: |
|||
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</syntaxhighlight> |
|||
=={{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. |
|||
<syntaxhighlight lang="asm"> .model small ;.exe file, max 128 KB |
|||
.stack 1024 ;reserve 1 KB for the stack pointer. |
|||
.data |
|||
;no data needed |
|||
.code |
|||
start: |
|||
mov ax,0100h ;UNPACKED BCD "10" |
|||
mov cx,0Bh ;loop counter |
|||
repeat_countdown: |
|||
call PrintBCD_IgnoreLeadingZeroes |
|||
sub ax,1 |
|||
aas |
|||
;ascii adjust for subtraction, normally 0100h - 1 = 0FFh but this corrects it to 0009h |
|||
push ax |
|||
mov dl,13 |
|||
mov ah,02h |
|||
int 21h |
|||
mov dl,10 |
|||
mov ah,02h |
|||
int 21h |
|||
;these 6 lines of code are the "new line" function |
|||
pop ax |
|||
loop repeat_countdown ;decrement CX and jump back to the label "repeat_countdown" if CX != 0 |
|||
mov ax,4C00h |
|||
int 21h ;return to DOS |
|||
PrintBCD_IgnoreLeadingZeroes: |
|||
push ax |
|||
cmp ah,0 |
|||
jz skipLeadingZero |
|||
or ah,30h ;convert a binary-coded decimal quantity to an ASCII numeral |
|||
push dx |
|||
push ax |
|||
mov al,ah |
|||
mov ah,0Eh |
|||
int 10h ;prints AL to screen |
|||
pop ax |
|||
pop dx |
|||
skipLeadingZero: |
|||
or al,30h |
|||
push dx |
|||
push ax |
|||
mov ah,0Eh |
|||
int 10h |
|||
pop ax |
|||
pop dx |
|||
pop ax |
|||
ret |
|||
end start ;EOF</syntaxhighlight> |
|||
=={{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 */ |
|||
/*******************************************/ |
|||
/* Constantes file */ |
|||
/*******************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeConstantesARM64.inc" |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessResult: .asciz "Counter = @ \n" // message result |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: // entry of program |
|||
mov x4,#10 |
|||
1: // begin loop |
|||
mov x0,x4 |
|||
ldr x1,qAdrsZoneConv // display value |
|||
bl conversion10 // call decimal conversion |
|||
ldr x0,qAdrszMessResult |
|||
ldr x1,qAdrsZoneConv // display value |
|||
bl strInsertAtCharInc // insert result at @ character |
|||
bl affichageMess // display message |
|||
subs x4,x4,1 // decrement counter |
|||
bge 1b // loop if greather |
|||
100: // standard end of the program |
|||
mov x0,0 // return code |
|||
mov x8,EXIT // request to exit program |
|||
svc 0 // perform the system call |
|||
qAdrsZoneConv: .quad sZoneConv |
|||
qAdrszMessResult: .quad szMessResult |
|||
/********************************************************/ |
|||
/* File Include fonctions */ |
|||
/********************************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly */ |
|||
.include "../includeARM64.inc" |
|||
</syntaxhighlight> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<syntaxhighlight lang="ada">for I in reverse 0..10 loop |
|||
<ada> |
|||
for I in reverse 0..10 loop |
|||
Put_Line(Integer'Image(I)); |
Put_Line(Integer'Image(I)); |
||
end loop; |
end loop;</syntaxhighlight> |
||
</ada> |
|||
=={{header|Agena}}== |
|||
Tested with Agena 2.9.5 Win32 |
|||
<syntaxhighlight lang="agena">for i from 10 downto 0 do |
|||
print( i ) |
|||
od</syntaxhighlight> |
|||
=={{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}} |
|||
<syntaxhighlight lang="algol60">'BEGIN' 'COMMENT' Loops/Downward for - Algol60 - 23/06/2018; |
|||
'INTEGER' I; |
|||
'FOR' I := 10 'STEP' -1 'UNTIL' 0 'DO' |
|||
OUTINTEGER(1,I) |
|||
'END'</syntaxhighlight> |
|||
=={{header|ALGOL 68}}== |
|||
{{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|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
|||
<syntaxhighlight lang="algol68">FOR i FROM 10 BY -1 TO 0 DO |
|||
print((i,new line)) |
|||
OD</syntaxhighlight> |
|||
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]]. |
|||
<syntaxhighlight lang="algol68">FOR i FROM 10 DOWNTO 0 DO |
|||
print((i,new line)) |
|||
OD</syntaxhighlight> |
|||
=={{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}}== |
|||
<syntaxhighlight lang="algolw">begin |
|||
for i := 10 step -1 until 0 do |
|||
begin |
|||
write( i ) |
|||
end |
|||
end.</syntaxhighlight> |
|||
=={{header|Amazing Hopper}}== |
|||
<syntaxhighlight lang="amazing hopper"> |
|||
#include <flow.h> |
|||
DEF-MAIN |
|||
CLR-SCR |
|||
SET(i, 10) |
|||
LOOP(ciclo abajo) |
|||
PRNL(i) |
|||
BACK-IF-NOT-ZERO(i--, ciclo abajo) |
|||
END |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
</pre> |
|||
=={{header|AmigaE}}== |
|||
<syntaxhighlight lang="amigae">PROC main() |
|||
DEF i |
|||
FOR i := 10 TO 0 STEP -1 |
|||
WriteF('\d\n', i) |
|||
ENDFOR |
|||
ENDPROC</syntaxhighlight> |
|||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">repeat with i from 10 to 0 by -1 |
|||
log i |
|||
end repeat</syntaxhighlight> |
|||
=={{header|ARM Assembly}}== |
|||
{{works with|as|Raspberry Pi}} |
|||
<syntaxhighlight lang="arm assembly"> |
|||
/* ARM assembly Raspberry PI */ |
|||
/* program loopdownward.s */ |
|||
/* Constantes */ |
|||
.equ STDOUT, 1 @ Linux output console |
|||
.equ EXIT, 1 @ Linux syscall |
|||
.equ WRITE, 4 @ Linux syscall |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessResult: .ascii "Counter = " @ message result |
|||
sMessValeur: .fill 12, 1, ' ' |
|||
.asciz "\n" |
|||
/*********************************/ |
|||
/* UnInitialized data */ |
|||
/*********************************/ |
|||
.bss |
|||
/*********************************/ |
|||
/* code section */ |
|||
/*********************************/ |
|||
.text |
|||
.global main |
|||
main: @ entry of program |
|||
push {fp,lr} @ saves 2 registers |
|||
mov r4,#10 |
|||
1: @ begin loop |
|||
mov r0,r4 |
|||
ldr r1,iAdrsMessValeur @ display value |
|||
bl conversion10 @ call function with 2 parameter (r0,r1) |
|||
ldr r0,iAdrszMessResult |
|||
bl affichageMess @ display message |
|||
subs r4,#1 @ decrement counter |
|||
bge 1b @ loop if greather |
|||
100: @ standard end of the program |
|||
mov r0, #0 @ return code |
|||
pop {fp,lr} @restaur 2 registers |
|||
mov r7, #EXIT @ request to exit program |
|||
svc #0 @ perform the system call |
|||
iAdrsMessValeur: .int sMessValeur |
|||
iAdrszMessResult: .int szMessResult |
|||
/******************************************************************/ |
|||
/* display text with size calculation */ |
|||
/******************************************************************/ |
|||
/* r0 contains the address of the message */ |
|||
affichageMess: |
|||
push {r0,r1,r2,r7,lr} @ save registres |
|||
mov r2,#0 @ counter length |
|||
1: @ loop length calculation |
|||
ldrb r1,[r0,r2] @ read octet start position + index |
|||
cmp r1,#0 @ if 0 its over |
|||
addne r2,r2,#1 @ else add 1 in the length |
|||
bne 1b @ and loop |
|||
@ so here r2 contains the length of the message |
|||
mov r1,r0 @ address message in r1 |
|||
mov r0,#STDOUT @ code to write to the standard output Linux |
|||
mov r7, #WRITE @ code call system "write" |
|||
svc #0 @ call systeme |
|||
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */ |
|||
bx lr @ return |
|||
/******************************************************************/ |
|||
/* Converting a register to a decimal */ |
|||
/******************************************************************/ |
|||
/* r0 contains value and r1 address area */ |
|||
conversion10: |
|||
push {r1-r4,lr} @ save registers |
|||
mov r3,r1 |
|||
mov r2,#10 |
|||
1: @ start loop |
|||
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1 |
|||
add r1,#48 @ digit |
|||
strb r1,[r3,r2] @ store digit on area |
|||
sub r2,#1 @ previous position |
|||
cmp r0,#0 @ stop if quotient = 0 */ |
|||
bne 1b @ else loop |
|||
@ and move spaces in first on area |
|||
mov r1,#' ' @ space |
|||
2: |
|||
strb r1,[r3,r2] @ store space in area |
|||
subs r2,#1 @ @ previous position |
|||
bge 2b @ loop if r2 >= zéro |
|||
100: |
|||
pop {r1-r4,lr} @ restaur registres |
|||
bx lr @return |
|||
/***************************************************/ |
|||
/* division par 10 signé */ |
|||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/* |
|||
/* and http://www.hackersdelight.org/ */ |
|||
/***************************************************/ |
|||
/* r0 dividende */ |
|||
/* r0 quotient */ |
|||
/* r1 remainder */ |
|||
divisionpar10: |
|||
/* r0 contains the argument to be divided by 10 */ |
|||
push {r2-r4} /* save registers */ |
|||
mov r4,r0 |
|||
mov r3,#0x6667 @ r3 <- magic_number lower |
|||
movt r3,#0x6666 @ r3 <- magic_number upper |
|||
smull r1, r2, r3, r0 @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) |
|||
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */ |
|||
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */ |
|||
add r0, r2, r1 /* r0 <- r2 + r1 */ |
|||
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */ |
|||
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */ |
|||
pop {r2-r4} |
|||
bx lr /* leave function */ |
|||
</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">loop 10..0 'i [ |
|||
print i |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
=={{header|Asymptote}}== |
|||
Asymptote's control structures are similar to those in C, C++, or Java |
|||
<syntaxhighlight lang="asymptote">for(int i = 10; i >=0; --i) { |
|||
write(i); |
|||
}</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
|||
<syntaxhighlight lang="autohotkey">x := 10 |
|||
While (x >= 0) |
|||
{ |
|||
output .= "`n" . x |
|||
x-- |
|||
} |
|||
MsgBox % output |
|||
</syntaxhighlight> |
|||
=={{header|Avail}}== |
|||
<syntaxhighlight lang="avail">For each i from 10 to 0 by -1 do [Print: “i” ++ "\n";];</syntaxhighlight> |
|||
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}}== |
|||
<syntaxhighlight lang="awk">BEGIN { |
|||
for(i=10; i>=0; i--) { |
|||
print i |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|Axe}}== |
|||
Axe does not support for loops with step sizes other than 1. |
|||
<syntaxhighlight lang="axe">For(I,0,10) |
|||
Disp 10-I▶Dec,i |
|||
End</syntaxhighlight> |
|||
=={{header|Bait}}== |
|||
<syntaxhighlight lang="bait"> |
|||
fun main() { |
|||
for i := 10; i >= 0; i -= 1 { |
|||
println(i) |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|BASIC}}== |
|||
==={{header|Applesoft BASIC}}=== |
|||
<syntaxhighlight lang="applesoftbasic">FOR I = 10 TO 0 STEP -1 : PRINT I : NEXT I</syntaxhighlight> |
|||
==={{header|BaCon}}=== |
|||
<syntaxhighlight lang="freebasic">' Downward for |
|||
FOR i = 10 DOWNTO 0 : PRINT i : NEXT</syntaxhighlight> |
|||
==={{header|BASIC256}}=== |
|||
{{works with|QBasic}} |
|||
<syntaxhighlight lang="basic256">for i = 10 to 0 step -1 |
|||
print i; " "; |
|||
next i |
|||
print |
|||
end</syntaxhighlight> |
|||
==={{header|BBC BASIC}}=== |
|||
<syntaxhighlight lang="bbcbasic"> FOR i% = 10 TO 0 STEP -1 |
|||
PRINT i% |
|||
NEXT</syntaxhighlight> |
|||
==={{header|Commodore BASIC}}=== |
|||
<syntaxhighlight lang="basic">10 FOR I = 10 TO 0 STEP -1 |
|||
20 PRINT I |
|||
30 NEXT</syntaxhighlight> |
|||
==={{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}}=== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
For i As Integer = 10 To 0 Step -1 |
|||
Print i; " "; |
|||
Next |
|||
Print |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 10 9 8 7 6 5 4 3 2 1 0</pre> |
|||
==={{header|FutureBasic}}=== |
|||
<syntaxhighlight lang="futurebasic"> |
|||
window 1, @"Countdown", ( 0, 0, 400, 300 ) |
|||
NSInteger i |
|||
for i = 10 to 0 step -1 |
|||
print i |
|||
next |
|||
HandleEvents</syntaxhighlight> |
|||
Output: |
|||
<pre> 10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
==={{header|Gambas}}=== |
|||
'''[https://gambas-playground.proko.eu/?gist=b236db5bdb1087fa90e934a5a8210e1f Click this link to run this code]''' |
|||
<syntaxhighlight lang="gambas">Public Sub Main() |
|||
Dim siCount As Short |
|||
For siCount = 10 DownTo 0 |
|||
Print siCount;; |
|||
Next |
|||
End</syntaxhighlight> |
|||
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}}=== |
|||
<syntaxhighlight lang="is-basic">100 FOR I=10 TO 0 STEP-1 |
|||
110 PRINT I |
|||
120 NEXT</syntaxhighlight> |
|||
==={{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</syntaxhighlight> |
|||
==={{header|Microsoft Small Basic}}=== |
|||
<syntaxhighlight lang="microsoftsmallbasic"> |
|||
For i = 10 To 0 Step -1 |
|||
TextWindow.WriteLine(i) |
|||
EndFor</syntaxhighlight> |
|||
==={{header|MSX Basic}}=== |
|||
The [[#GW-BASIC|GW-BASIC]] solution works without any changes. |
|||
==={{header|NS-HUBASIC}}=== |
|||
<syntaxhighlight lang="ns-hubasic">10 FOR 1=10 TO 0 STEP -1 |
|||
20 PRINT I |
|||
30 NEXT</syntaxhighlight> |
|||
==={{header|PureBasic}}=== |
|||
<syntaxhighlight lang="purebasic">For i=10 To 0 Step -1 |
|||
Debug i |
|||
Next</syntaxhighlight> |
|||
==={{header|QB64}}=== |
|||
''CBTJD'': 2020/03/14 |
|||
<syntaxhighlight lang="qbasic">FOR n = 10 TO 0 STEP -1 |
|||
PRINT n |
|||
NEXT</syntaxhighlight> |
|||
==={{header|QuickBASIC}}=== |
|||
<syntaxhighlight lang="qbasic">for i = 10 to 0 step -1 |
|||
print i |
|||
next i</syntaxhighlight> |
|||
==={{header|Quite BASIC}}=== |
|||
The [[#GW-BASIC|GW-BASIC]] solution works without any changes. |
|||
==={{header|Run BASIC}}=== |
|||
{{works with|Just BASIC}} |
|||
{{works with|Liberty BASIC}} |
|||
<syntaxhighlight lang="lb">for i = 10 to 0 step -1 |
|||
print i |
|||
next i |
|||
end</syntaxhighlight> |
|||
==={{header|SmallBASIC}}=== |
|||
<syntaxhighlight lang="qbasic"> |
|||
for ii = 10 to 0 step -1 |
|||
print ii |
|||
next |
|||
</syntaxhighlight> |
|||
==={{header|TI-83 BASIC}}=== |
|||
<syntaxhighlight lang="ti83b">:For(I,10,0,-1 |
|||
:Disp I |
|||
:End</syntaxhighlight> |
|||
==={{header|TI-89 BASIC}}=== |
|||
<syntaxhighlight lang="ti89b">Local i |
|||
For i, 10, 0, –1 |
|||
Disp i |
|||
EndFor</syntaxhighlight> |
|||
==={{header|True BASIC}}=== |
|||
{{works with|QBasic}} |
|||
<syntaxhighlight lang="basic">FOR i = 10 TO 0 STEP -1 |
|||
PRINT i; " "; |
|||
NEXT i |
|||
PRINT |
|||
END</syntaxhighlight> |
|||
==={{Header|Tiny BASIC}}=== |
|||
{{works with|TinyBasic}} |
|||
<syntaxhighlight lang="basic">10 REM Loops/Downward for |
|||
20 LET I = 10 |
|||
30 IF I = -1 THEN END |
|||
40 PRINT I |
|||
50 LET I = I - 1 |
|||
60 GOTO 30 |
|||
70 END</syntaxhighlight> |
|||
==={{header|VBA}}=== |
|||
<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}} |
|||
<syntaxhighlight lang="xbasic">PROGRAM "downwardfor" |
|||
DECLARE FUNCTION Entry() |
|||
FUNCTION Entry() |
|||
FOR i% = 10 TO 0 STEP -1 |
|||
PRINT i% |
|||
NEXT i% |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
==={{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}}=== |
|||
<syntaxhighlight lang="zxbasic">10 FOR l = 10 TO 0 STEP -1 |
|||
20 PRINT l |
|||
30 NEXT l</syntaxhighlight> |
|||
=={{header|Batch File}}== |
|||
<syntaxhighlight lang="dos">@echo off |
|||
for /l %%D in (10,-1,0) do echo %%D</syntaxhighlight> |
|||
=={{header|bc}}== |
|||
<syntaxhighlight lang="bc">for (i = 10; i >= 0; i--) i |
|||
quit</syntaxhighlight> |
|||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
<syntaxhighlight lang="befunge">55+>:.:v |
|||
55+>:.:v |
|||
@ ^ -1_</syntaxhighlight> |
|||
=={{header|BQN}}== |
|||
Each (<code>¨</code>) is an operator in BQN that helps with emulating loops like for and foreach. |
|||
<syntaxhighlight lang="bqn">•Show¨⌽↕11</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
|||
<syntaxhighlight lang="bracmat"> 10:?i |
|||
& whl'(out$!i&!i+-1:~<0:?i)</syntaxhighlight> |
|||
=={{header|Brainf***}}== |
|||
<syntaxhighlight lang="brainf***">>++++++++[-<++++++>] //cell 0 now contains 48 the ASCII code for "0" |
|||
<+.-. //print the digits 1 and 0 |
|||
>++++++++++. //cell 1 now contains the carriage return; print it! |
|||
>+++++++++ //cell 2 now contains the number 9; this is our counter |
|||
<<+++++++++>> //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</syntaxhighlight> |
|||
=={{header|Brat}}== |
|||
<syntaxhighlight lang="brat">10.to 0 { n | p n }</syntaxhighlight> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang="c">int i; |
|||
<c> |
|||
int i; |
|||
for(i = 10; i >= 0; --i) |
for(i = 10; i >= 0; --i) |
||
printf("%d\n",i); |
printf("%d\n",i);</syntaxhighlight> |
||
</c> |
|||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp">for (int i = 10; i >= 0; i--) |
|||
{ |
|||
Console.WriteLine(i); |
|||
}</syntaxhighlight> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
<syntaxhighlight lang="cpp">for(int i = 10; i >= 0; --i) |
|||
<cpp> |
|||
std::cout << i << "\n";</syntaxhighlight> |
|||
for(int i = 10; i >= 0; --i) |
|||
std::cout << i << "\n"; |
|||
=={{header|C3}}== |
|||
</cpp> |
|||
<syntaxhighlight lang="c3">for (int i = 10; i >= 0; i--) |
|||
{ |
|||
io::printn(i); |
|||
}</syntaxhighlight> |
|||
=={{header|Ceylon}}== |
|||
<syntaxhighlight lang="ceylon">for (i in 10..0) { |
|||
print(i); |
|||
}</syntaxhighlight> |
|||
=={{header|Chapel}}== |
|||
<syntaxhighlight lang="chapel">for i in 1..10 by -1 do |
|||
writeln(i);</syntaxhighlight> |
|||
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 |
|||
<syntaxhighlight lang="chapel">var r = 1..10; |
|||
for i in r by -1 do { ... }</syntaxhighlight> |
|||
=={{header|Clipper}}== |
|||
<syntaxhighlight lang="clipper"> FOR i := 10 TO 0 STEP -1 |
|||
? i |
|||
NEXT</syntaxhighlight> |
|||
=={{header|Clojure}}== |
|||
<syntaxhighlight lang="csharp">(doseq [x (range 10 -1 -1)] (println x))</syntaxhighlight> |
|||
=={{header|COBOL}}== |
|||
free-form |
|||
<syntaxhighlight lang="cobol">identification division. |
|||
program-id. countdown. |
|||
environment division. |
|||
data division. |
|||
working-storage section. |
|||
01 counter pic 99. |
|||
88 counter-done value 0. |
|||
01 counter-disp pic Z9. |
|||
procedure division. |
|||
perform with test after varying counter from 10 by -1 until counter-done |
|||
move counter to counter-disp |
|||
display counter-disp |
|||
end-perform |
|||
stop run.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
=={{header|CoffeeScript}}== |
|||
This could be written either in the array comprehension style, |
|||
or in "regular" for loop style. |
|||
<syntaxhighlight lang="coffeescript"># The more compact "array comprehension" style |
|||
console.log i for i in [10..0] |
|||
# The "regular" for loop style. |
|||
for i in [10..0] |
|||
console.log i |
|||
# More compact version of the above |
|||
for i in [10..0] then console.log i</syntaxhighlight> |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
(the output is repeated three times; once for each loop) |
|||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
With tags: |
With tags: |
||
<syntaxhighlight lang="cfm"><cfloop index = "i" from = "10" to = "0" step = "-1"> |
|||
#i# |
|||
</cfloop></syntaxhighlight> |
|||
With script: |
With script: |
||
<syntaxhighlight lang="cfm"><cfscript> |
|||
for( i = 10; i <= 0; i-- ) |
|||
{ |
|||
writeOutput( i ); |
|||
} |
|||
</cfscript></syntaxhighlight> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lisp>(loop for i from 10 downto 1 do |
<syntaxhighlight lang="lisp">(loop for i from 10 downto 1 do |
||
(print i))</ |
(print i))</syntaxhighlight> |
||
=== Using DO === |
|||
<syntaxhighlight lang="lisp"> |
|||
(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 === |
|||
<syntaxhighlight lang="lisp"> |
|||
(let ((count 10)) ; Create local variable count = 10 |
|||
(tagbody |
|||
dec ; Create tag dec |
|||
(print count) ; Prints count |
|||
(decf count) ; Decreases count |
|||
(if (not (< count 0)) ; Ends loop when negative |
|||
(go dec)))) ; Loops back to tag dec |
|||
</syntaxhighlight> |
|||
=== Using Recursion === |
|||
<syntaxhighlight lang="lisp"> |
|||
(defun down-to-0 (count) |
|||
(print count) |
|||
(if (not (zerop count)) |
|||
(down-to-0 (1- count)))) |
|||
(down-to-0 10) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
</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}}== |
|||
<syntaxhighlight lang="crystal">10.step(to: 0, by: -1).each { |i| |
|||
puts i |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
<syntaxhighlight lang="d">import std.stdio: writeln; |
|||
<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 |
|||
<d>foreach_reverse(i ; 0..10+1) writefln(i) ;</d> |
|||
void main() { |
|||
=={{header|dc}}== |
|||
for (int i = 10; i >= 0; --i) |
|||
writeln(i); |
|||
writeln(); |
|||
foreach_reverse (i ; 0 .. 10 + 1) |
|||
writeln(i); |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
=={{header|Dart}}== |
|||
<syntaxhighlight lang="dart">void main() { |
|||
for (var i = 10; i >= 0; --i) { |
|||
print(i); |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|dc}}== |
|||
does not use GNU extensions |
does not use GNU extensions |
||
Line 62: | Line 1,087: | ||
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. |
||
<syntaxhighlight lang="dc">c |
|||
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</syntaxhighlight> |
|||
Using it |
|||
<syntaxhighlight lang="dc">|dc < ./for.dc |
|||
10 |
|||
9 |
|||
... |
|||
0</syntaxhighlight> |
|||
=={{header|Delphi}}== |
|||
:''See [[#Pascal|Pascal]]'' |
|||
=={{header|Draco}}== |
|||
<syntaxhighlight lang="draco">proc nonrec main() void: |
|||
byte i; |
|||
for i from 10 downto 0 do |
|||
write(i," ") |
|||
od |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 9 8 7 6 5 4 3 2 1 0</pre> |
|||
=={{header|DWScript}}== |
|||
<syntaxhighlight lang="pascal">for i := 10 downto 0 do |
|||
PrintLn(i);</syntaxhighlight> |
|||
=={{header|E}}== |
|||
<syntaxhighlight lang="e">for i in (0..10).descending() { println(i) }</syntaxhighlight> |
|||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="text">for i = 10 downto 0 |
|||
print i |
|||
.</syntaxhighlight> |
|||
=={{header|EchoLisp}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
(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. |
|||
<syntaxhighlight lang="edsac">[ Loop with downward counter |
|||
========================== |
|||
A program for the EDSAC |
|||
Prints the integers 10 down to 0 |
|||
The counter is stored at address 20@ |
|||
Its initial value is 9 * 2^12 |
|||
(9 in the high 5 bits, representing |
|||
the character '9') and it counts |
|||
down in steps of 2^12 |
|||
Works with Initial Orders 2 ] |
|||
T56K [ set load point ] |
|||
GK [ set base address ] |
|||
[ orders ] |
|||
O14@ [ print figure shift ] |
|||
O15@ [ print '1' ] |
|||
O16@ [ print '0' ] |
|||
O17@ [ print CR ] |
|||
O18@ [ print LF ] |
|||
[ 5 ] O20@ [ print c ] |
|||
O17@ [ print CR ] |
|||
O18@ [ print LF ] |
|||
T19@ [ acc := 0 ] |
|||
A20@ [ acc += c ] |
|||
S15@ [ acc -:= character '1' ] |
|||
U20@ [ c := acc ] |
|||
E5@ [ branch on non-negative ] |
|||
ZF [ stop ] |
|||
[ constants ] |
|||
[ 14 ] #F [ πF -- figure shift ] |
|||
[ 15 ] QF [ character '1' ] |
|||
[ 16 ] PF [ character '0' ] |
|||
[ 17 ] @F [ θF -- CR ] |
|||
[ 18 ] &F [ ΔF -- LF ] |
|||
[ variables ] |
|||
[ 19 ] P0F [ used to clear acc ] |
|||
[ 20 ] OF [ character c = '9' ] |
|||
EZPF [ start when loaded ]</syntaxhighlight> |
|||
=={{header|EGL}}== |
|||
<syntaxhighlight lang="egl">for ( i int from 10 to 0 decrement by 1 ) |
|||
SysLib.writeStdout( i ); |
|||
end</syntaxhighlight> |
|||
=={{header|Ela}}== |
|||
===Standard Approach=== |
|||
<syntaxhighlight lang="ela">open monad io |
|||
each [] = do return () |
|||
each (x::xs) = do |
|||
putStrLn $ show x |
|||
each xs |
|||
each [10,9..0] ::: IO</syntaxhighlight> |
|||
===Alternative Approach=== |
|||
<syntaxhighlight lang="ela">open monad io |
|||
countDown m n | n < m = do return () |
|||
| else = do |
|||
putStrLn $ show n |
|||
countDown m (n - 1) |
|||
_ = countDown 0 10 ::: IO</syntaxhighlight> |
|||
=={{header|Elixir}}== |
|||
<syntaxhighlight lang="elixir">iex(1)> Enum.each(10..0, fn i -> IO.puts i end) |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
:ok</syntaxhighlight> |
|||
=={{header|Erlang}}== |
|||
<syntaxhighlight lang="erlang">%% Implemented by Arjun Sunel |
|||
-module(downward_loop). |
|||
-export([main/0]). |
|||
main() -> |
|||
[macro s(swap) - (a b : b a)]s. |
|||
for_loop(10). |
|||
[Sa Sb La Lb] ss |
|||
for_loop(N) -> |
|||
[macro d(2dup) - (a b : a b a b)]s. |
|||
if N > 0 -> |
|||
[Sa d Sb La d Lb lsx] sd |
|||
io:format("~p~n",[N] ), |
|||
for_loop(N-1); |
|||
[macro m(for) - ]s. |
|||
true -> |
|||
[lfx 1 - ldx !<m ] sm |
|||
io:format("~p~n",[N]) |
|||
end. |
|||
0 10 ldx [p] sf !<m |
|||
</syntaxhighlight> |
|||
q |
|||
{{out}} |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
ok</pre> |
|||
=={{header|ERRE}}== |
|||
Using it |
|||
<syntaxhighlight lang="erre"> |
|||
|dc < ./for.dc |
|||
FOR I%=10 TO 0 STEP -1 DO |
|||
10 |
|||
PRINT(I%) |
|||
9 |
|||
END FOR |
|||
... |
|||
</syntaxhighlight> |
|||
0 |
|||
=={{header|Euphoria}}== |
|||
<syntaxhighlight lang="euphoria">for i = 10 to 0 by -1 do |
|||
? i |
|||
end for</syntaxhighlight> |
|||
=={{header|F_Sharp|F#}}== |
|||
Using an enumerable expression: |
|||
<syntaxhighlight lang="fsharp">for i in 10..-1..0 do |
|||
printfn "%d" i</syntaxhighlight> |
|||
Using the 'downto' keyword: |
|||
<syntaxhighlight lang="fsharp">for i = 10 downto 0 do |
|||
printfn "%d" i</syntaxhighlight> |
|||
=={{header|Factor}}== |
|||
<syntaxhighlight lang="factor">11 <iota> <reversed> [ . ] each</syntaxhighlight> |
|||
=={{header|FALSE}}== |
|||
<syntaxhighlight lang="false">10[$0>][$." "1-]#.</syntaxhighlight> |
|||
=={{header|Fantom}}== |
|||
<syntaxhighlight lang="fantom"> |
|||
class DownwardFor |
|||
{ |
|||
public static Void main () |
|||
{ |
|||
for (Int i := 10; i >= 0; i--) |
|||
{ |
|||
echo (i) |
|||
} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Fermat}}== |
|||
<syntaxhighlight lang="fermat">for i = 10 to 0 by -1 do !!i; od</syntaxhighlight> |
|||
=={{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. |
||
<syntaxhighlight lang="forth">: loop-down 0 10 do i . -1 +loop ;</syntaxhighlight> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{Works with|Fortran|90 and later}} |
{{Works with|Fortran|90 and later}} |
||
<syntaxhighlight lang="fortran">DO i = 10, 0, -1 |
|||
WRITE(*, *) i |
|||
END DO</syntaxhighlight> |
|||
{{works with|Fortran|77 and later}} |
|||
<syntaxhighlight lang="fortran"> PROGRAM DOWNWARDFOR |
|||
C Initialize the loop parameters. |
|||
INTEGER I, START, FINISH, STEP |
|||
PARAMETER (START = 10, FINISH = 0, STEP = -1) |
|||
C If you were to leave off STEP, it would default to positive one. |
|||
DO 10 I = START, FINISH, STEP |
|||
WRITE (*,*) I |
|||
10 CONTINUE |
|||
STOP |
|||
END</syntaxhighlight> |
|||
=={{header|Frink}}== |
|||
<syntaxhighlight lang="frink"> |
|||
for i = 10 to 0 step -1 |
|||
println[i] |
|||
</syntaxhighlight> |
|||
=={{header|GAP}}== |
|||
<syntaxhighlight lang="gap">for i in [10, 9 .. 0] do |
|||
Print(i, "\n"); |
|||
od;</syntaxhighlight> |
|||
=={{header|GML}}== |
|||
<syntaxhighlight lang="gml">for(i = 10; i >= 0; i -= 1) |
|||
show_message(string(i))</syntaxhighlight> |
|||
=={{header|Go}}== |
|||
<syntaxhighlight lang="go">for i := 10; i >= 0; i-- { |
|||
fmt.Println(i) |
|||
}</syntaxhighlight> |
|||
::<syntaxhighlight lang="go">package main |
|||
import "fmt" |
|||
import "time" |
|||
func main() { |
|||
i := 10 |
|||
for i > 0 { |
|||
fmt.Println(i) |
|||
time.Sleep(time.Second) |
|||
i = i - 1 |
|||
} |
|||
fmt.Println("blast off") |
|||
}</syntaxhighlight> |
|||
=={{header|Groovy}}== |
|||
<syntaxhighlight lang="groovy">for (i in (10..0)) { |
|||
println i |
|||
}</syntaxhighlight> |
|||
=={{header|Harbour}}== |
|||
<syntaxhighlight lang="visualfoxpro">FOR i := 10 TO 0 STEP -1 |
|||
? i |
|||
NEXT</syntaxhighlight> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang="haskell">import Control.Monad |
|||
forM_ [10,9..0] print |
|||
main :: IO () |
|||
main = forM_ [10,9 .. 0] print</syntaxhighlight> |
|||
=={{header|Haxe}}== |
|||
Haxe lacks a downward for-loop, but it is easy to create an iterator to serve that purpose. |
|||
<syntaxhighlight lang="haxe">class Step { |
|||
var end:Int; |
|||
var step:Int; |
|||
var index:Int; |
|||
public inline function new(start:Int, end:Int, step:Int) { |
|||
this.index = start; |
|||
this.end = end; |
|||
this.step = step; |
|||
} |
|||
public inline function hasNext() return step > 0 ? end >= index : index >= end; |
|||
public inline function next() return (index += step) - step; |
|||
} |
|||
class Main { |
|||
static function main() { |
|||
for (i in new Step(10, 0, -1)) { |
|||
Sys.print('$i '); |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 9 8 7 6 5 4 3 2 1 0</pre> |
|||
=={{header|hexiscript}}== |
|||
<syntaxhighlight lang="hexiscript">for let i 10; i >= 0; i-- |
|||
println i |
|||
endfor</syntaxhighlight> |
|||
=={{header|HicEst}}== |
|||
<syntaxhighlight lang="hicest">DO i = 10, 0, -1 |
|||
WRITE() i |
|||
ENDDO</syntaxhighlight> |
|||
=={{header|HolyC}}== |
|||
<syntaxhighlight lang="holyc">I8 i; |
|||
for (i = 10; i >= 0; --i) |
|||
Print("%d\n", i);</syntaxhighlight> |
|||
=={{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'. |
|||
<syntaxhighlight lang="icon">every i := 10 to 0 by -1 do { |
|||
# things to do within the loop |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|IDL}}== |
=={{header|IDL}}== |
||
Line 101: | Line 1,448: | ||
Using a loop (with an "increment of minus one" ): |
Using a loop (with an "increment of minus one" ): |
||
<syntaxhighlight lang="idl">for i=10,0,-1 do print,i</syntaxhighlight> |
|||
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 107: | Line 1,454: | ||
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: |
||
<syntaxhighlight lang="idl">print,10-indgen(11)</syntaxhighlight> |
|||
=={{header|Inform 6}}== |
|||
<syntaxhighlight lang="inform 6">for(i = 10: i >= 0: i--) |
|||
print i, "^";</syntaxhighlight> |
|||
=={{header|Io}}== |
|||
<syntaxhighlight lang="io">for(i,10,0,-1, |
|||
i println |
|||
)</syntaxhighlight> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way: |
||
,. i. -11 |
<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). |
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). |
||
<syntaxhighlight lang="j">3 : 0 ] 11 |
|||
<pre> |
|||
for_i. i. - y do. |
|||
smoutput i |
|||
end. |
|||
i 1!:2 ]2 |
|||
)</syntaxhighlight> |
|||
end. |
|||
i.0 0 |
|||
) |
|||
</pre> |
|||
Though it's rare to see J code like this. |
Though it's rare to see J code like this. |
||
That said, a convenient routine for generating intervals in J might be: |
|||
<syntaxhighlight lang="j">thru=: <. + i.@(+*)@-~</syntaxhighlight> |
|||
For example: |
|||
<syntaxhighlight lang="j"> 10 thru 0 |
|||
10 9 8 7 6 5 4 3 2 1 0</syntaxhighlight> |
|||
(or <code>,.10 thru 0</code> if you want each number on a line by itself) |
|||
This verb "thru" will count up or down, starting and stop at the indicated left and right ending points. (However, note that this particular implementation of 'thru' will return an empty result if the starting and ending values are the same. Whether that's useful or a problem, depends on the application.) |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
<syntaxhighlight lang="java"> |
|||
<java>for(i = 10; i >= 0; --i){ |
|||
for (int i = 10; i >= 0; i--) { |
|||
System.out.println(i); |
|||
System.out.println(i); |
|||
}</java> |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<javascript>for (var i=10; i>=0; --i) print(i);</ |
<syntaxhighlight lang="javascript">for (var i=10; i>=0; --i) print(i);</syntaxhighlight> |
||
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: |
|||
<syntaxhighlight lang="javascript">for (var i = 11; i--;) console.log(i);</syntaxhighlight> |
|||
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. |
|||
<syntaxhighlight lang="javascript">var i = 11; |
|||
while (i--) console.log(i);</syntaxhighlight> |
|||
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. |
|||
If we have grown over-attached to the English morpheme 'for', we might think first of turning to '''Array.forEach()''', and write something like: |
|||
<syntaxhighlight lang="javascript">function range(m, n) { |
|||
return Array.apply(null, Array(n - m + 1)).map( |
|||
function (x, i) { |
|||
return m + i; |
|||
} |
|||
); |
|||
} |
|||
range(0, 10).reverse().forEach( |
|||
function (x) { |
|||
console.log(x); |
|||
} |
|||
);</syntaxhighlight> |
|||
but this is still a statement with side-effects, rather than a composable expression with a value. |
|||
We can get an expression (assuming that the range() function (above) is defined) but replacing Array.forEach with '''Array.map()''' |
|||
<syntaxhighlight lang="javascript">console.log( |
|||
range(0, 10).reverse().map( |
|||
function (x) { |
|||
return x; |
|||
} |
|||
).join('\n') |
|||
);</syntaxhighlight> |
|||
but in this case, we are simply mapping an identity function over the values, so the expression simplifies down to: |
|||
<syntaxhighlight lang="javascript">console.log( |
|||
range(0, 10).reverse().join('\n') |
|||
);</syntaxhighlight> |
|||
=={{header|jq}}== |
|||
If range/3 is available in your jq: |
|||
<syntaxhighlight lang="jq">range(10;-1;-1)</syntaxhighlight> |
|||
Otherwise: |
|||
range(-10;1) | -. |
|||
=={{header|Julia}}== |
|||
<syntaxhighlight lang="julia">for i in 10:-1:0 |
|||
println(i) |
|||
end</syntaxhighlight> |
|||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang="scala"> |
|||
// version 1.3.61 |
|||
fun main() { |
|||
(10 downTo 0).forEach { println(it) } |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Lambdatalk}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
{def downward_for |
|||
{lambda {:i} |
|||
{if {< :i 0} |
|||
then (end of loop) |
|||
else :i {downward_for {- :i 1}}}}} |
|||
-> downward_for |
|||
{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="langur"> |
|||
for i = 10; i > -1; i -= 1 { |
|||
writeln i |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Lasso}}== |
|||
<syntaxhighlight lang="lasso">loop(-from=10, -to=0, -by=-1) => {^ loop_count + ' ' ^}</syntaxhighlight> |
|||
=={{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. |
|||
<syntaxhighlight lang="logo">for "i [10 0] [print :i]</syntaxhighlight> |
|||
=={{header|Lingo}}== |
|||
<syntaxhighlight lang="lingo">repeat with i = 10 down to 0 |
|||
put i |
|||
end repeat</syntaxhighlight> |
|||
=={{header|Lisaac}}== |
|||
<syntaxhighlight lang="lisaac">10.downto 0 do { i : INTEGER; |
|||
i.println; |
|||
};</syntaxhighlight> |
|||
=={{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 |
|||
<syntaxhighlight lang="livecode">local x=10 |
|||
repeat for 10 times |
|||
put x & return |
|||
add -1 to x |
|||
end repeat</syntaxhighlight> |
|||
A more idiomatic approach using "with" variant of repeat which does have a "down to" form |
|||
<syntaxhighlight lang="livecode">repeat with n=10 down to 1 |
|||
put n |
|||
end repeat</syntaxhighlight> |
|||
=={{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. |
||
<syntaxhighlight lang="logo">for [i 10 0] [print :i]</syntaxhighlight> |
|||
=={{header|Lua}}== |
|||
<syntaxhighlight lang="lua"> |
|||
for i=10,0,-1 do |
|||
print(i) |
|||
end |
|||
</syntaxhighlight> |
|||
=={{header|M2000 Interpreter}}== |
|||
M2000 can operate a For like in BASIC or Like M2000. In M2000 mode, a For always execute at least one time the block inside. This FOR use absolute value of step, except when we have start value and end value the same value, so from sign of step, interpreter calculate the exit value. |
|||
We can change the iterator variable of a For, but this variable is a copy of actual iterator, and next step get the proper value. So we can't change the numbers of steps, but we can use continue to skip rest of code and execute next step, or exit to exit block and stop loop. Also we can use Goto to stop loop and continue from a label. |
|||
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. |
|||
This is not an error (each for has private counter value): |
|||
for i=1 to 10 :for i=1 to 2:Print i:Next i:Next i |
|||
We get 10 times two values: 1 2 |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Form 80, 50 |
|||
Module Checkit { |
|||
set switches "+For" |
|||
For i=10 to 1 step -1 { |
|||
Print i |
|||
} |
|||
Print i=0 |
|||
\\ this For switch make it like For in BASIC |
|||
\\ block skipped |
|||
For i=1 to 10 step -1 { |
|||
Print i |
|||
} |
|||
print i=1 |
|||
\\ but this is the default behavior |
|||
\\ |
|||
set switches "-For" |
|||
\\ sign of step used when start is same as end to calculate the exit value of i |
|||
\\ This is the standard, and a For always execute at least one time the block. |
|||
\\ use absulute step_Value. Because 10>1 direction is downward. |
|||
For i=10 to 1 step -1 { |
|||
Print i |
|||
} |
|||
Print i=0 |
|||
\\ loop from 1 to 10, using abs(step_value) |
|||
For i=1 to 10 step -1 { |
|||
Print i |
|||
} |
|||
print i=11 |
|||
For i=1 to 1 step -1 { |
|||
Print i |
|||
} |
|||
Print i=0 |
|||
} |
|||
CheckIt |
|||
</syntaxhighlight> |
|||
=={{header|M4}}== |
|||
<syntaxhighlight 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 |
|||
')</syntaxhighlight> |
|||
=={{header|Maple}}== |
|||
Using an explicit loop: |
|||
<syntaxhighlight lang="maple">for i from 10 to 0 by -1 do print(i) end:</syntaxhighlight> |
|||
Pushing the loop into the kernel: |
|||
<syntaxhighlight lang="maple">seq(print(i),i=10..0,-1)</syntaxhighlight> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
|||
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: |
|||
<syntaxhighlight lang="mathematica">For[i = 10, i >= 0, i--, Print[i]]</syntaxhighlight> |
|||
Using Do: |
|||
<syntaxhighlight lang="mathematica">Do[Print[i], {i, 10, 0, -1}]</syntaxhighlight> |
|||
Using Scan: |
|||
<syntaxhighlight lang="mathematica">Scan[Print, Range[10, 0, -1]]</syntaxhighlight> |
|||
=={{header|MATLAB}} / {{header|Octave}}== |
|||
<syntaxhighlight lang="matlab"> for k = 10:-1:0, |
|||
printf('%d\n',k) |
|||
end; </syntaxhighlight> |
|||
A vectorized version of the code is |
|||
<syntaxhighlight lang="matlab"> printf('%d\n',10:-1:0); </syntaxhighlight> |
|||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima">for i from 10 thru 0 step -1 do print(i);</syntaxhighlight> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<syntaxhighlight lang="maxscript">for i in 10 to 0 by -1 do print i</syntaxhighlight> |
|||
<pre> |
|||
for i in 10 to 0 by -1 do print i |
|||
=={{header|Mercury}}== |
|||
<syntaxhighlight lang="text">:- module loops_downward_for. |
|||
:- interface. |
|||
:- import_module io. |
|||
:- pred main(io::di, io::uo) is det. |
|||
:- implementation. |
|||
:- import_module int. |
|||
main(!IO) :- |
|||
Print = (pred(I::in, !.IO::di, !:IO::uo) is det :- |
|||
io.write_int(I, !IO), io.nl(!IO) |
|||
), |
|||
int.fold_down(Print, 1, 10, !IO).</syntaxhighlight> |
|||
=={{header|Metafont}}== |
|||
<syntaxhighlight lang="metafont">for i = 10 step -1 until 0: show i; endfor |
|||
end</syntaxhighlight> |
|||
The basic set of macros for Metafont defines <tt>downto</tt>, so that we can write |
|||
<syntaxhighlight lang="metafont">for i = 10 downto 0: show i; endfor end</syntaxhighlight> |
|||
=={{header|min}}== |
|||
{{works with|min|0.19.6}} |
|||
<syntaxhighlight lang="min">10 :n (n 0 >=) (n puts! n pred @n) while</syntaxhighlight> |
|||
Or |
|||
<syntaxhighlight lang="min">10 (dup 0 <) 'pop (puts pred) () linrec</syntaxhighlight> |
|||
=={{header|MiniScript}}== |
|||
<syntaxhighlight lang="miniscript">for i in range(10, 0) |
|||
print i |
|||
end for</syntaxhighlight> |
|||
=={{header|МК-61/52}}== |
|||
<syntaxhighlight lang="text">1 0 П0 ИП0 L0 03 С/П</syntaxhighlight> |
|||
=={{header|Modula-2}}== |
|||
<syntaxhighlight lang="modula2">MODULE Downward; |
|||
IMPORT InOut; |
|||
VAR |
|||
i: INTEGER; |
|||
BEGIN |
|||
FOR i := 10 TO 0 BY -1 DO |
|||
InOut.WriteInt(i, 2); |
|||
InOut.WriteLn |
|||
END |
|||
END Downward.</syntaxhighlight> |
|||
=={{header|Modula-3}}== |
|||
<syntaxhighlight lang="modula3">FOR i := 10 TO 0 BY -1 DO |
|||
IO.PutInt(i); |
|||
END;</syntaxhighlight> |
|||
=={{header|MUMPS}}== |
|||
<syntaxhighlight lang="mumps">LOOPDOWN |
|||
NEW I FOR I=10:-1:1 WRITE I WRITE:I'=1 ", " |
|||
KILL I QUIT</syntaxhighlight> |
|||
=={{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> |
</pre> |
||
=={{header|Nemerle}}== |
|||
<syntaxhighlight lang="nemerle">for (i = 10; i >= 0; i--) {WriteLine($"$i")}</syntaxhighlight> |
|||
<syntaxhighlight lang="nemerle">foreach (i in [10, 9 .. 0]) {WriteLine($"$i")}</syntaxhighlight> |
|||
=={{header|NetRexx}}== |
|||
<syntaxhighlight lang="netrexx">/* NetRexx */ |
|||
options replace format comments java crossref savelog symbols nobinary |
|||
say |
|||
say 'Loops/Downward for' |
|||
loop i_ = 10 to 0 by -1 |
|||
say i_.right(2) |
|||
end i_ |
|||
</syntaxhighlight> |
|||
=={{header|NewLISP}}== |
|||
<syntaxhighlight lang="newlisp">(for (i 10 0) |
|||
(println i))</syntaxhighlight> |
|||
=={{header|Nim}}== |
|||
<syntaxhighlight lang="nim">for x in countdown(10,0): echo(x)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0</pre> |
|||
=={{header|Nu}}== |
|||
<syntaxhighlight lang="nu"> |
|||
for i in 10..0 {print $i} |
|||
</syntaxhighlight> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
<syntaxhighlight lang="oberon2">FOR i := 10 TO 0 BY -1 DO |
|||
<pre> |
|||
FOR i := 10 TO 0 BY -1 DO |
|||
Out.Int(i,0); |
Out.Int(i,0); |
||
END;</syntaxhighlight> |
|||
END; |
|||
</pre> |
|||
=={{header|Objeck}}== |
|||
<syntaxhighlight lang="objeck"> |
|||
for(i := 10; i >= 0; i--;) { |
|||
i->PrintLine(); |
|||
}; |
|||
</syntaxhighlight> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
<ocaml>for i = 10 downto 0 do |
<syntaxhighlight lang="ocaml">for i = 10 downto 0 do |
||
Printf.printf "%d\n" i |
Printf.printf "%d\n" i |
||
done</ |
done</syntaxhighlight> |
||
=={{header|Octave}}== |
|||
<syntaxhighlight lang="octave">for i = 10:-1:0 |
|||
% ... |
|||
endfor</syntaxhighlight> |
|||
=={{header|Oforth}}== |
|||
<syntaxhighlight lang="oforth">10 0 -1 step: i [ i println ]</syntaxhighlight> |
|||
=={{header|Oz}}== |
|||
<syntaxhighlight lang="oz">for I in 10..0;~1 do |
|||
{Show I} |
|||
end</syntaxhighlight> |
|||
=={{header|PARI/GP}}== |
|||
<syntaxhighlight lang="parigp">forstep(n=10,0,-1,print(n))</syntaxhighlight> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
<syntaxhighlight lang="pascal">for i := 10 downto 0 do |
|||
<pascal> |
|||
writeln(i);</syntaxhighlight> |
|||
for i := 10 downto 0 do |
|||
writeln(i); |
|||
=={{header|PascalABC.NET}}== |
|||
</pascal> |
|||
<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 |
|||
<syntaxhighlight lang="sgml"><@ ITEFORLITLITLITLIT>0|<@ SAYVALFOR>...</@>|10|-1</@></syntaxhighlight> |
|||
Simplified Chinese variable-length opcodes |
|||
<syntaxhighlight lang="sgml"><# 迭代迭代次数字串字串字串字串>0|<# 显示值迭代次数>...</#>|10|-1</#></syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<perl>foreach (reverse 0..10) { |
<syntaxhighlight lang="perl">foreach (reverse 0..10) { |
||
print "$_\n"; |
print "$_\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
|||
{{libheader|Phix/basics}} |
|||
<!--<syntaxhighlight lang="phix">--> |
|||
<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> |
|||
<!--</syntaxhighlight>--> |
|||
=={{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}}== |
=={{header|PHP}}== |
||
<php>for ($i = 10; $i >= 0; $i--) |
<syntaxhighlight lang="php">for ($i = 10; $i >= 0; $i--) |
||
echo "$i\n";</ |
echo "$i\n";</syntaxhighlight> |
||
or |
or |
||
<php>foreach (range(10, 0) as $i) |
<syntaxhighlight lang="php">foreach (range(10, 0) as $i) |
||
echo "$i\n";</ |
echo "$i\n";</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
|||
<syntaxhighlight lang="picolisp">(for (I 10 (ge0 I) (dec I)) |
|||
(println I) )</syntaxhighlight> |
|||
or: |
|||
<syntaxhighlight lang="picolisp">(mapc println (range 10 0))</syntaxhighlight> |
|||
=={{header|Pike}}== |
|||
<syntaxhighlight lang="pike">int main(){ |
|||
for(int i = 10; i >= 0; i--){ |
|||
write(i + "\n"); |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{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: |
|||
<syntaxhighlight lang="plainenglish">To run: |
|||
Start up. |
|||
Put 11 into a counter. |
|||
Loop. |
|||
If the counter is below 0, break. |
|||
Convert the counter to a string. |
|||
Write the string on the console. |
|||
Repeat. |
|||
Wait for the escape key. |
|||
Shut down. |
|||
To decide if a counter is below a number: |
|||
Subtract 1 from the counter. |
|||
If the counter is less than the number, say yes. |
|||
Say no.</syntaxhighlight> |
|||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
<syntaxhighlight 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;</syntaxhighlight> |
||
=={{header|PowerShell}}== |
|||
<syntaxhighlight lang="powershell">for ($i = 10; $i -ge 0; $i--) { |
|||
$i |
|||
}</syntaxhighlight> |
|||
Alternatively, the range operator might be used as well which simply returns a contiguous range of integers: |
|||
<syntaxhighlight lang="powershell">10..0</syntaxhighlight> |
|||
=={{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. |
|||
<syntaxhighlight lang="prolog">rfor(Hi,Lo,Hi) :- Hi >= Lo. |
|||
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.</syntaxhighlight> |
|||
<pre>?- reverse_iter. |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
true. |
|||
</pre> |
</pre> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python">for i in xrange(10, -1, -1): |
|||
<python> |
|||
print i</syntaxhighlight> |
|||
for i in xrange(10, -1, -1): |
|||
print i |
|||
=== List comprehension === |
|||
</python> |
|||
<syntaxhighlight lang="python">[i for i in xrange(10, -1, -1)]</syntaxhighlight> |
|||
<syntaxhighlight lang="python">import pprint |
|||
pprint.pprint([i for i in xrange(10, -1, -1)]) |
|||
</syntaxhighlight> |
|||
=={{header|Quackery}}== |
|||
<syntaxhighlight lang="quackery">11 times [ i echo sp ]</syntaxhighlight> |
|||
{{Out}} |
|||
<pre>10 9 8 7 6 5 4 3 2 1 0</pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="r">for(i in 10:0) {print(i)}</syntaxhighlight> |
|||
=={{header|Racket}}== |
|||
<syntaxhighlight lang="racket"> |
|||
#lang racket |
|||
(for ([i (in-range 10 -1 -1)]) |
|||
(displayln i)) |
|||
</syntaxhighlight> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo Star|2010.08}} |
|||
<syntaxhighlight lang="raku" line>for 10 ... 0 { |
|||
.say; |
|||
}</syntaxhighlight> |
|||
=={{header|REBOL}}== |
|||
<syntaxhighlight lang="rebol">for i 10 0 -1 [print i]</syntaxhighlight> |
|||
=={{header|Retro}}== |
|||
<syntaxhighlight lang="retro">11 [ putn space ] iterd</syntaxhighlight> |
|||
=={{header|REXX}}== |
|||
===version 1=== |
|||
(equivalent to version 2 and version 3) |
|||
<syntaxhighlight lang="rexx"> do j=10 to 0 by -1 |
|||
say j |
|||
end</syntaxhighlight> |
|||
===version 2=== |
|||
(equivalent to version 1 and version 3) |
|||
<syntaxhighlight lang="rexx"> do j=10 by -1 to 0 |
|||
say j |
|||
end</syntaxhighlight> |
|||
===version 3=== |
|||
(equivalent to version 1 and version 2) |
|||
<br><br>Anybody who programs like this should be hunted down and shot like dogs! |
|||
<br><br>Hurrumph! Hurrumph! |
|||
<syntaxhighlight lang="rexx"> do j=10 by -2 to 0 |
|||
say j |
|||
j=j+1 /*this increments the DO index. Do NOT program like this! */ |
|||
end</syntaxhighlight> |
|||
===version 4=== |
|||
This example isn't compliant to the task, |
|||
but it shows that the increment/decrement can be a non-integer: |
|||
<syntaxhighlight lang="rexx"> do j=30 to 1 by -.25 |
|||
say j |
|||
end</syntaxhighlight> |
|||
=={{header|Ring}}== |
|||
count from 10 to 0 by -1 step: |
|||
<syntaxhighlight lang="ring"> |
|||
for i = 10 to 0 step -1 see i + nl next |
|||
</syntaxhighlight> |
|||
=={{header|RPL}}== |
|||
≪ 10 1 '''FOR''' n |
|||
n |
|||
-1 '''STEP''' |
|||
≫ |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<ruby>10.downto( |
<syntaxhighlight lang="ruby">10.downto(0) do |i| |
||
puts i |
puts i |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust">fn main() { |
|||
for i in (0..=10).rev() { |
|||
println!("{}", i); |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|Salmon}}== |
|||
<syntaxhighlight lang="salmon">for (x; 10; x >= 0; -1) |
|||
x!;</syntaxhighlight> |
|||
=={{header|Sather}}== |
|||
<syntaxhighlight lang="sather">class MAIN is |
|||
main is |
|||
i:INT; |
|||
loop i := 10.downto!(0); |
|||
#OUT + i + "\n"; |
|||
end; |
|||
end; |
|||
end;</syntaxhighlight> |
|||
=={{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}}== |
|||
<syntaxhighlight lang="scala">for(i <- 10 to 0 by -1) println(i) |
|||
//or |
|||
10 to 0 by -1 foreach println</syntaxhighlight> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<scheme>(do ((i 10 (- i 1))) |
<syntaxhighlight lang="scheme">(do ((i 10 (- i 1))) |
||
((< i 0)) |
((< i 0)) |
||
(display i) |
(display i) |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
=={{header|Scilab}}== |
|||
{{works with|Scilab|5.5.1}} |
|||
<syntaxhighlight lang="text">for i=10:-1:0 |
|||
printf("%d\n",i) |
|||
end</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:20ex"> |
|||
10 |
|||
9 |
|||
8 |
|||
7 |
|||
6 |
|||
5 |
|||
4 |
|||
3 |
|||
2 |
|||
1 |
|||
0 |
|||
</pre> |
|||
=={{header|Seed7}}== |
|||
<syntaxhighlight lang="seed7">for i range 10 downto 0 do |
|||
writeln(i); |
|||
end for;</syntaxhighlight> |
|||
=={{header|Sidef}}== |
|||
'''for(;;)''' loop: |
|||
<syntaxhighlight lang="ruby">for (var i = 10; i >= 0; i--) { |
|||
say i |
|||
}</syntaxhighlight> |
|||
'''for-in''' loop: |
|||
<syntaxhighlight lang="ruby">for i in (11 ^.. 0) { |
|||
say i |
|||
}</syntaxhighlight> |
|||
'''.each''' method: |
|||
<syntaxhighlight lang="ruby">10.downto(0).each { |i| |
|||
say i |
|||
}</syntaxhighlight> |
|||
=={{header|Simula}}== |
|||
<syntaxhighlight lang="simula">BEGIN |
|||
Integer i; |
|||
for i := 10 step -1 until 0 do |
|||
BEGIN |
|||
OutInt(i, 2); |
|||
OutImage |
|||
END |
|||
END</syntaxhighlight> |
|||
=={{header|Slate}}== |
|||
<syntaxhighlight lang="slate">10 downTo: 0 do: [| :n | print: n]</syntaxhighlight> |
|||
=={{header|Smalltalk}}== |
|||
<syntaxhighlight lang="smalltalk">10 to: 0 by: -1 do:[:aNumber | |
|||
aNumber displayNl. |
|||
]. |
|||
10 downTo: 0 do:[:eachNumber | |
|||
eachNumber displayNl. |
|||
]</syntaxhighlight> |
|||
Both enumerate 10 to 0 inclusive. |
|||
Non-Smalltalkers might be confused when seeing: |
|||
<syntaxhighlight lang="smalltalk">(10 to: 0 by: -1) do:[:eachNumber | |
|||
eachNumber displayNl. |
|||
]</syntaxhighlight> |
|||
which has the same effect, but a slightly different mechanism. |
|||
The first one sends a <tt>"to:by:do:"</tt> message to the Integer 10, passing "0", "-1", and the closure as arguments. There (in the integer), the counting and closure invokation takes place (who cares how it does it?). |
|||
The second example first instantiates a range-collection object (called ''Interval'' in Smalltalk) with the <tt>"to:by:"</tt> message (sent to the integer), and then this Interval object gets a <tt>"do:"</tt> message.<br>Which - like all collections - enumerates its elements, in this case [10..0]. |
|||
Thus the first variant is one message send (aka virtual function call) to the number, whereas the second is two message sends and an object instantiation. |
|||
The nice thing with Intervals is that they can be concatenated with a <tt>","</tt> operator (like all collections); thus, I could also write: |
|||
<syntaxhighlight lang="smalltalk">(10 to: 5 by: -1),(0 to: 4) do:[:eachNumber | |
|||
eachNumber displayNl. |
|||
]</syntaxhighlight> |
|||
to enumerate in a different order, |
|||
<br>or combine ranges with a constant array: |
|||
<syntaxhighlight lang="smalltalk">(10 to: 0 by: -2),#(99 999),(1 to: 9 by: 2) do:[:each | |
|||
each displayNl. |
|||
]</syntaxhighlight> |
|||
or with a computed array: |
|||
<syntaxhighlight lang="smalltalk">(10 to: 0 by: -2),{ 10 factorial . 11 factorial},(1 to: 9 by: 2) do:[:each | |
|||
each displayNl. |
|||
]</syntaxhighlight> |
|||
PS: there is also a reverse do, as in: |
|||
<syntaxhighlight lang="smalltalk">(0 to:10) reverseDo:[:each | |
|||
each displayNl. |
|||
]</syntaxhighlight> |
|||
=={{header|SNOBOL4}}== |
|||
<syntaxhighlight lang="snobol4"> COUNT = 10 |
|||
LOOP OUTPUT = COUNT |
|||
COUNT = COUNT - 1 |
|||
GE(COUNT, 0) :S(LOOP) |
|||
END</syntaxhighlight> |
|||
=={{header|SNUSP}}== |
=={{header|SNUSP}}== |
||
<syntaxhighlight lang="snusp">++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi |
|||
\n counter #\?>.</ \ @@@+@+++++# itoa |
|||
loop</syntaxhighlight> |
|||
=={{header|Sparkling}}== |
|||
<syntaxhighlight lang="sparkling">for var i = 10; i >= 0; i-- { |
|||
print(i); |
|||
}</syntaxhighlight> |
|||
=={{header|Spin}}== |
|||
{{works with|BST/BSTC}} |
|||
{{works with|FastSpin/FlexSpin}} |
|||
{{works with|HomeSpun}} |
|||
{{works with|OpenSpin}} |
|||
<syntaxhighlight lang="spin">con |
|||
_clkmode = xtal1 + pll16x |
|||
_clkfreq = 80_000_000 |
|||
obj |
|||
ser : "FullDuplexSerial.spin" |
|||
pub main | n |
|||
ser.start(31, 30, 0, 115200) |
|||
repeat n from 10 to 0 |
|||
ser.dec(n) |
|||
ser.tx(32) |
|||
waitcnt(_clkfreq + cnt) |
|||
ser.stop |
|||
cogstop(0)</syntaxhighlight> |
|||
{{out}} |
|||
<pre>10 9 8 7 6 5 4 3 2 1 0</pre> |
|||
=={{header|SPL}}== |
|||
<syntaxhighlight lang="spl">> i, 10..0,-1 |
|||
#.output(i) |
|||
<</syntaxhighlight> |
|||
=={{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. |
|||
<syntaxhighlight lang="ssem">10001000000000100000000000000000 0. -17 to c |
|||
11001000000001100000000000000000 1. c to 19 |
|||
11001000000000100000000000000000 2. -19 to c |
|||
01001000000000010000000000000000 3. Sub. 18 |
|||
00010000000001100000000000000000 4. c to 8 |
|||
01001000000000100000000000000000 5. -18 to c |
|||
11001000000001100000000000000000 6. c to 19 |
|||
11001000000000100000000000000000 7. -19 to c |
|||
00000000000000000000000000000000 8. generated at run time |
|||
11110000000000010000000000000000 9. Sub. 15 |
|||
01001000000001100000000000000000 10. c to 18 |
|||
11110000000000010000000000000000 11. Sub. 15 |
|||
00000000000000110000000000000000 12. Test |
|||
00001000000000000000000000000000 13. 16 to CI |
|||
00000000000001110000000000000000 14. Stop |
|||
10000000000000000000000000000000 15. 1 |
|||
11111111111111111111111111111111 16. -1 |
|||
00000100000001100000000000000000 17. c to 32 |
|||
01010000000000000000000000000000 18. 10</syntaxhighlight> |
|||
=={{header|Stata}}== |
|||
See '''[https://www.stata.com/help.cgi?forvalues forvalues]''' and '''[https://www.stata.com/help.cgi?foreach foreach]''' in Stata help. |
|||
<syntaxhighlight lang="stata">forvalues n=10(-1)0 { |
|||
display `n' |
|||
} |
|||
forvalues n=10 9 to 0 { |
|||
display `n' |
|||
} |
|||
foreach n of numlist 10/0 { |
|||
display `n' |
|||
}</syntaxhighlight> |
|||
=={{header|Swift}}== |
|||
<syntaxhighlight lang="swift">for i in stride(from: 10, through: 0, by: -1) { |
|||
println(i) |
|||
}</syntaxhighlight> |
|||
Alternately: |
|||
<syntaxhighlight lang="swift">for i in lazy(0...10).reverse() { |
|||
println(i) |
|||
}</syntaxhighlight> |
|||
In Swift 1.2 Alternately: |
|||
<syntaxhighlight lang="swift">for i in reverse(0 ... 10) { |
|||
println(i) |
|||
}</syntaxhighlight> |
|||
Alternately (removed in Swift 3): |
|||
<syntaxhighlight lang="swift">for var i = 10; i >= 0; i-- { |
|||
println(i) |
|||
}</syntaxhighlight> |
|||
Swift 3: |
|||
<syntaxhighlight lang="swift">for i in (0...10).reversed() { |
|||
print(i) |
|||
}</syntaxhighlight> |
|||
=={{header|Tailspin}}== |
|||
Not really a for-loop, but it sends a stream of values where each gets treated the same way. |
|||
<syntaxhighlight lang="tailspin"> |
|||
10..0:-1 -> '$; |
|||
' -> !OUT::write |
|||
</syntaxhighlight> |
|||
=={{header|Tcl}}== |
|||
<syntaxhighlight lang="tcl">for {set i 10} {$i >= 0} {incr i -1} { |
|||
puts $i |
|||
} |
|||
# puts "We have liftoff!"</syntaxhighlight> |
|||
=={{header|Trith}}== |
|||
<syntaxhighlight lang="trith">10 inc iota reverse [print] each</syntaxhighlight> |
|||
<syntaxhighlight lang="trith">10 [dup print dec] [dup 0 >=] while drop</syntaxhighlight> |
|||
=={{header|TUSCRIPT}}== |
|||
<syntaxhighlight lang="tuscript"> |
|||
$$ MODE TUSCRIPT |
|||
LOOP n=10,0,-1 |
|||
PRINT n |
|||
ENDLOOP |
|||
</syntaxhighlight> |
|||
=={{header|UNIX Shell}}== |
|||
{{works with|Bourne Shell}} |
|||
<syntaxhighlight lang="bash">i=10 |
|||
while test $i -ge 0; do |
|||
echo $i |
|||
i=`expr $i - 1` |
|||
done |
|||
# or |
|||
jot - 10 0 -1 |
|||
# or |
|||
seq 10 -1 0</syntaxhighlight> |
|||
---- |
|||
{{works with|bash}} |
|||
<syntaxhighlight lang="bash">for(( Z=10; Z>=0; Z-- )); do |
|||
echo $Z |
|||
done |
|||
#or |
|||
for Z in {10..0}; do |
|||
echo $Z |
|||
done |
|||
</syntaxhighlight> |
|||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
{{works with|OpenBSD|4.9}} |
|||
yes \ |cat -n |head -n 10 | tac |
|||
<syntaxhighlight lang="bash">yes '' | cat -n | head -n 11 | while read n; do |
|||
expr $n - 1 |
|||
done | tail -r</syntaxhighlight> |
|||
This pipe uses several nonstandard commands: <code>cat -n</code> and <code>tail -r</code> might not work with some systems. |
|||
If there is no <code>tail -r</code>, try <code>tac</code>. |
|||
=={{header|Ursa}}== |
|||
<syntaxhighlight lang="ursa">decl int i |
|||
for (set i 10) (> i -1) (dec i) |
|||
out i endl console |
|||
end for</syntaxhighlight> |
|||
=={{header|V}}== |
=={{header|V}}== |
||
<syntaxhighlight lang="v">10 |
|||
10 |
|||
[0 >] |
|||
[dup puts pred] |
|||
while</syntaxhighlight> |
|||
=={{header|Vala}}== |
|||
<syntaxhighlight lang="vala">for (int i = 10; i >= 0; --i) |
|||
stdout.printf("%d\n", i);</syntaxhighlight> |
|||
=={{header|Vedit macro language}}== |
|||
<syntaxhighlight lang="vedit">for (#1 = 10; #1 >= 0; #1--) { |
|||
Num_Type(#1) |
|||
}</syntaxhighlight> |
|||
=={{header|Verilog}}== |
|||
<syntaxhighlight lang="verilog"> |
|||
module main; |
|||
integer i; |
|||
initial begin |
|||
for(i = 10; i >= 0; i = i - 1) $write(i); |
|||
$finish ; |
|||
end |
|||
endmodule |
|||
</syntaxhighlight> |
|||
=={{header|V (Vlang)}}== |
|||
<syntaxhighlight lang="v (vlang)">fn main() { |
|||
for i := 10; i >= 0; i-- { |
|||
print('$i ') |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 9 8 7 6 5 4 3 2 1 0 |
|||
</pre> |
|||
=={{header|Wart}}== |
|||
<syntaxhighlight lang="text">for i 10 (i >= 0) --i |
|||
prn i</syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">for (i in 10..0) System.write("%(i) ") |
|||
System.print()</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
10 9 8 7 6 5 4 3 2 1 0 |
|||
</pre> |
|||
=={{header|XPL0}}== |
|||
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
|||
int I; |
|||
for I:= 10 downto 0 do |
|||
[IntOut(0, I); CrLf(0)]</syntaxhighlight> |
|||
=={{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}}== |
|||
<syntaxhighlight lang="zkl">foreach n in ([10..0,-1]){ println(n) } |
|||
[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)</syntaxhighlight> |
|||
=={{header|Zig}}== |
|||
<syntaxhighlight lang="zig">const std = @import("std"); |
|||
pub fn main() !void { |
|||
var i: u8 = 11; |
|||
while (i > 0) { |
|||
i -= 1; |
|||
try std.io.getStdOut().writer().print("{d}\n", .{i}); |
|||
} |
|||
}</syntaxhighlight> |
Latest revision as of 12:42, 10 July 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Write a for loop which writes a countdown from 10 to 0.
- Related tasks
- Loop over multiple arrays simultaneously
- Loops/Break
- Loops/Continue
- Loops/Do-while
- Loops/Downward for
- Loops/For
- Loops/For with a specified step
- Loops/Foreach
- Loops/Increment loop index within loop body
- Loops/Infinite
- Loops/N plus one half
- Loops/Nested
- Loops/While
- Loops/with multiple ranges
- Loops/Wrong ranges
11l
L(i) (10..0).step(-1)
print(i)
360 Assembly
Use of BXLE and BCT opcodes.
* Loops/Downward for 27/07/2015
LOOPDOWN CSECT
USING LOOPDOWN,R12
LR R12,R15 set base register
BEGIN EQU *
* fisrt loop with a BXLE BXLE: Branch on indeX Low or Equal
LH R2,=H'11' from 10 (R2=11) index
LH R4,=H'-1' step -1 (R4=-1)
LH R5,=H'-1' to 0 (R5=-1)
LOOPI BXLE R2,R4,ELOOPI R2=R2+R4 if R2<=R5 goto ELOOPI
XDECO R2,BUFFER edit R2
XPRNT BUFFER,L'BUFFER print
B LOOPI
ELOOPI EQU *
* second loop with a BCT BCT: Branch on CounT
LA R2,10 index R2=10
LA R3,11 counter R3=11
LOOPJ XDECO R2,BUFFER edit R2
XPRNT BUFFER,L'BUFFER print
BCTR R2,0 R2=R2-1
ELOOPJ BCT R3,LOOPJ R3=R3-1 if R3<>0 goto LOOPI
RETURN XR R15,R15 set return code
BR R14 return to caller
BUFFER DC CL80' '
YREGS
END LOOPDOWN
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.
;An OS/hardware specific routine that is setup to display the Ascii character
;value contained in the Accumulator
Send = $9000 ;routine not implemented here
PrintNewLine = $9050 ;routine not implemented here
*= $8000 ;set base address
Start PHA ;push Accumulator and Y register onto stack
TYA
PHA
LDY #10 ;set Y register to loop start value
TYA ;place loop value in the Accumulator
Loop JSR PrintTwoDigits
JSR PrintNewLine
DEY ;decrement loop value
BPL Loop ;continue loop if sign flag is clear
PLA ;pop Y register and Accumulator off of stack
TAY
PLA
RTS ;exit
;Print value in Accumulator as two hex digits
PrintTwoDigits
PHA
LSR
LSR
LSR
LSR
JSR PrintDigit
PLA
AND #$0F
JSR PrintDigit
RTS
;Convert value in Accumulator to an Ascii hex digit
PrintDigit
ORA #$30
JSR Send ;routine not implemented here
RTS
68000 Assembly
Code is called as a subroutine, i.e. "JSR ForLoop." OS/Hardware specific printing subroutines are unimplemented here.
ForLoop:
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
8080 Assembly
This assumes the CP/M operating system.
;-------------------------------------------------------
; 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
- Output:
10 9 8 7 6 5 4 3 2 1
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 LOOP
instruction will loop a section of code, using the CX
register as the loop counter.
.model small ;.exe file, max 128 KB
.stack 1024 ;reserve 1 KB for the stack pointer.
.data
;no data needed
.code
start:
mov ax,0100h ;UNPACKED BCD "10"
mov cx,0Bh ;loop counter
repeat_countdown:
call PrintBCD_IgnoreLeadingZeroes
sub ax,1
aas
;ascii adjust for subtraction, normally 0100h - 1 = 0FFh but this corrects it to 0009h
push ax
mov dl,13
mov ah,02h
int 21h
mov dl,10
mov ah,02h
int 21h
;these 6 lines of code are the "new line" function
pop ax
loop repeat_countdown ;decrement CX and jump back to the label "repeat_countdown" if CX != 0
mov ax,4C00h
int 21h ;return to DOS
PrintBCD_IgnoreLeadingZeroes:
push ax
cmp ah,0
jz skipLeadingZero
or ah,30h ;convert a binary-coded decimal quantity to an ASCII numeral
push dx
push ax
mov al,ah
mov ah,0Eh
int 10h ;prints AL to screen
pop ax
pop dx
skipLeadingZero:
or al,30h
push dx
push ax
mov ah,0Eh
int 10h
pop ax
pop dx
pop ax
ret
end start ;EOF
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopdownward64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "Counter = @ \n" // message result
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x4,#10
1: // begin loop
mov x0,x4
ldr x1,qAdrsZoneConv // display value
bl conversion10 // call decimal conversion
ldr x0,qAdrszMessResult
ldr x1,qAdrsZoneConv // display value
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
subs x4,x4,1 // decrement counter
bge 1b // loop if greather
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
Ada
for I in reverse 0..10 loop
Put_Line(Integer'Image(I));
end loop;
Agena
Tested with Agena 2.9.5 Win32
for i from 10 downto 0 do
print( i )
od
ALGOL 60
Based on the 1962 Revised Report on ALGOL:
begin integer i; for i:=10 step -1 until 0 do outinteger(1,i) end
'BEGIN' 'COMMENT' Loops/Downward for - Algol60 - 23/06/2018;
'INTEGER' I;
'FOR' I := 10 'STEP' -1 'UNTIL' 0 'DO'
OUTINTEGER(1,I)
'END'
ALGOL 68
FOR i FROM 10 BY -1 TO 0 DO
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.
FOR i FROM 10 DOWNTO 0 DO
print((i,new line))
OD
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
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
- Output:
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
ALGOL W
begin
for i := 10 step -1 until 0 do
begin
write( i )
end
end.
Amazing Hopper
#include <flow.h>
DEF-MAIN
CLR-SCR
SET(i, 10)
LOOP(ciclo abajo)
PRNL(i)
BACK-IF-NOT-ZERO(i--, ciclo abajo)
END
- Output:
10 9 8 7 6 5 4 3 2 1 0
AmigaE
PROC main()
DEF i
FOR i := 10 TO 0 STEP -1
WriteF('\d\n', i)
ENDFOR
ENDPROC
AppleScript
repeat with i from 10 to 0 by -1
log i
end repeat
ARM Assembly
/* ARM assembly Raspberry PI */
/* program loopdownward.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .ascii "Counter = " @ message result
sMessValeur: .fill 12, 1, ' '
.asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
push {fp,lr} @ saves 2 registers
mov r4,#10
1: @ begin loop
mov r0,r4
ldr r1,iAdrsMessValeur @ display value
bl conversion10 @ call function with 2 parameter (r0,r1)
ldr r0,iAdrszMessResult
bl affichageMess @ display message
subs r4,#1 @ decrement counter
bge 1b @ loop if greather
100: @ standard end of the program
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrszMessResult: .int szMessResult
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal */
/******************************************************************/
/* r0 contains value and r1 address area */
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#10
1: @ start loop
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
sub r2,#1 @ previous position
cmp r0,#0 @ stop if quotient = 0 */
bne 1b @ else loop
@ and move spaces in first on area
mov r1,#' ' @ space
2:
strb r1,[r3,r2] @ store space in area
subs r2,#1 @ @ previous position
bge 2b @ loop if r2 >= zéro
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 signé */
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
/* and http://www.hackersdelight.org/ */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10:
/* r0 contains the argument to be divided by 10 */
push {r2-r4} /* save registers */
mov r4,r0
mov r3,#0x6667 @ r3 <- magic_number lower
movt r3,#0x6666 @ r3 <- magic_number upper
smull r1, r2, r3, r0 @ r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0)
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
add r0, r2, r1 /* r0 <- r2 + r1 */
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
pop {r2-r4}
bx lr /* leave function */
Arturo
loop 10..0 'i [
print i
]
- Output:
10 9 8 7 6 5 4 3 2 1 0
Asymptote
Asymptote's control structures are similar to those in C, C++, or Java
for(int i = 10; i >=0; --i) {
write(i);
}
AutoHotkey
x := 10
While (x >= 0)
{
output .= "`n" . x
x--
}
MsgBox % output
Avail
For each i from 10 to 0 by -1 do [Print: “i” ++ "\n";];
Note that 10 to 0 by -1
section isn't a fixed part of the loop syntax, but a call to the _to_by_
method, which returns a tuple of integers in a range separated by a particular step size, in this case returning <10, 9, 8, 7, 6, 5, 4, 3, 2, 1>
.
AWK
BEGIN {
for(i=10; i>=0; i--) {
print i
}
}
Axe
Axe does not support for loops with step sizes other than 1.
For(I,0,10)
Disp 10-I▶Dec,i
End
Bait
fun main() {
for i := 10; i >= 0; i -= 1 {
println(i)
}
}
BASIC
Applesoft BASIC
FOR I = 10 TO 0 STEP -1 : PRINT I : NEXT I
BaCon
' Downward for
FOR i = 10 DOWNTO 0 : PRINT i : NEXT
BASIC256
for i = 10 to 0 step -1
print i; " ";
next i
print
end
BBC BASIC
FOR i% = 10 TO 0 STEP -1
PRINT i%
NEXT
Commodore BASIC
10 FOR I = 10 TO 0 STEP -1
20 PRINT I
30 NEXT
Chipmunk Basic
The GW-BASIC solution works without any changes.
FBSL
#APPTYPE CONSOLE
FOR DIM i = 10 DOWNTO 0
PRINT i
NEXT
PAUSE
FreeBASIC
' FB 1.05.0 Win64
For i As Integer = 10 To 0 Step -1
Print i; " ";
Next
Print
Sleep
- Output:
10 9 8 7 6 5 4 3 2 1 0
FutureBasic
window 1, @"Countdown", ( 0, 0, 400, 300 )
NSInteger i
for i = 10 to 0 step -1
print i
next
HandleEvents
Output:
10 9 8 7 6 5 4 3 2 1 0
Gambas
Click this link to run this code
Public Sub Main()
Dim siCount As Short
For siCount = 10 DownTo 0
Print siCount;;
Next
End
Output:
10 9 8 7 6 5 4 3 2 1 0
GW-BASIC
10 FOR I% = 10 TO 0 STEP -1
20 PRINT I%
30 NEXT I%
IS-BASIC
100 FOR I=10 TO 0 STEP-1
110 PRINT I
120 NEXT
Liberty BASIC
for i = 10 to 0 step -1
print i
next i
end
Microsoft Small Basic
For i = 10 To 0 Step -1
TextWindow.WriteLine(i)
EndFor
MSX Basic
The GW-BASIC solution works without any changes.
NS-HUBASIC
10 FOR 1=10 TO 0 STEP -1
20 PRINT I
30 NEXT
PureBasic
For i=10 To 0 Step -1
Debug i
Next
QB64
CBTJD: 2020/03/14
FOR n = 10 TO 0 STEP -1
PRINT n
NEXT
QuickBASIC
for i = 10 to 0 step -1
print i
next i
Quite BASIC
The GW-BASIC solution works without any changes.
Run BASIC
for i = 10 to 0 step -1
print i
next i
end
SmallBASIC
for ii = 10 to 0 step -1
print ii
next
TI-83 BASIC
:For(I,10,0,-1
:Disp I
:End
TI-89 BASIC
Local i
For i, 10, 0, –1
Disp i
EndFor
True BASIC
FOR i = 10 TO 0 STEP -1
PRINT i; " ";
NEXT i
PRINT
END
Tiny BASIC
10 REM Loops/Downward for
20 LET I = 10
30 IF I = -1 THEN END
40 PRINT I
50 LET I = I - 1
60 GOTO 30
70 END
VBA
For i = 10 To 0 Step -1
Debug.Print i
Next i
Visual Basic .NET
For i = 10 To 0 Step -1
Console.WriteLine(i)
Next
XBasic
PROGRAM "downwardfor"
DECLARE FUNCTION Entry()
FUNCTION Entry()
FOR i% = 10 TO 0 STEP -1
PRINT i%
NEXT i%
END FUNCTION
END PROGRAM
Yabasic
for i = 10 to 0 step -1
print i, " ";
next i
print
end
ZX Spectrum Basic
10 FOR l = 10 TO 0 STEP -1
20 PRINT l
30 NEXT l
Batch File
@echo off
for /l %%D in (10,-1,0) do echo %%D
bc
for (i = 10; i >= 0; i--) i
quit
Befunge
55+>:.:v
@ ^ -1_
BQN
Each (¨
) is an operator in BQN that helps with emulating loops like for and foreach.
•Show¨⌽↕11
Bracmat
10:?i
& whl'(out$!i&!i+-1:~<0:?i)
Brainf***
>++++++++[-<++++++>] //cell 0 now contains 48 the ASCII code for "0"
<+.-. //print the digits 1 and 0
>++++++++++. //cell 1 now contains the carriage return; print it!
>+++++++++ //cell 2 now contains the number 9; this is our counter
<<+++++++++>> //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
Brat
10.to 0 { n | p n }
C
int i;
for(i = 10; i >= 0; --i)
printf("%d\n",i);
C#
for (int i = 10; i >= 0; i--)
{
Console.WriteLine(i);
}
C++
for(int i = 10; i >= 0; --i)
std::cout << i << "\n";
C3
for (int i = 10; i >= 0; i--)
{
io::printn(i);
}
Ceylon
for (i in 10..0) {
print(i);
}
Chapel
for i in 1..10 by -1 do
writeln(i);
In case you wonder why it is not written as 10..1 by -1: by is an operator that works on ranges, and it should work the same when the range was defined earlier, like in
var r = 1..10;
for i in r by -1 do { ... }
Clipper
FOR i := 10 TO 0 STEP -1
? i
NEXT
Clojure
(doseq [x (range 10 -1 -1)] (println x))
COBOL
free-form
identification division.
program-id. countdown.
environment division.
data division.
working-storage section.
01 counter pic 99.
88 counter-done value 0.
01 counter-disp pic Z9.
procedure division.
perform with test after varying counter from 10 by -1 until counter-done
move counter to counter-disp
display counter-disp
end-perform
stop run.
- Output:
10 9 8 7 6 5 4 3 2 1 0
CoffeeScript
This could be written either in the array comprehension style, or in "regular" for loop style.
# The more compact "array comprehension" style
console.log i for i in [10..0]
# The "regular" for loop style.
for i in [10..0]
console.log i
# More compact version of the above
for i in [10..0] then console.log i
10 9 8 7 6 5 4 3 2 1 0
(the output is repeated three times; once for each loop)
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
(loop for i from 10 downto 1 do
(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
Using Tagbody and Go
(let ((count 10)) ; Create local variable count = 10
(tagbody
dec ; Create tag dec
(print count) ; Prints count
(decf count) ; Decreases count
(if (not (< count 0)) ; Ends loop when negative
(go dec)))) ; Loops back to tag dec
Using Recursion
(defun down-to-0 (count)
(print count)
(if (not (zerop count))
(down-to-0 (1- count))))
(down-to-0 10)
- Output:
10 9 8 7 6 5 4 3 2 1 0
Computer/zero Assembly
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
- Output:
Crystal
10.step(to: 0, by: -1).each { |i|
puts i
}
D
import std.stdio: writeln;
void main() {
for (int i = 10; i >= 0; --i)
writeln(i);
writeln();
foreach_reverse (i ; 0 .. 10 + 1)
writeln(i);
}
- Output:
10 9 8 7 6 5 4 3 2 1 0 10 9 8 7 6 5 4 3 2 1 0
Dart
void main() {
for (var i = 10; i >= 0; --i) {
print(i);
}
}
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
Delphi
- See Pascal
Draco
proc nonrec main() void:
byte i;
for i from 10 downto 0 do
write(i," ")
od
corp
- Output:
10 9 8 7 6 5 4 3 2 1 0
DWScript
for i := 10 downto 0 do
PrintLn(i);
E
for i in (0..10).descending() { println(i) }
EasyLang
for i = 10 downto 0
print i
.
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
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.
[ Loop with downward counter
==========================
A program for the EDSAC
Prints the integers 10 down to 0
The counter is stored at address 20@
Its initial value is 9 * 2^12
(9 in the high 5 bits, representing
the character '9') and it counts
down in steps of 2^12
Works with Initial Orders 2 ]
T56K [ set load point ]
GK [ set base address ]
[ orders ]
O14@ [ print figure shift ]
O15@ [ print '1' ]
O16@ [ print '0' ]
O17@ [ print CR ]
O18@ [ print LF ]
[ 5 ] O20@ [ print c ]
O17@ [ print CR ]
O18@ [ print LF ]
T19@ [ acc := 0 ]
A20@ [ acc += c ]
S15@ [ acc -:= character '1' ]
U20@ [ c := acc ]
E5@ [ branch on non-negative ]
ZF [ stop ]
[ constants ]
[ 14 ] #F [ πF -- figure shift ]
[ 15 ] QF [ character '1' ]
[ 16 ] PF [ character '0' ]
[ 17 ] @F [ θF -- CR ]
[ 18 ] &F [ ΔF -- LF ]
[ variables ]
[ 19 ] P0F [ used to clear acc ]
[ 20 ] OF [ character c = '9' ]
EZPF [ start when loaded ]
EGL
for ( i int from 10 to 0 decrement by 1 )
SysLib.writeStdout( i );
end
Ela
Standard Approach
open monad io
each [] = do return ()
each (x::xs) = do
putStrLn $ show x
each xs
each [10,9..0] ::: IO
Alternative Approach
open monad io
countDown m n | n < m = do return ()
| else = do
putStrLn $ show n
countDown m (n - 1)
_ = countDown 0 10 ::: IO
Elixir
iex(1)> Enum.each(10..0, fn i -> IO.puts i end)
10
9
8
7
6
5
4
3
2
1
0
:ok
Erlang
%% Implemented by Arjun Sunel
-module(downward_loop).
-export([main/0]).
main() ->
for_loop(10).
for_loop(N) ->
if N > 0 ->
io:format("~p~n",[N] ),
for_loop(N-1);
true ->
io:format("~p~n",[N])
end.
- Output:
10 9 8 7 6 5 4 3 2 1 0 ok
ERRE
FOR I%=10 TO 0 STEP -1 DO
PRINT(I%)
END FOR
Euphoria
for i = 10 to 0 by -1 do
? i
end for
F#
Using an enumerable expression:
for i in 10..-1..0 do
printfn "%d" i
Using the 'downto' keyword:
for i = 10 downto 0 do
printfn "%d" i
Factor
11 <iota> <reversed> [ . ] each
FALSE
10[$0>][$." "1-]#.
Fantom
class DownwardFor
{
public static Void main ()
{
for (Int i := 10; i >= 0; i--)
{
echo (i)
}
}
}
Fermat
for i = 10 to 0 by -1 do !!i; od
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
PROGRAM DOWNWARDFOR
C Initialize the loop parameters.
INTEGER I, START, FINISH, STEP
PARAMETER (START = 10, FINISH = 0, STEP = -1)
C If you were to leave off STEP, it would default to positive one.
DO 10 I = START, FINISH, STEP
WRITE (*,*) I
10 CONTINUE
STOP
END
Frink
for i = 10 to 0 step -1
println[i]
GAP
for i in [10, 9 .. 0] do
Print(i, "\n");
od;
GML
for(i = 10; i >= 0; i -= 1)
show_message(string(i))
Go
for i := 10; i >= 0; i-- {
fmt.Println(i)
}
package main import "fmt" import "time" func main() { i := 10 for i > 0 { fmt.Println(i) time.Sleep(time.Second) i = i - 1 } fmt.Println("blast off") }
Groovy
for (i in (10..0)) {
println i
}
Harbour
FOR i := 10 TO 0 STEP -1
? i
NEXT
Haskell
import Control.Monad
main :: IO ()
main = forM_ [10,9 .. 0] print
Haxe
Haxe lacks a downward for-loop, but it is easy to create an iterator to serve that purpose.
class Step {
var end:Int;
var step:Int;
var index:Int;
public inline function new(start:Int, end:Int, step:Int) {
this.index = start;
this.end = end;
this.step = step;
}
public inline function hasNext() return step > 0 ? end >= index : index >= end;
public inline function next() return (index += step) - step;
}
class Main {
static function main() {
for (i in new Step(10, 0, -1)) {
Sys.print('$i ');
}
}
}
- Output:
10 9 8 7 6 5 4 3 2 1 0
hexiscript
for let i 10; i >= 0; i--
println i
endfor
HicEst
DO i = 10, 0, -1
WRITE() i
ENDDO
HolyC
I8 i;
for (i = 10; i >= 0; --i)
Print("%d\n", i);
Icon and Unicon
There are four looping controls 'every', 'repeat', 'until', and 'while' (see Introduction to Icon and Unicon/Looping Controls for more information.) The closest to a 'for' loop is 'every'.
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)
Inform 6
for(i = 10: i >= 0: i--)
print i, "^";
Io
for(i,10,0,-1,
i println
)
J
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
,. i. -11
10
9
8
7
6
5
4
3
2
1
0
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.
smoutput i
end.
)
Though it's rare to see J code like this.
That said, a convenient routine for generating intervals in J might be:
thru=: <. + i.@(+*)@-~
For example:
10 thru 0
10 9 8 7 6 5 4 3 2 1 0
(or ,.10 thru 0
if you want each number on a line by itself)
This verb "thru" will count up or down, starting and stop at the indicated left and right ending points. (However, note that this particular implementation of 'thru' will return an empty result if the starting and ending values are the same. Whether that's useful or a problem, depends on the application.)
Java
for (int i = 10; i >= 0; i--) {
System.out.println(i);
}
JavaScript
for (var i=10; i>=0; --i) print(i);
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:
for (var i = 11; i--;) console.log(i);
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.
var i = 11;
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.
If we have grown over-attached to the English morpheme 'for', we might think first of turning to Array.forEach(), and write something like:
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(
function (x, i) {
return m + i;
}
);
}
range(0, 10).reverse().forEach(
function (x) {
console.log(x);
}
);
but this is still a statement with side-effects, rather than a composable expression with a value.
We can get an expression (assuming that the range() function (above) is defined) but replacing Array.forEach with Array.map()
console.log(
range(0, 10).reverse().map(
function (x) {
return x;
}
).join('\n')
);
but in this case, we are simply mapping an identity function over the values, so the expression simplifies down to:
console.log(
range(0, 10).reverse().join('\n')
);
jq
If range/3 is available in your jq:
range(10;-1;-1)
Otherwise:
range(-10;1) | -.
Julia
for i in 10:-1:0
println(i)
end
Kotlin
// version 1.3.61
fun main() {
(10 downTo 0).forEach { println(it) }
}
Lambdatalk
{def downward_for
{lambda {:i}
{if {< :i 0}
then (end of loop)
else :i {downward_for {- :i 1}}}}}
-> downward_for
{downward_for 10}
-> 10 9 8 7 6 5 4 3 2 1 0 (end of loop)
langur
for i in 10..0 {
writeln i
}
for i = 10; i > -1; i -= 1 {
writeln i
}
Lasso
loop(-from=10, -to=0, -by=-1) => {^ loop_count + ' ' ^}
LDPL
data:
i is number
procedure:
for i from 10 to -1 step -1 do
display i lf
repeat
Lhogho
Slightly different syntax for for
compared to Logo.
for "i [10 0] [print :i]
Lingo
repeat with i = 10 down to 0
put i
end repeat
Lisaac
10.downto 0 do { i : INTEGER;
i.println;
};
LiveCode
Livecode's repeat "for" variant does not have a "down to" form, in a function you would need to manually decrement a counter
local x=10
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
repeat with n=10 down to 1
put n
end repeat
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]
Lua
for i=10,0,-1 do
print(i)
end
M2000 Interpreter
M2000 can operate a For like in BASIC or Like M2000. In M2000 mode, a For always execute at least one time the block inside. This FOR use absolute value of step, except when we have start value and end value the same value, so from sign of step, interpreter calculate the exit value.
We can change the iterator variable of a For, but this variable is a copy of actual iterator, and next step get the proper value. So we can't change the numbers of steps, but we can use continue to skip rest of code and execute next step, or exit to exit block and stop loop. Also we can use Goto to stop loop and continue from a label.
There is a slower For, the For Next style:
For i=1 to 10 step 2 : Print i : Next i
We have to use Exit For to exit from that type of For.
This is not an error (each for has private counter value):
for i=1 to 10 :for i=1 to 2:Print i:Next i:Next i
We get 10 times two values: 1 2
Form 80, 50
Module Checkit {
set switches "+For"
For i=10 to 1 step -1 {
Print i
}
Print i=0
\\ this For switch make it like For in BASIC
\\ block skipped
For i=1 to 10 step -1 {
Print i
}
print i=1
\\ but this is the default behavior
\\
set switches "-For"
\\ sign of step used when start is same as end to calculate the exit value of i
\\ This is the standard, and a For always execute at least one time the block.
\\ use absulute step_Value. Because 10>1 direction is downward.
For i=10 to 1 step -1 {
Print i
}
Print i=0
\\ loop from 1 to 10, using abs(step_value)
For i=1 to 10 step -1 {
Print i
}
print i=11
For i=1 to 1 step -1 {
Print i
}
Print i=0
}
CheckIt
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
')
Maple
Using an explicit loop:
for i from 10 to 0 by -1 do print(i) end:
Pushing the loop into the kernel:
seq(print(i),i=10..0,-1)
Mathematica /Wolfram Language
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:
For[i = 10, i >= 0, i--, Print[i]]
Using Do:
Do[Print[i], {i, 10, 0, -1}]
Using Scan:
Scan[Print, Range[10, 0, -1]]
MATLAB / Octave
for k = 10:-1:0,
printf('%d\n',k)
end;
A vectorized version of the code is
printf('%d\n',10:-1:0);
Maxima
for i from 10 thru 0 step -1 do print(i);
MAXScript
for i in 10 to 0 by -1 do print i
Mercury
:- module loops_downward_for.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int.
main(!IO) :-
Print = (pred(I::in, !.IO::di, !:IO::uo) is det :-
io.write_int(I, !IO), io.nl(!IO)
),
int.fold_down(Print, 1, 10, !IO).
Metafont
for i = 10 step -1 until 0: show i; endfor
end
The basic set of macros for Metafont defines downto, so that we can write
for i = 10 downto 0: show i; endfor end
min
10 :n (n 0 >=) (n puts! n pred @n) while
Or
10 (dup 0 <) 'pop (puts pred) () linrec
MiniScript
for i in range(10, 0)
print i
end for
МК-61/52
1 0 П0 ИП0 L0 03 С/П
Modula-2
MODULE Downward;
IMPORT InOut;
VAR
i: INTEGER;
BEGIN
FOR i := 10 TO 0 BY -1 DO
InOut.WriteInt(i, 2);
InOut.WriteLn
END
END Downward.
Modula-3
FOR i := 10 TO 0 BY -1 DO
IO.PutInt(i);
END;
MUMPS
LOOPDOWN
NEW I FOR I=10:-1:1 WRITE I WRITE:I'=1 ", "
KILL I QUIT
N/t/roff
.nr a 11 1
.while (\na > 0) \{\
\n-a
.\}
- Output:
10 9 8 7 6 5 4 3 2 1 0
Nemerle
for (i = 10; i >= 0; i--) {WriteLine($"$i")}
foreach (i in [10, 9 .. 0]) {WriteLine($"$i")}
NetRexx
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
say
say 'Loops/Downward for'
loop i_ = 10 to 0 by -1
say i_.right(2)
end i_
NewLISP
(for (i 10 0)
(println i))
Nim
for x in countdown(10,0): echo(x)
- Output:
10 9 8 7 6 5 4 3 2 1 0
Nu
for i in 10..0 {print $i}
Oberon-2
FOR i := 10 TO 0 BY -1 DO
Out.Int(i,0);
END;
Objeck
for(i := 10; i >= 0; i--;) {
i->PrintLine();
};
OCaml
for i = 10 downto 0 do
Printf.printf "%d\n" i
done
Octave
for i = 10:-1:0
% ...
endfor
Oforth
10 0 -1 step: i [ i println ]
Oz
for I in 10..0;~1 do
{Show I}
end
PARI/GP
forstep(n=10,0,-1,print(n))
Pascal
for i := 10 downto 0 do
writeln(i);
PascalABC.NET
##
for var i:=10 downto 0 do
Print(i);
- Output:
10 9 8 7 6 5 4 3 2 1 0
Peloton
English fixed-length opcodes
<@ ITEFORLITLITLITLIT>0|<@ SAYVALFOR>...</@>|10|-1</@>
Simplified Chinese variable-length opcodes
<# 迭代迭代次数字串字串字串字串>0|<# 显示值迭代次数>...</#>|10|-1</#>
Perl
foreach (reverse 0..10) {
print "$_\n";
}
Phix
for i=10 to 0 by -1 do ?i end for
Phixmonti
/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/Downward_for
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
( 10 0 -1 ) for ? endfor
PHP
for ($i = 10; $i >= 0; $i--)
echo "$i\n";
or
foreach (range(10, 0) as $i)
echo "$i\n";
PicoLisp
(for (I 10 (ge0 I) (dec I))
(println I) )
or:
(mapc println (range 10 0))
Pike
int main(){
for(int i = 10; i >= 0; i--){
write(i + "\n");
}
}
PL/0
var i;
begin
i := 10;
while i > -1 do
begin
! i;
i := i - 1
end
end.
PL/I
do i = 10 to 0 by -1;
put skip list (i);
end;
Plain English
One way might be:
To run:
Start up.
Put 11 into a counter.
Loop.
If the counter is below 0, break.
Convert the counter to a string.
Write the string on the console.
Repeat.
Wait for the escape key.
Shut down.
To decide if a counter is below a number:
Subtract 1 from the counter.
If the counter is less than the number, say yes.
Say no.
Pop11
lvars i;
for i from 10 by -1 to 0 do
printf(i, '%p\n');
endfor;
PowerShell
for ($i = 10; $i -ge 0; $i--) {
$i
}
Alternatively, the range operator might be used as well which simply returns a contiguous range of integers:
10..0
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,Hi) :- Hi >= Lo.
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.
?- reverse_iter. 10 9 8 7 6 5 4 3 2 1 0 true.
Python
for i in xrange(10, -1, -1):
print i
List comprehension
[i for i in xrange(10, -1, -1)]
import pprint
pprint.pprint([i for i in xrange(10, -1, -1)])
Quackery
11 times [ i echo sp ]
- Output:
10 9 8 7 6 5 4 3 2 1 0
R
for(i in 10:0) {print(i)}
Racket
#lang racket
(for ([i (in-range 10 -1 -1)])
(displayln i))
Raku
(formerly Perl 6)
for 10 ... 0 {
.say;
}
REBOL
for i 10 0 -1 [print i]
Retro
11 [ putn space ] iterd
REXX
version 1
(equivalent to version 2 and version 3)
do j=10 to 0 by -1
say j
end
version 2
(equivalent to version 1 and version 3)
do j=10 by -1 to 0
say j
end
version 3
(equivalent to version 1 and version 2)
Anybody who programs like this should be hunted down and shot like dogs!
Hurrumph! Hurrumph!
do j=10 by -2 to 0
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:
do j=30 to 1 by -.25
say j
end
Ring
count from 10 to 0 by -1 step:
for i = 10 to 0 step -1 see i + nl next
RPL
≪ 10 1 FOR n n -1 STEP ≫
Ruby
10.downto(0) do |i|
puts i
end
Rust
fn main() {
for i in (0..=10).rev() {
println!("{}", i);
}
}
Salmon
for (x; 10; x >= 0; -1)
x!;
Sather
class MAIN is
main is
i:INT;
loop i := 10.downto!(0);
#OUT + i + "\n";
end;
end;
end;
S-BASIC
var i = integer
for i = 10 to 1 step -1
print i;
next i
end
- Output:
10 9 8 7 6 5 4 3 2 1
Scala
for(i <- 10 to 0 by -1) println(i)
//or
10 to 0 by -1 foreach println
Scheme
(do ((i 10 (- i 1)))
((< i 0))
(display i)
(newline))
Scilab
for i=10:-1:0
printf("%d\n",i)
end
- Output:
10 9 8 7 6 5 4 3 2 1 0
Seed7
for i range 10 downto 0 do
writeln(i);
end for;
Sidef
for(;;) loop:
for (var i = 10; i >= 0; i--) {
say i
}
for-in loop:
for i in (11 ^.. 0) {
say i
}
.each method:
10.downto(0).each { |i|
say i
}
Simula
BEGIN
Integer i;
for i := 10 step -1 until 0 do
BEGIN
OutInt(i, 2);
OutImage
END
END
Slate
10 downTo: 0 do: [| :n | print: n]
Smalltalk
10 to: 0 by: -1 do:[:aNumber |
aNumber displayNl.
].
10 downTo: 0 do:[:eachNumber |
eachNumber displayNl.
]
Both enumerate 10 to 0 inclusive.
Non-Smalltalkers might be confused when seeing:
(10 to: 0 by: -1) do:[:eachNumber |
eachNumber displayNl.
]
which has the same effect, but a slightly different mechanism.
The first one sends a "to:by:do:" message to the Integer 10, passing "0", "-1", and the closure as arguments. There (in the integer), the counting and closure invokation takes place (who cares how it does it?).
The second example first instantiates a range-collection object (called Interval in Smalltalk) with the "to:by:" message (sent to the integer), and then this Interval object gets a "do:" message.
Which - like all collections - enumerates its elements, in this case [10..0].
Thus the first variant is one message send (aka virtual function call) to the number, whereas the second is two message sends and an object instantiation.
The nice thing with Intervals is that they can be concatenated with a "," operator (like all collections); thus, I could also write:
(10 to: 5 by: -1),(0 to: 4) do:[:eachNumber |
eachNumber displayNl.
]
to enumerate in a different order,
or combine ranges with a constant array:
(10 to: 0 by: -2),#(99 999),(1 to: 9 by: 2) do:[:each |
each displayNl.
]
or with a computed array:
(10 to: 0 by: -2),{ 10 factorial . 11 factorial},(1 to: 9 by: 2) do:[:each |
each displayNl.
]
PS: there is also a reverse do, as in:
(0 to:10) reverseDo:[:each |
each displayNl.
]
SNOBOL4
COUNT = 10
LOOP OUTPUT = COUNT
COUNT = COUNT - 1
GE(COUNT, 0) :S(LOOP)
END
SNUSP
++++++++++>++++++++++!/- @!\=@\.@@@-@-----# atoi
\n counter #\?>.</ \ @@@+@+++++# itoa
loop
Sparkling
for var i = 10; i >= 0; i-- {
print(i);
}
Spin
con
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
obj
ser : "FullDuplexSerial.spin"
pub main | n
ser.start(31, 30, 0, 115200)
repeat n from 10 to 0
ser.dec(n)
ser.tx(32)
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)
- Output:
10 9 8 7 6 5 4 3 2 1 0
SPL
> i, 10..0,-1
#.output(i)
<
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 n (stored at address 18) from the illegal instruction c to 32, yielding the actual instruction we use to store n into the array.
10001000000000100000000000000000 0. -17 to c
11001000000001100000000000000000 1. c to 19
11001000000000100000000000000000 2. -19 to c
01001000000000010000000000000000 3. Sub. 18
00010000000001100000000000000000 4. c to 8
01001000000000100000000000000000 5. -18 to c
11001000000001100000000000000000 6. c to 19
11001000000000100000000000000000 7. -19 to c
00000000000000000000000000000000 8. generated at run time
11110000000000010000000000000000 9. Sub. 15
01001000000001100000000000000000 10. c to 18
11110000000000010000000000000000 11. Sub. 15
00000000000000110000000000000000 12. Test
00001000000000000000000000000000 13. 16 to CI
00000000000001110000000000000000 14. Stop
10000000000000000000000000000000 15. 1
11111111111111111111111111111111 16. -1
00000100000001100000000000000000 17. c to 32
01010000000000000000000000000000 18. 10
Stata
See forvalues and foreach in Stata help.
forvalues n=10(-1)0 {
display `n'
}
forvalues n=10 9 to 0 {
display `n'
}
foreach n of numlist 10/0 {
display `n'
}
Swift
for i in stride(from: 10, through: 0, by: -1) {
println(i)
}
Alternately:
for i in lazy(0...10).reverse() {
println(i)
}
In Swift 1.2 Alternately:
for i in reverse(0 ... 10) {
println(i)
}
Alternately (removed in Swift 3):
for var i = 10; i >= 0; i-- {
println(i)
}
Swift 3:
for i in (0...10).reversed() {
print(i)
}
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
Tcl
for {set i 10} {$i >= 0} {incr i -1} {
puts $i
}
# puts "We have liftoff!"
Trith
10 inc iota reverse [print] each
10 [dup print dec] [dup 0 >=] while drop
TUSCRIPT
$$ MODE TUSCRIPT
LOOP n=10,0,-1
PRINT n
ENDLOOP
UNIX Shell
i=10
while test $i -ge 0; do
echo $i
i=`expr $i - 1`
done
# or
jot - 10 0 -1
# or
seq 10 -1 0
for(( Z=10; Z>=0; Z-- )); do
echo $Z
done
#or
for Z in {10..0}; do
echo $Z
done
UnixPipes
yes '' | cat -n | head -n 11 | while read n; do
expr $n - 1
done | tail -r
This pipe uses several nonstandard commands: cat -n
and tail -r
might not work with some systems.
If there is no tail -r
, try tac
.
Ursa
decl int i
for (set i 10) (> i -1) (dec i)
out i endl console
end for
V
10
[0 >]
[dup puts pred]
while
Vala
for (int i = 10; i >= 0; --i)
stdout.printf("%d\n", i);
Vedit macro language
for (#1 = 10; #1 >= 0; #1--) {
Num_Type(#1)
}
Verilog
module main;
integer i;
initial begin
for(i = 10; i >= 0; i = i - 1) $write(i);
$finish ;
end
endmodule
V (Vlang)
fn main() {
for i := 10; i >= 0; i-- {
print('$i ')
}
}
- Output:
10 9 8 7 6 5 4 3 2 1 0
Wart
for i 10 (i >= 0) --i
prn i
Wren
for (i in 10..0) System.write("%(i) ")
System.print()
- Output:
10 9 8 7 6 5 4 3 2 1 0
XPL0
include c:\cxpl\codes; \intrinsic 'code' declarations
int I;
for I:= 10 downto 0 do
[IntOut(0, I); CrLf(0)]
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.
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.
- Output:
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
zkl
foreach n in ([10..0,-1]){ println(n) }
[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)
Zig
const std = @import("std");
pub fn main() !void {
var i: u8 = 11;
while (i > 0) {
i -= 1;
try std.io.getStdOut().writer().print("{d}\n", .{i});
}
}
- Programming Tasks
- Iteration
- Simple
- 11l
- 360 Assembly
- 6502 Assembly
- 68000 Assembly
- 8080 Assembly
- 8086 Assembly
- AArch64 Assembly
- Ada
- Agena
- ALGOL 60
- ALGOL 68
- ALGOL-M
- ALGOL W
- Amazing Hopper
- AmigaE
- AppleScript
- ARM Assembly
- Arturo
- Asymptote
- AutoHotkey
- Avail
- AWK
- Axe
- Bait
- BASIC
- Applesoft BASIC
- BaCon
- BASIC256
- BBC BASIC
- Commodore BASIC
- Chipmunk Basic
- FBSL
- FreeBASIC
- FutureBasic
- Gambas
- GW-BASIC
- IS-BASIC
- Liberty BASIC
- Microsoft Small Basic
- MSX Basic
- NS-HUBASIC
- PureBasic
- QB64
- QuickBASIC
- Quite BASIC
- Run BASIC
- SmallBASIC
- TI-83 BASIC
- TI-89 BASIC
- True BASIC
- Tiny BASIC
- VBA
- Visual Basic .NET
- XBasic
- Yabasic
- ZX Spectrum Basic
- Batch File
- Bc
- Befunge
- BQN
- Bracmat
- Brainf***
- Brat
- C
- C sharp
- C++
- C3
- Ceylon
- Chapel
- Clipper
- Clojure
- COBOL
- CoffeeScript
- ColdFusion
- Common Lisp
- Computer/zero Assembly
- Crystal
- D
- Dart
- Dc
- Delphi
- Draco
- DWScript
- E
- EasyLang
- EchoLisp
- EDSAC order code
- EGL
- Ela
- Elixir
- Erlang
- ERRE
- Euphoria
- F Sharp
- Factor
- FALSE
- Fantom
- Fermat
- Forth
- Fortran
- Frink
- GAP
- GML
- Go
- Groovy
- Harbour
- Haskell
- Haxe
- Hexiscript
- HicEst
- HolyC
- Icon
- Unicon
- IDL
- Inform 6
- Io
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lambdatalk
- Langur
- Lasso
- LDPL
- Lhogho
- Lingo
- Lisaac
- LiveCode
- Logo
- Lua
- M2000 Interpreter
- M4
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Maxima
- MAXScript
- Mercury
- Metafont
- Min
- MiniScript
- МК-61/52
- Modula-2
- Modula-3
- MUMPS
- N/t/roff
- Nemerle
- NetRexx
- NewLISP
- Nim
- Nu
- Oberon-2
- Objeck
- OCaml
- Oforth
- Oz
- PARI/GP
- Pascal
- PascalABC.NET
- Peloton
- Perl
- Phix
- Phix/basics
- Phixmonti
- PHP
- PicoLisp
- Pike
- PL/0
- PL/I
- Plain English
- Pop11
- PowerShell
- Prolog
- Python
- Quackery
- R
- Racket
- Raku
- REBOL
- Retro
- REXX
- Ring
- RPL
- Ruby
- Rust
- Salmon
- Sather
- S-BASIC
- Scala
- Scheme
- Scilab
- Seed7
- Sidef
- Simula
- Slate
- Smalltalk
- SNOBOL4
- SNUSP
- Sparkling
- Spin
- SPL
- SSEM
- Stata
- Swift
- Tailspin
- Tcl
- Trith
- TUSCRIPT
- UNIX Shell
- UnixPipes
- Ursa
- V
- Vala
- Vedit macro language
- Verilog
- V (Vlang)
- Wart
- Wren
- XPL0
- Z80 Assembly
- Zkl
- Zig
- Pages with too many expensive parser function calls