Loops/Downward for: Difference between revisions

m
→‎{{header|ALGOL-M}}: added workaround
m (→‎{{header|ALGOL-M}}: added workaround)
 
(27 intermediate revisions by 12 users not shown)
Line 107:
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}}==
Line 237 ⟶ 315:
 
=={{header|ALGOL 60}}==
{{works with|A60}}
'''Based on the 1962 Revised Repport on ALGOL''':
'''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}}
Line 263 ⟶ 342:
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}}==
Line 481 ⟶ 589:
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}}==
Line 507 ⟶ 624:
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}}===
Line 517 ⟶ 646:
Sleep</syntaxhighlight>
{{out}}
<pre> 10 9 8 7 6 5 4 3 2 1 0</pre>
<pre>
10 9 8 7 6 5 4 3 2 1 0
</pre>
 
==={{header|FutureBasic}}===
Line 531 ⟶ 658:
next
 
HandleEvents</syntaxhighlight>
</syntaxhighlight>
Output:
<pre> 10
10
9
8
Line 545 ⟶ 670:
2
1
0</pre>
</pre>
 
==={{header|Gambas}}===
Line 559 ⟶ 683:
End</syntaxhighlight>
Output:
<pre>10 9 8 7 6 5 4 3 2 1 0</pre>
<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="qbasicgwbasic">10 FOR I% = 10 TO 0 STEP -1
10 FOR I% = 10 TO 0 STEP -1
20 PRINT I%
30 NEXT I%</syntaxhighlight>
</syntaxhighlight>
 
==={{header|IS-BASIC}}===
Line 579 ⟶ 700:
{{works with|Just BASIC}}
{{works with|Run BASIC}}
<syntaxhighlight lang="lb">for i = 10 to 0 step -1
for i = 10 to 0 step -1
print i
next i
Line 590 ⟶ 710:
TextWindow.WriteLine(i)
EndFor</syntaxhighlight>
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|NS-HUBASIC}}===
Line 607 ⟶ 730:
NEXT</syntaxhighlight>
 
==={{header|QBASICQuickBASIC}}===
<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}}===
Line 621 ⟶ 747:
 
==={{header|TI-83 BASIC}}===
<syntaxhighlight lang="ti83b">:For(I,10,0,-1
:For(I,10,0,-1
:Disp I
:End</syntaxhighlight>
:End
</syntaxhighlight>
 
==={{header|TI-89 BASIC}}===
Line 642 ⟶ 766:
 
==={{Header|Tiny BASIC}}===
{{works with|TinyBasic}}
<syntaxhighlight lang="tiny basic"> LET i = 10
<syntaxhighlight lang="basic">10 REM Loops/Downward for
10 IF i = -1 THEN END
20 LET I = 10
PRINT i
30 IF LET iI = i -1 1THEN END
40 PRINT I
GOTO 10
50 LET I = I - 1
END</syntaxhighlight>
60 GOTO 30
70 END</syntaxhighlight>
 
==={{header|YabasicVBA}}===
<syntaxhighlight lang="vb">For i = 10 To 0 Step -1
{{works with|QBasic}}
Debug.Print i
<syntaxhighlight lang="yabasic">for i = 10 to 0 step -1
Next i</syntaxhighlight>
print i, " ";
 
next i
==={{header|Visual Basic .NET}}===
print
end</syntaxhighlight lang="vbnet">For i = 10 To 0 Step -1
Console.WriteLine(i)
Next</syntaxhighlight>
 
==={{header|XBasic}}===
Line 669 ⟶ 797:
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}}===
Line 724 ⟶ 860:
<syntaxhighlight lang="cpp">for(int i = 10; i >= 0; --i)
std::cout << i << "\n";</syntaxhighlight>
 
=={{header|C3}}==
<syntaxhighlight lang="c3">for (int i = 10; i >= 0; i--)
{
io::printn(i);
}</syntaxhighlight>
 
=={{header|Ceylon}}==
Line 918 ⟶ 1,060:
0</pre>
 
=={{header|dcDart}}==
<syntaxhighlight lang="dart">void main() {
for (var i = 10; i >= 0; --i) {
print(i);
}
}</syntaxhighlight>
 
=={{header|dc}}==
does not use GNU extensions
 
Line 1,158 ⟶ 1,306:
}
}
</syntaxhighlight>
 
=={{header|FBSL}}==
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE
 
FOR DIM i = 10 DOWNTO 0
PRINT i
NEXT
 
PAUSE
</syntaxhighlight>
 
Line 1,323 ⟶ 1,461:
J is array-oriented, so there is very little need for loops. For example, one could satisfy this task this way:
 
<syntaxhighlight lang=J> ,. i. -11
10
9
8
7
6
5
4
3
2
1
0
</syntaxhighlight>
 
J does support loops for those times they can't be avoided (just like many languages support gotos for those time they can't be avoided).
Line 1,437 ⟶ 1,587:
 
=={{header|langur}}==
You can use a for in loop to count downward. You cannot use a for of loop for this.
 
<syntaxhighlight lang="langur">for .i in 10..0 {
writeln .i
Line 1,449 ⟶ 1,597:
=={{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}}==
Line 1,650 ⟶ 1,807:
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>
 
=={{header|Nemerle}}==
Line 1,685 ⟶ 1,853:
1
0</pre>
 
=={{header|Nu}}==
<syntaxhighlight lang="nu">
for i in 10..0 {print $i}
</syntaxhighlight>
 
=={{header|Oberon-2}}==
Line 1,772 ⟶ 1,945:
}
}</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}}==
Line 1,912 ⟶ 2,098:
for i = 10 to 0 step -1 see i + nl next
</syntaxhighlight>
 
=={{header|RPL}}==
≪ 10 1 '''FOR''' n
n
-1 '''STEP'''
 
=={{header|Ruby}}==
Line 1,938 ⟶ 2,130:
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}}==
Line 2,228 ⟶ 2,434:
<syntaxhighlight lang="vala">for (int i = 10; i >= 0; --i)
stdout.printf("%d\n", i);</syntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">For i = 10 To 0 Step -1
Debug.Print i
Next i</syntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 2,251 ⟶ 2,452:
endmodule
</syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang="vbnet">For i = 10 To 0 Step -1
Console.WriteLine(i)
Next</syntaxhighlight>
 
=={{header|V (Vlang)}}==
Line 2,274 ⟶ 2,470:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">for (i in 10..0) System.write("%(i) ")
System.print()</syntaxhighlight>
 
13

edits