Loops/For with a specified step: Difference between revisions
Content added Content deleted
(add Zig example) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 26: | Line 26: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang="11l">L(i) (1..9).step(2) |
||
print(i)</ |
print(i)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 41: | Line 41: | ||
;Basic - Algol style |
;Basic - Algol style |
||
The opcode BXH uses 3 registers, one for index one for step and one for limit. |
The opcode BXH uses 3 registers, one for index one for step and one for limit. |
||
< |
<syntaxhighlight lang="360asm">* Loops/For with a specified step 12/08/2015 |
||
LOOPFORS CSECT |
LOOPFORS CSECT |
||
USING LOOPFORS,R12 |
USING LOOPFORS,R12 |
||
Line 60: | Line 60: | ||
XDEC DS CL12 temp for edit |
XDEC DS CL12 temp for edit |
||
YREGS |
YREGS |
||
END LOOPFORS</ |
END LOOPFORS</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 67: | Line 67: | ||
;Basic - Fortran style |
;Basic - Fortran style |
||
The opcode BXLE uses 3 registers, one for index one for step and one for limit. |
The opcode BXLE uses 3 registers, one for index one for step and one for limit. |
||
< |
<syntaxhighlight lang="360asm">* == Fortran style ============== test at the end |
||
LA R3,BUF idx=0 |
LA R3,BUF idx=0 |
||
LA R5,5 from 5 |
LA R5,5 from 5 |
||
Line 76: | Line 76: | ||
LA R3,4(R3) idx=idx+4 |
LA R3,4(R3) idx=idx+4 |
||
BXLE R5,R6,LOOPJ next j |
BXLE R5,R6,LOOPJ next j |
||
XPRNT BUF,80 print buffer</ |
XPRNT BUF,80 print buffer</syntaxhighlight> |
||
;Structured Macros |
;Structured Macros |
||
< |
<syntaxhighlight lang="360asm">* == Algol style ================ test at the beginning |
||
LA R3,BUF idx=0 |
LA R3,BUF idx=0 |
||
LA R5,5 from 5 |
LA R5,5 from 5 |
||
Line 89: | Line 89: | ||
AR R5,R6 i=i+step |
AR R5,R6 i=i+step |
||
ENDDO , next i |
ENDDO , next i |
||
XPRNT BUF,80 print buffer</ |
XPRNT BUF,80 print buffer</syntaxhighlight> |
||
;Structured Macros HLASM |
;Structured Macros HLASM |
||
< |
<syntaxhighlight lang="360asm">* == Fortran style ============== test at the end |
||
LA R3,BUF idx=0 |
LA R3,BUF idx=0 |
||
DO FROM=(R5,5),TO=(R7,25),BY=(R6,5) for i=5 to 25 step 5 |
DO FROM=(R5,5),TO=(R7,25),BY=(R6,5) for i=5 to 25 step 5 |
||
Line 98: | Line 98: | ||
LA R3,4(R3) idx=idx+4 |
LA R3,4(R3) idx=idx+4 |
||
ENDDO , next i |
ENDDO , next i |
||
XPRNT BUF,80 print buffer</ |
XPRNT BUF,80 print buffer</syntaxhighlight> |
||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
This loop loads from an array and writes each element to memory addresses $D000, $D002, $D004, $D006, $D008, $D00A, $D00C, $D00E, in ascending order. |
This loop loads from an array and writes each element to memory addresses $D000, $D002, $D004, $D006, $D008, $D00A, $D00C, $D00E, in ascending order. |
||
< |
<syntaxhighlight lang="6502asm">define ArrayPointerLo $00 ;define some helpful labels. |
||
define ArrayPointerHi $01 |
define ArrayPointerHi $01 |
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
Line 147: | Line 147: | ||
bpl loop_fill ;if destination index equals #$FF, we are done. |
bpl loop_fill ;if destination index equals #$FF, we are done. |
||
brk ;end of program</ |
brk ;end of program</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 156: | Line 156: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program loopstep64.s */ |
/* program loopstep64.s */ |
||
Line 209: | Line 209: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Main() |
||
BYTE i |
BYTE i |
||
Line 219: | Line 219: | ||
PrintF("%B ",i) |
PrintF("%B ",i) |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/For_with_a_specified_step.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/For_with_a_specified_step.png Screenshot from Atari 8-bit computer] |
||
Line 233: | Line 233: | ||
Looper_3 most closely adheres to the requirements of this task, and achieves this by using a second range for the indices. |
Looper_3 most closely adheres to the requirements of this task, and achieves this by using a second range for the indices. |
||
< |
<syntaxhighlight lang="ada">with Loopers; |
||
use Loopers; |
use Loopers; |
||
Line 297: | Line 297: | ||
end Loopers; |
end Loopers; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Agena}}== |
=={{header|Agena}}== |
||
Tested with Agena 2.9.5 Win32 |
Tested with Agena 2.9.5 Win32 |
||
< |
<syntaxhighlight lang="agena">for i from 2 to 8 by 2 do |
||
print( i ) |
print( i ) |
||
od</ |
od</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">integer i; |
||
i = 0; |
i = 0; |
||
Line 314: | Line 314: | ||
} |
} |
||
o_newline();</ |
o_newline();</syntaxhighlight> |
||
=={{header|ALGOL 60}}== |
=={{header|ALGOL 60}}== |
||
Line 357: | Line 357: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
for i := 3 step 2 until 9 do write( i ) |
for i := 3 step 2 until 9 do write( i ) |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|ALGOL-M}}== |
=={{header|ALGOL-M}}== |
||
< |
<syntaxhighlight lang="algol">BEGIN |
||
INTEGER I; |
INTEGER I; |
||
FOR I := 1 STEP 3 UNTIL 19 DO |
FOR I := 1 STEP 3 UNTIL 19 DO |
||
WRITE( I ); |
WRITE( I ); |
||
END</ |
END</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">repeat with i from 2 to 10 by 2 |
||
log i |
log i |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 516: | Line 516: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">loop 0..10 .step:2 'i [ |
||
print i |
print i |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 |
<pre>0 |
||
Line 531: | Line 531: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">SetBatchLines, -1 |
||
iterations := 5 |
iterations := 5 |
||
step := 10 |
step := 10 |
||
Line 541: | Line 541: | ||
MsgBox, % A_Index |
MsgBox, % A_Index |
||
} |
} |
||
ExitApp</ |
ExitApp</syntaxhighlight> |
||
=={{header|Avail}}== |
=={{header|Avail}}== |
||
< |
<syntaxhighlight lang="avail">For each i from 0 to 100 by 7 do [Print: “i” ++ " is a multiple of 7!\n";];</syntaxhighlight> |
||
Note the <code>0 to 100 by 7</code> segment 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. |
Note the <code>0 to 100 by 7</code> segment 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. |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
for (i= 2; i <= 8; i = i + 2) { |
for (i= 2; i <= 8; i = i + 2) { |
||
print i |
print i |
||
} |
} |
||
print "Ain't never too late!" |
print "Ain't never too late!" |
||
}</ |
}</syntaxhighlight> |
||
− |
− |
||
Line 561: | Line 561: | ||
This example increments by 2: |
This example increments by 2: |
||
< |
<syntaxhighlight lang="axe">For(I,0,10) |
||
Disp I▶Dec,i |
Disp I▶Dec,i |
||
I++ |
I++ |
||
End</ |
End</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="qbasic">FOR I = 2 TO 8 STEP 2 : PRINT I; ", "; : NEXT I : PRINT "WHO DO WE APPRECIATE?"</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="freebasic"> |
||
FOR i = 1 TO 10 STEP 2 |
FOR i = 1 TO 10 STEP 2 |
||
PRINT i |
PRINT i |
||
NEXT</ |
NEXT</syntaxhighlight> |
||
==={{header|Basic|QuickBasic}}=== |
==={{header|Basic|QuickBasic}}=== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
< |
<syntaxhighlight lang="qbasic">for i = 2 to 8 step 2 |
||
print i; ", "; |
print i; ", "; |
||
next i |
next i |
||
print "who do we appreciate?"</ |
print "who do we appreciate?"</syntaxhighlight> |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
< |
<syntaxhighlight lang="basic256">for i = 1 to 21 step 2 |
||
print i; " "; |
print i; " "; |
||
next i |
next i |
||
end</ |
end</syntaxhighlight> |
||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
< |
<syntaxhighlight lang="bbcbasic"> FOR n = 2 TO 8 STEP 1.5 |
||
PRINT n |
PRINT n |
||
NEXT</ |
NEXT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 604: | Line 604: | ||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
< |
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 10 STEP 2 |
||
20 PRINT I |
20 PRINT I |
||
30 NEXT</ |
30 NEXT</syntaxhighlight> |
||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
For i As Integer = 1 To 21 Step 2 |
For i As Integer = 1 To 21 Step 2 |
||
Line 615: | Line 615: | ||
Next |
Next |
||
Print |
Print |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
==={{header|FutureBasic}}=== |
==={{header|FutureBasic}}=== |
||
< |
<syntaxhighlight lang="futurebasic"> |
||
include "ConsoleWindow" |
include "ConsoleWindow" |
||
Line 634: | Line 634: | ||
print s(i); |
print s(i); |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 643: | Line 643: | ||
==={{header|Gambas}}=== |
==={{header|Gambas}}=== |
||
'''[https://gambas-playground.proko.eu/?gist=cdd9b10b64ac4d78b75c364061f25641 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=cdd9b10b64ac4d78b75c364061f25641 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim siCount As Short |
Dim siCount As Short |
||
Line 650: | Line 650: | ||
Next |
Next |
||
End</ |
End</syntaxhighlight> |
||
<pre> |
<pre> |
||
Gambas is great! |
Gambas is great! |
||
Line 665: | Line 665: | ||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 FOR I=1 TO 10 STEP 2 |
||
110 PRINT I |
110 PRINT I |
||
120 NEXT</ |
120 NEXT</syntaxhighlight> |
||
==={{header|Liberty BASIC}}=== |
==={{header|Liberty BASIC}}=== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
for i = 2 to 8 step 2 |
for i = 2 to 8 step 2 |
||
print i; ", "; |
print i; ", "; |
||
Line 676: | Line 676: | ||
print "who do we appreciate?" |
print "who do we appreciate?" |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Microsoft Small Basic}}=== |
==={{header|Microsoft Small Basic}}=== |
||
< |
<syntaxhighlight lang="microsoftsmallbasic"> |
||
For i = 0 To 100 Step 2 |
For i = 0 To 100 Step 2 |
||
TextWindow.WriteLine(i) |
TextWindow.WriteLine(i) |
||
EndFor |
EndFor |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|NS-HUBASIC}}=== |
==={{header|NS-HUBASIC}}=== |
||
< |
<syntaxhighlight lang="ns-hubasic">10 FOR I=1 TO 10 STEP 2 |
||
20 PRINT I |
20 PRINT I |
||
30 NEXT</ |
30 NEXT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 696: | Line 696: | ||
==={{header|PureBasic}}=== |
==={{header|PureBasic}}=== |
||
< |
<syntaxhighlight lang="purebasic">For i = -15 To 25 Step 5 |
||
Debug i |
Debug i |
||
Next i</ |
Next i</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 713: | Line 713: | ||
Decrementing with step |
Decrementing with step |
||
< |
<syntaxhighlight lang="purebasic">For i = 10 To 0 Step -2 |
||
Debug i |
Debug i |
||
Next ; i is optional</ |
Next ; i is optional</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 727: | Line 727: | ||
==={{header|QB64}}=== |
==={{header|QB64}}=== |
||
< |
<syntaxhighlight lang="qbasic">For i% = 0 to 10 Step 2 |
||
Print i% |
Print i% |
||
Next 'To be more explicit use "Next i%" |
Next 'To be more explicit use "Next i%" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
A newline is inserted automatically after the Print statement |
A newline is inserted automatically after the Print statement |
||
Line 743: | Line 743: | ||
We can also decrement with stepping |
We can also decrement with stepping |
||
< |
<syntaxhighlight lang="qbasic">For i% = 10 to 0 Step -2 |
||
Print i% |
Print i% |
||
Next i |
Next i |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 759: | Line 759: | ||
==={{header|Run BASIC}}=== |
==={{header|Run BASIC}}=== |
||
< |
<syntaxhighlight lang="runbasic">for i = 2 to 8 step 2 |
||
print i; ", "; |
print i; ", "; |
||
next i |
next i |
||
print "who do we appreciate?"</ |
print "who do we appreciate?"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
||
Line 769: | Line 769: | ||
Notice how the ampersand (&) is used to concatenate the variable with the text instead of a semicolon. |
Notice how the ampersand (&) is used to concatenate the variable with the text instead of a semicolon. |
||
< |
<syntaxhighlight lang="qbasic">FOR n = 2 TO 8 STEP 2 |
||
PRINT n & ".."; |
PRINT n & ".."; |
||
NEXT n |
NEXT n |
||
PRINT "who do we appreciate?" |
PRINT "who do we appreciate?" |
||
END</ |
END</syntaxhighlight> |
||
==={{header|TI-83 BASIC}}=== |
==={{header|TI-83 BASIC}}=== |
||
Prints numbers from 0 to 100 stepping by 5. |
Prints numbers from 0 to 100 stepping by 5. |
||
< |
<syntaxhighlight lang="ti83b">:For(I,0,100,5 |
||
:Disp I |
:Disp I |
||
:End</ |
:End</syntaxhighlight> |
||
==={{header|TI-89 BASIC}}=== |
==={{header|TI-89 BASIC}}=== |
||
< |
<syntaxhighlight lang="ti89b">Local i |
||
For i, 0, 100, 5 |
For i, 0, 100, 5 |
||
Disp i |
Disp i |
||
EndFor</ |
EndFor</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
<syntaxhighlight lang="qbasic"> |
|||
<lang Qbasic> |
|||
FOR i = 1 TO 21 STEP 2 |
FOR i = 1 TO 21 STEP 2 |
||
PRINT i; " "; |
PRINT i; " "; |
||
NEXT i |
NEXT i |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 800: | Line 800: | ||
Since TrueBasic does not distinguish between integer or real values, we can increment using decimal values as well |
Since TrueBasic does not distinguish between integer or real values, we can increment using decimal values as well |
||
<syntaxhighlight lang="qbasic"> |
|||
<lang Qbasic> |
|||
FOR i = 1 TO 5 STEP .5 |
FOR i = 1 TO 5 STEP .5 |
||
PRINT i |
PRINT i |
||
NEXT i |
NEXT i |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 821: | Line 821: | ||
==={{header|Visual Basic}}=== |
==={{header|Visual Basic}}=== |
||
{{works with|Visual Basic|VB6 Standard}} |
{{works with|Visual Basic|VB6 Standard}} |
||
< |
<syntaxhighlight lang="vb">Sub MyLoop() |
||
For i = 2 To 8 Step 2 |
For i = 2 To 8 Step 2 |
||
Debug.Print i; |
Debug.Print i; |
||
Next i |
Next i |
||
Debug.Print |
Debug.Print |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 834: | Line 834: | ||
==={{header|Visual Basic .NET}}=== |
==={{header|Visual Basic .NET}}=== |
||
{{works with|Visual Basic .NET|.NET Core 3.0}} |
{{works with|Visual Basic .NET|.NET Core 3.0}} |
||
< |
<syntaxhighlight lang="vbnet">Imports System.Console |
||
Module Program |
Module Program |
||
Sub Main() |
Sub Main() |
||
Line 842: | Line 842: | ||
WriteLine("who do we appreciate?") |
WriteLine("who do we appreciate?") |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 849: | Line 849: | ||
{{works with|Visual Basic .NET|2011}} |
{{works with|Visual Basic .NET|2011}} |
||
< |
<syntaxhighlight lang="vbnet">Public Class FormPG |
||
Private Sub FormPG_Load(sender As Object, e As EventArgs) Handles MyBase.Load |
Private Sub FormPG_Load(sender As Object, e As EventArgs) Handles MyBase.Load |
||
Dim i As Integer, buffer As String |
Dim i As Integer, buffer As String |
||
Line 858: | Line 858: | ||
Debug.Print(buffer) |
Debug.Print(buffer) |
||
End Sub |
End Sub |
||
End Class</ |
End Class</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 866: | Line 866: | ||
==={{header|XBasic}}=== |
==={{header|XBasic}}=== |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
PROGRAM "forby" |
PROGRAM "forby" |
||
Line 877: | Line 877: | ||
END FUNCTION |
END FUNCTION |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang="yabasic">for i = 1 to 21 step 2 |
||
print i, " "; |
print i, " "; |
||
next i |
next i |
||
print |
print |
||
end</ |
end</syntaxhighlight> |
||
==={{header|ZX Spectrum Basic}}=== |
==={{header|ZX Spectrum Basic}}=== |
||
< |
<syntaxhighlight lang="basic">10 FOR l = 2 TO 8 STEP 2 |
||
20 PRINT l; ", "; |
20 PRINT l; ", "; |
||
30 NEXT l |
30 NEXT l |
||
40 PRINT "Who do we appreciate?"</ |
40 PRINT "Who do we appreciate?"</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
for /l %%A in (1,2,10) do ( |
for /l %%A in (1,2,10) do ( |
||
echo %%A |
echo %%A |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>>Sample.BAT |
<pre>>Sample.BAT |
||
Line 908: | Line 908: | ||
=={{header|bc}}== |
=={{header|bc}}== |
||
< |
<syntaxhighlight lang="bc">for (i = 2; i <= 10; i += 2) { |
||
i |
i |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="befunge">1 >:.55+,v |
||
@_^#`9:+2<</ |
@_^#`9:+2<</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="c">int i; |
||
for(i = 1; i < 10; i += 2) |
for(i = 1; i < 10; i += 2) |
||
printf("%d\n", i);</ |
printf("%d\n", i);</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
class Program { |
class Program { |
||
Line 935: | Line 935: | ||
Console.WriteLine("who do we appreciate?"); |
Console.WriteLine("who do we appreciate?"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="cpp">for (int i = 1; i < 10; i += 2) |
||
std::cout << i << std::endl;</ |
std::cout << i << std::endl;</syntaxhighlight> |
||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">shared void run() { |
||
for(i in (2..8).by(2)) { |
for(i in (2..8).by(2)) { |
||
Line 949: | Line 949: | ||
} |
} |
||
print("who do we appreciate?"); |
print("who do we appreciate?"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Chapel}}== |
=={{header|Chapel}}== |
||
< |
<syntaxhighlight lang="chapel"> |
||
// Can be set on commandline via --N=x |
// Can be set on commandline via --N=x |
||
config const N = 3; |
config const N = 3; |
||
Line 959: | Line 959: | ||
writeln(i); |
writeln(i); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 976: | Line 976: | ||
=={{header|ChucK}}== |
=={{header|ChucK}}== |
||
Chuck style |
Chuck style |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
SinOsc s => dac; |
SinOsc s => dac; |
||
Line 984: | Line 984: | ||
100::ms => now; |
100::ms => now; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
General purpose style: |
General purpose style: |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
for (0 => int i; i < 2000; 5 +=> i ) |
for (0 => int i; i < 2000; 5 +=> i ) |
||
{ |
{ |
||
<<< i >>>; |
<<< i >>>; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
The first example here is following the literal specification, but is not idiomatic Clojure code. |
The first example here is following the literal specification, but is not idiomatic Clojure code. |
||
The second example achieves the same effect without explicit looping, and would (I think) be viewed as better code by the Clojure community. |
The second example achieves the same effect without explicit looping, and would (I think) be viewed as better code by the Clojure community. |
||
< |
<syntaxhighlight lang="clojure">(loop [i 0] |
||
(println i) |
(println i) |
||
(when (< i 10) |
(when (< i 10) |
||
Line 1,002: | Line 1,002: | ||
(doseq [i (range 0 12 2)] |
(doseq [i (range 0 12 2)] |
||
(println i))</ |
(println i))</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% This prints all odd digits |
||
start_up = proc () |
start_up = proc () |
||
Line 1,013: | Line 1,013: | ||
stream$putl(po, int$unparse(i)) |
stream$putl(po, int$unparse(i)) |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. Display-Odd-Nums. |
PROGRAM-ID. Display-Odd-Nums. |
||
Line 1,029: | Line 1,029: | ||
GOBACK |
GOBACK |
||
.</ |
.</syntaxhighlight> |
||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
< |
<syntaxhighlight lang="cfm"> |
||
<cfloop from="0" to="99" step="3" index="i"> |
<cfloop from="0" to="99" step="3" index="i"> |
||
<Cfoutput>#i#</Cfoutput> |
<Cfoutput>#i#</Cfoutput> |
||
</cfloop> |
</cfloop> |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(format t "~{~S, ~}who do we appreciate?~%" (loop for i from 2 to 8 by 2 collect i)) |
(format t "~{~S, ~}who do we appreciate?~%" (loop for i from 2 to 8 by 2 collect i)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,049: | Line 1,049: | ||
=== Using DO === |
=== Using DO === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(do ((n 0 (incf n (+ (random 3) 2)))) ; Initialize to 0 and set random step-value 2, 3 or 4 |
(do ((n 0 (incf n (+ (random 3) 2)))) ; Initialize to 0 and set random step-value 2, 3 or 4 |
||
((> n 20)) ; Break condition |
((> n 20)) ; Break condition |
||
(print n)) ; On every loop print value |
(print n)) ; On every loop print value |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,069: | Line 1,069: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.range; |
||
void main() { |
void main() { |
||
Line 1,079: | Line 1,079: | ||
foreach (i; iota(1, 10, 2)) |
foreach (i; iota(1, 10, 2)) |
||
writeln(i); |
writeln(i); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 1,093: | Line 1,093: | ||
=={{header|Dao}}== |
=={{header|Dao}}== |
||
< |
<syntaxhighlight lang="dao"># first value: 1 |
||
# max value: 9 |
# max value: 9 |
||
# step: 2 |
# step: 2 |
||
for( i = 1 : 2 : 9 ) io.writeln( i )</ |
for( i = 1 : 2 : 9 ) io.writeln( i )</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 1,102: | Line 1,102: | ||
It would have to be simulated using something like a While loop. |
It would have to be simulated using something like a While loop. |
||
< |
<syntaxhighlight lang="delphi">program LoopWithStep; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,114: | Line 1,114: | ||
Inc(i, 2); |
Inc(i, 2); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 |
<pre>2 |
||
Line 1,122: | Line 1,122: | ||
=={{header|Dragon}}== |
=={{header|Dragon}}== |
||
< |
<syntaxhighlight lang="dragon">for(i = 2, i <= 8,i += 2){ |
||
show ", " + i |
show ", " + i |
||
} |
} |
||
showln "who do we appreciate?"</ |
showln "who do we appreciate?"</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">var i : Integer; |
||
for i := 2 to 8 step 2 do |
for i := 2 to 8 step 2 do |
||
PrintLn(i);</ |
PrintLn(i);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,140: | Line 1,140: | ||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang="dyalect">//Prints odd numbers from 1 to 10 |
||
for i in 1^2..10 { |
for i in 1^2..10 { |
||
print(i) |
print(i) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
There is no step in the standard numeric range object (a..b and a..!b) in E, which is typically used for numeric iteration. |
There is no step in the standard numeric range object (a..b and a..!b) in E, which is typically used for numeric iteration. |
||
An ordinary while loop can of course be used: |
An ordinary while loop can of course be used: |
||
< |
<syntaxhighlight lang="e">var i := 2 |
||
while (i <= 8) { |
while (i <= 8) { |
||
print(`$i, `) |
print(`$i, `) |
||
i += 2 |
i += 2 |
||
} |
} |
||
println("who do we appreciate?")</ |
println("who do we appreciate?")</syntaxhighlight> |
||
A programmer frequently in need of iteration with an arbitrary step should define an appropriate range object: |
A programmer frequently in need of iteration with an arbitrary step should define an appropriate range object: |
||
< |
<syntaxhighlight lang="e">def stepRange(low, high, step) { |
||
def range { |
def range { |
||
to iterate(f) { |
to iterate(f) { |
||
Line 1,173: | Line 1,173: | ||
print(`$i, `) |
print(`$i, `) |
||
} |
} |
||
println("who do we appreciate?")</ |
println("who do we appreciate?")</syntaxhighlight> |
||
The least efficient, but perhaps convenient, solution is to iterate over successive integers and discard undesired ones: |
The least efficient, but perhaps convenient, solution is to iterate over successive integers and discard undesired ones: |
||
< |
<syntaxhighlight lang="e">for i ? (i %% 2 <=> 0) in 2..8 { |
||
print(`$i, `) |
print(`$i, `) |
||
} |
} |
||
println("who do we appreciate?")</ |
println("who do we appreciate?")</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
Steps may be integers, float, rationals. |
Steps may be integers, float, rationals. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(for ((i (in-range 0 15 2))) (write i)) |
(for ((i (in-range 0 15 2))) (write i)) |
||
0 2 4 6 8 10 12 14 |
0 2 4 6 8 10 12 14 |
||
Line 1,193: | Line 1,193: | ||
(for ((x (in-range 0 15 PI))) (write x)) |
(for ((x (in-range 0 15 PI))) (write x)) |
||
0 3.141592653589793 6.283185307179586 9.42477796076938 12.566370614359172 |
0 3.141592653589793 6.283185307179586 9.42477796076938 12.566370614359172 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ela}}== |
=={{header|Ela}}== |
||
< |
<syntaxhighlight lang="ela">open monad io |
||
for m s n | n > m = do return () |
for m s n | n > m = do return () |
||
Line 1,204: | Line 1,204: | ||
for m s (n+s) |
for m s (n+s) |
||
_ = for 10 2 0 ::: IO</ |
_ = for 10 2 0 ::: IO</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,216: | Line 1,216: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x |
ELENA 4.x |
||
< |
<syntaxhighlight lang="elena">public program() |
||
{ |
{ |
||
for(int i := 2, i <= 8, i += 2 ) |
for(int i := 2, i <= 8, i += 2 ) |
||
Line 1,222: | Line 1,222: | ||
console.writeLine:i |
console.writeLine:i |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Loops do |
||
def for_step(n, step) do |
def for_step(n, step) do |
||
IO.inspect Enum.take_every(1..n, step) |
IO.inspect Enum.take_every(1..n, step) |
||
Line 1,231: | Line 1,231: | ||
end |
end |
||
Loops.for_step(20, 3)</ |
Loops.for_step(20, 3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,238: | Line 1,238: | ||
</pre> |
</pre> |
||
or |
or |
||
< |
<syntaxhighlight lang="elixir">iex(1)> Stream.iterate(1, &(&1+2)) |> Enum.take(10) |
||
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]</ |
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">%% Implemented by Arjun Sunel |
||
%% for_loop/4 by Bengt Kleberg. |
%% for_loop/4 by Bengt Kleberg. |
||
-module(loop_step). |
-module(loop_step). |
||
Line 1,259: | Line 1,259: | ||
for_loop( I+Step, End, Step, Do ); |
for_loop( I+Step, End, Step, Do ); |
||
for_loop( _I, _End, _Step, _Do ) -> ok. |
for_loop( _I, _End, _Step, _Do ) -> ok. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,267: | Line 1,267: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
FOR N=2 TO 8 STEP 1.5 DO |
FOR N=2 TO 8 STEP 1.5 DO |
||
PRINT(N) |
PRINT(N) |
||
END FOR |
END FOR |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,283: | Line 1,283: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
for i = 1 to 10 by 2 do |
for i = 1 to 10 by 2 do |
||
? i |
? i |
||
end for |
end for |
||
</syntaxhighlight> |
|||
</lang> |
|||
As a note, <code>? something</code> is shorthand for: |
As a note, <code>? something</code> is shorthand for: |
||
<syntaxhighlight lang="euphoria"> |
|||
<lang Euphoria> |
|||
print(1, something) |
print(1, something) |
||
puts(1, "\n") |
puts(1, "\n") |
||
</syntaxhighlight> |
|||
</lang> |
|||
<code>print()</code> differs from <code>puts()</code> in that <code>print()</code> will print out the actual <code>sequence</code> it is given. |
<code>print()</code> differs from <code>puts()</code> in that <code>print()</code> will print out the actual <code>sequence</code> it is given. |
||
Line 1,299: | Line 1,299: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">for i in 2..2..8 do |
||
printf "%d, " i |
printf "%d, " i |
||
printfn "done"</ |
printfn "done"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,309: | Line 1,309: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Prints odd digits. |
Prints odd digits. |
||
< |
<syntaxhighlight lang="factor">1 10 2 <range> [ . ] each</syntaxhighlight> |
||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
< |
<syntaxhighlight lang="false">2[$9\>][$.", "2+]#"who do we appreciate!"</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 1,328: | Line 1,328: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FBSL}}== |
=={{header|FBSL}}== |
||
< |
<syntaxhighlight lang="qbasic">#APPTYPE CONSOLE |
||
DIM n AS INTEGER |
DIM n AS INTEGER |
||
Line 1,340: | Line 1,340: | ||
PRINT ", who will we obliterate?" |
PRINT ", who will we obliterate?" |
||
PAUSE |
PAUSE |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">for i = 1 to 100 by 13 do !i;!' '; od</syntaxhighlight> |
||
{{out}}<pre>1 14 27 40 53 66 79 92</pre> |
{{out}}<pre>1 14 27 40 53 66 79 92</pre> |
||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
If a <tt>FOR</tt> statement has three parameters, they are (in order) the start, the step, and the end; if only two parameters are supplied, they are taken to be the start and the end. The step is then set to 1. |
If a <tt>FOR</tt> statement has three parameters, they are (in order) the start, the step, and the end; if only two parameters are supplied, they are taken to be the start and the end. The step is then set to 1. |
||
< |
<syntaxhighlight lang="focal">FOR I = 1,3,10; TYPE I, !</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: test |
||
9 2 do |
9 2 do |
||
i . |
i . |
||
2 +loop |
2 +loop |
||
." who do we appreciate?" cr ;</ |
." who do we appreciate?" cr ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">do i = 1,10,2 |
||
print *, i |
print *, i |
||
end do</ |
end do</syntaxhighlight> |
||
{{works with|Fortran|77 and later}} |
{{works with|Fortran|77 and later}} |
||
< |
<syntaxhighlight lang="fortran"> PROGRAM STEPFOR |
||
INTEGER I |
INTEGER I |
||
Line 1,373: | Line 1,373: | ||
STOP |
STOP |
||
END</ |
END</syntaxhighlight> |
||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink"> |
||
for a = 1 to 100 step 5 |
for a = 1 to 100 step 5 |
||
println[a] |
println[a] |
||
</syntaxhighlight> |
|||
</lang> |
|||
All values may have units of measure, in which case a specified step is required: |
All values may have units of measure, in which case a specified step is required: |
||
< |
<syntaxhighlight lang="frink"> |
||
for a = 1 km to 3 km step 1 meter |
for a = 1 km to 3 km step 1 meter |
||
println[a] |
println[a] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
# Use a range [a, b .. c], where the step is b-a (b is the value following a), and c-a must be a multiple of the step. |
# Use a range [a, b .. c], where the step is b-a (b is the value following a), and c-a must be a multiple of the step. |
||
< |
<syntaxhighlight lang="gap">for i in [1, 3 .. 11] do |
||
Print(i, "\n"); |
Print(i, "\n"); |
||
od; |
od; |
||
Line 1,399: | Line 1,399: | ||
9 |
9 |
||
11 |
11 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|GML}}== |
=={{header|GML}}== |
||
< |
<syntaxhighlight lang="gml">for(i = 0; i < 10; i += 2) |
||
show_message(string(i))</ |
show_message(string(i))</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="go">for i := 1; i < 10; i += 2 { |
||
fmt.Printf("%d\n", i) |
fmt.Printf("%d\n", i) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
"for" loop: |
"for" loop: |
||
< |
<syntaxhighlight lang="groovy">for(i in (2..9).step(2)) { |
||
print "${i} " |
print "${i} " |
||
} |
} |
||
println "Who do we appreciate?"</ |
println "Who do we appreciate?"</syntaxhighlight> |
||
"each() method: |
"each() method: |
||
Though technically not a loop, most Groovy programmers would use the slightly more terse "each()" method on the collection itself, instead of a "for" loop. |
Though technically not a loop, most Groovy programmers would use the slightly more terse "each()" method on the collection itself, instead of a "for" loop. |
||
< |
<syntaxhighlight lang="groovy">(2..9).step(2).each { |
||
print "${it} " |
print "${it} " |
||
} |
} |
||
println "Who do we appreciate?"</ |
println "Who do we appreciate?"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,430: | Line 1,430: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Control.Monad (forM_) |
||
main = do forM_ [2,4..8] (\x -> putStr (show x ++ ", ")) |
main = do forM_ [2,4..8] (\x -> putStr (show x ++ ", ")) |
||
putStrLn "who do we appreciate?"</ |
putStrLn "who do we appreciate?"</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
While Haxe's for-loop does not allow you to directly specify the step size, it is easy to create an iterator that allows you to do that. |
While Haxe's for-loop does not allow you to directly specify the step size, it is easy to create an iterator that allows you to do that. |
||
< |
<syntaxhighlight lang="haxe">class Step { |
||
var end:Int; |
var end:Int; |
||
var step:Int; |
var step:Int; |
||
Line 1,459: | Line 1,459: | ||
Sys.println('WHOM do we appreciate? GRAMMAR! GRAMMAR! GRAMMAR!'); |
Sys.println('WHOM do we appreciate? GRAMMAR! GRAMMAR! GRAMMAR!'); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,465: | Line 1,465: | ||
=={{header|hexiscript}}== |
=={{header|hexiscript}}== |
||
< |
<syntaxhighlight lang="hexiscript">for let i 0; i <= 50; let i (i + 5) |
||
println i |
println i |
||
endfor</ |
endfor</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">DO i = 1, 6, 1.25 ! from 1 to 6 step 1.25 |
||
WRITE() i |
WRITE() i |
||
ENDDO</ |
ENDDO</syntaxhighlight> |
||
=={{header|HolyC}}== |
=={{header|HolyC}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="holyc">U8 i; |
||
for (i = 1; i < 10; i += 2) |
for (i = 1; i < 10; i += 2) |
||
Print("%d\n", i);</ |
Print("%d\n", i);</syntaxhighlight> |
||
=={{header|Hy}}== |
=={{header|Hy}}== |
||
< |
<syntaxhighlight lang="clojure">(for [i (range 1 10 2)] (print i))</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
Icon and Unicon accomplish loop stepping through the use of a generator, the ternary operator to-by, and the every clause which forces a generator to consume all of its results. |
Icon and Unicon accomplish loop stepping through the use of a generator, the ternary operator to-by, and the every clause which forces a generator to consume all of its results. |
||
Because to-by is an operator it has precedence (just higher than assignments) and associativity (left) and can be combined with other operators. |
Because to-by is an operator it has precedence (just higher than assignments) and associativity (left) and can be combined with other operators. |
||
<syntaxhighlight lang="icon"> |
|||
<lang Icon> |
|||
every 1 to 10 by 2 # the simplest case that satisfies the task, step by 2 |
every 1 to 10 by 2 # the simplest case that satisfies the task, step by 2 |
||
Line 1,500: | Line 1,500: | ||
every writes( (TO_BY_EXPR) | "\n", " " ) # if you want to see how any of these work |
every writes( (TO_BY_EXPR) | "\n", " " ) # if you want to see how any of these work |
||
</syntaxhighlight> |
|||
</lang> |
|||
The ability to combine to-by arbitrarily is quite powerful. |
The ability to combine to-by arbitrarily is quite powerful. |
||
Yet it can lead to unexpected results. In cases of combined to-by operators the left associativity seems natural where the by is omitted. |
Yet it can lead to unexpected results. In cases of combined to-by operators the left associativity seems natural where the by is omitted. |
||
Line 1,507: | Line 1,507: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">for(i,2,8,2, |
||
write(i,", ") |
write(i,", ") |
||
) |
) |
||
write("who do we appreciate?")</ |
write("who do we appreciate?")</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j"> ' who do we appreciate?' ,~ ": 2 * >: i.4 |
||
2 4 6 8 who do we appreciate?</ |
2 4 6 8 who do we appreciate?</syntaxhighlight> |
||
Or, using an actual for loop: |
Or, using an actual for loop: |
||
< |
<syntaxhighlight lang="j"> 3 :0'' |
||
r=.$0 |
r=.$0 |
||
for_n. 2 * >: i.4 do. |
for_n. 2 * >: i.4 do. |
||
Line 1,525: | Line 1,525: | ||
' who do we appreciate?' ,~ ":n |
' who do we appreciate?' ,~ ":n |
||
) |
) |
||
2 4 6 8 who do we appreciate?</ |
2 4 6 8 who do we appreciate?</syntaxhighlight> |
||
That said, note also that J's '''steps''' verb lets us specify how many steps to take: |
That said, note also that J's '''steps''' verb lets us specify how many steps to take: |
||
< |
<syntaxhighlight lang="j"> i:8 |
||
_8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 |
_8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8 |
||
i:8j8 |
i:8j8 |
||
_8 _6 _4 _2 0 2 4 6 8</ |
_8 _6 _4 _2 0 2 4 6 8</syntaxhighlight> |
||
Or, if we prefer, we could borrow the definition of <code>thru</code> from the [[Loops/Downward_for#J|Downward for]] task and then filter for the desired values: |
Or, if we prefer, we could borrow the definition of <code>thru</code> from the [[Loops/Downward_for#J|Downward for]] task and then filter for the desired values: |
||
< |
<syntaxhighlight lang="j"> thru=: <./ + i.@(+*)@-~</syntaxhighlight> |
||
Example use: |
Example use: |
||
< |
<syntaxhighlight lang="j"> (#~ 0 = 2&|) 1 thru 20 |
||
2 4 6 8 10 12 14 16 18 20 |
2 4 6 8 10 12 14 16 18 20 |
||
(#~ 0 = 3&|) 1 thru 20 |
(#~ 0 = 3&|) 1 thru 20 |
||
3 6 9 12 15 18 |
3 6 9 12 15 18 |
||
(#~ 1 = 3&|) 1 thru 20 |
(#~ 1 = 3&|) 1 thru 20 |
||
1 4 7 10 13 16 19</ |
1 4 7 10 13 16 19</syntaxhighlight> |
||
And, of course, like filtering in any language, this approach supports non-constant step sizes: |
And, of course, like filtering in any language, this approach supports non-constant step sizes: |
||
< |
<syntaxhighlight lang="j"> (#~ 1&p:) 1 thru 20 |
||
2 3 5 7 11 13 17 19</ |
2 3 5 7 11 13 17 19</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">for(int i = 2; i <= 8;i += 2){ |
||
System.out.print(i + ", "); |
System.out.print(i + ", "); |
||
} |
} |
||
System.out.println("who do we appreciate?");</ |
System.out.println("who do we appreciate?");</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">var output = '', |
||
i; |
i; |
||
for (i = 2; i <= 8; i += 2) { |
for (i = 2; i <= 8; i += 2) { |
||
Line 1,565: | Line 1,565: | ||
} |
} |
||
output += 'who do we appreciate?'; |
output += 'who do we appreciate?'; |
||
document.write(output);</ |
document.write(output);</syntaxhighlight> |
||
In a functional idiom of JavaScript, however, we will only be able to compose this computation within the superordinate expressions of our program if it has the the form of an expression returning a value, rather than that of a statement which fires off side-effects but returns no value. |
In a functional idiom of JavaScript, however, we will only be able to compose this computation within the superordinate expressions of our program if it has the the form of an expression returning a value, rather than that of a statement which fires off side-effects but returns no value. |
||
Line 1,571: | Line 1,571: | ||
Following the example of languages like Haskell and J on this page, we can begin by generating the stepped series as an expression. In functional JavaScript we will typically replace a state-changing loop with a non-mutating map or fold, writing, for example, something like: |
Following the example of languages like Haskell and J on this page, we can begin by generating the stepped series as an expression. In functional JavaScript we will typically replace a state-changing loop with a non-mutating map or fold, writing, for example, something like: |
||
< |
<syntaxhighlight lang="javascript">// range(iMax) |
||
// range(iMin, iMax) |
// range(iMin, iMax) |
||
// range(iMin, iMax, dI) |
// range(iMin, iMax, dI) |
||
Line 1,591: | Line 1,591: | ||
console.log( |
console.log( |
||
range(2, 8, 2).join(', ') + ', who do we appreciate ?' |
range(2, 8, 2).join(', ') + ', who do we appreciate ?' |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,597: | Line 1,597: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
To generate the stream: 2,4,6,8:< |
To generate the stream: 2,4,6,8:<syntaxhighlight lang="jq"># If your version of jq does not have range/3, use this: |
||
def range(m;n;step): range(0; ((n-m)/step) ) | m + (. * step); |
def range(m;n;step): range(0; ((n-m)/step) ) | m + (. * step); |
||
range(2;9;2)</ |
range(2;9;2)</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">reduce range(2;9;2) as $i |
||
(""; . + "\($i), ") + |
(""; . + "\($i), ") + |
||
"whom do we appreciate?"</ |
"whom do we appreciate?"</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">for i in 2:2:8 |
||
print(i, ", ") |
print(i, ", ") |
||
end |
end |
||
println("whom do we appreciate?")</ |
println("whom do we appreciate?")</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
for (i in 1 .. 21 step 2) print("$i ") |
for (i in 1 .. 21 step 2) print("$i ") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,628: | Line 1,628: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def loops_for_with_a_specified_step |
{def loops_for_with_a_specified_step |
||
{lambda {:a :b :step} |
{lambda {:a :b :step} |
||
Line 1,643: | Line 1,643: | ||
{S.map {lambda {:i} :i} {S.serie 0 9 2}} |
{S.map {lambda {:i} :i} {S.serie 0 9 2}} |
||
-> 0 2 4 6 8 |
-> 0 2 4 6 8 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang="lang5">: <range> over iota swap * rot + tuck swap <= select ; : tuck swap over ; |
||
: >>say.(*) . ; |
: >>say.(*) . ; |
||
1 10 2 <range> >>say.</ |
1 10 2 <range> >>say.</syntaxhighlight> |
||
=={{header|langur}}== |
=={{header|langur}}== |
||
< |
<syntaxhighlight lang="langur">for .i = 1; .i < 10; .i += 2 { |
||
writeln .i |
writeln .i |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,663: | Line 1,663: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">loop(-to=100, -from=1, -by=2) => {^ |
||
loop_count |
loop_count |
||
'\r' // for formatting |
'\r' // for formatting |
||
^}</ |
^}</syntaxhighlight> |
||
=={{header|LIL}}== |
=={{header|LIL}}== |
||
The '''inc''' command accepts a value to add to the variable, 1 if not specified. |
The '''inc''' command accepts a value to add to the variable, 1 if not specified. |
||
< |
<syntaxhighlight lang="tcl">for {set i 1} {$i < 15} {inc i 3} {print $i}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre># for {set i 1} {$i < 15} {inc i 3} {print $i} |
<pre># for {set i 1} {$i < 15} {inc i 3} {print $i} |
||
Line 1,682: | Line 1,682: | ||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
Lingo loops don't support a "step" parameter, so it has to be implemented manually: |
Lingo loops don't support a "step" parameter, so it has to be implemented manually: |
||
< |
<syntaxhighlight lang="lingo">step = 3 |
||
repeat with i = 0 to 10 |
repeat with i = 0 to 10 |
||
put i |
put i |
||
i = i + (step-1) |
i = i + (step-1) |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,696: | Line 1,696: | ||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
< |
<syntaxhighlight lang="lisaac">1.to 9 by 2 do { i : INTEGER; |
||
i.print; |
i.print; |
||
'\n'.print; |
'\n'.print; |
||
};</ |
};</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">repeat with n = 0 to 10 step 2 |
||
put n after loopn |
put n after loopn |
||
if n is not 10 then put comma after loopn |
if n is not 10 then put comma after loopn |
||
end repeat |
end repeat |
||
put loopn</ |
put loopn</syntaxhighlight> |
||
Output<lang |
Output<syntaxhighlight lang="livecode">0,2,4,6,8,10</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">for [i 2 8 2] [type :i type "|, |] print [who do we appreciate?]</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua"> |
||
for i=2,9,2 do |
for i=2,9,2 do |
||
print(i) |
print(i) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,734: | Line 1,734: | ||
For this task we use single float numbers, and we make the loop one time from lower to higher value, and one time form higher to lower value. |
For this task we use single float numbers, and we make the loop one time from lower to higher value, and one time form higher to lower value. |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module LoopFor { |
Module LoopFor { |
||
Locale 1036 |
Locale 1036 |
||
Line 1,755: | Line 1,755: | ||
} |
} |
||
LoopFor |
LoopFor |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,762: | Line 1,762: | ||
</pre> |
</pre> |
||
===Iterator step 2=== |
===Iterator step 2=== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
a=("A", "B", "C", "D", "E", "F", "Z") |
a=("A", "B", "C", "D", "E", "F", "Z") |
||
k=Each(a) |
k=Each(a) |
||
Line 1,778: | Line 1,778: | ||
} |
} |
||
Print |
Print |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,786: | Line 1,786: | ||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">define(`for', |
||
`ifelse($#,0,``$0'', |
`ifelse($#,0,``$0'', |
||
`ifelse(eval($2<=$3),1, |
`ifelse(eval($2<=$3),1, |
||
Line 1,793: | Line 1,793: | ||
for(`x',`1',`5',`3',`x |
for(`x',`1',`5',`3',`x |
||
') |
') |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,802: | Line 1,802: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">for i from 2 to 8 by 2 do |
||
i; |
i; |
||
end do;</ |
end do;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,814: | Line 1,814: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
<syntaxhighlight lang="mathematica">Do[ |
|||
<lang Mathematica>Do[ |
|||
Print[i], |
Print[i], |
||
{i, 1, 20, 4}]</ |
{i, 1, 20, 4}]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 1,826: | Line 1,826: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> for k = 0:10:100, |
||
printf('%d\n',k) |
printf('%d\n',k) |
||
end; </ |
end; </syntaxhighlight> |
||
A vectorized version of the code is |
A vectorized version of the code is |
||
< |
<syntaxhighlight lang="matlab"> printf('%d\n',0:10:100); </syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">for i: 1 step 2 thru 10 do print(i); |
||
/* 1 |
/* 1 |
||
3 |
3 |
||
5 |
5 |
||
7 */</ |
7 */</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
< |
<syntaxhighlight lang="maxscript">for i = 0 to 10 by 2 do format "%\n" i</syntaxhighlight> |
||
Output: |
Output: |
||
<syntaxhighlight lang="maxscript"> |
|||
<lang MAXScript> |
|||
0 |
0 |
||
2 |
2 |
||
Line 1,852: | Line 1,852: | ||
10 |
10 |
||
OK |
OK |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|min}}== |
=={{header|min}}== |
||
Printing the even numbers in <tt>[0,10)</tt>: |
Printing the even numbers in <tt>[0,10)</tt>: |
||
{{works with|min|0.19.6}} |
{{works with|min|0.19.6}} |
||
< |
<syntaxhighlight lang="min">0 (dup 10 >=) 'pop (puts 2 +) () linrec</syntaxhighlight> |
||
=={{header|MiniScript}}== |
=={{header|MiniScript}}== |
||
< |
<syntaxhighlight lang="miniscript">for i in range(1,20,4) |
||
print i |
print i |
||
end for</ |
end for</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,872: | Line 1,872: | ||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>1 П0 ИП0 3 + П0 1 0 - x#0 |
<syntaxhighlight lang="text">1 П0 ИП0 3 + П0 1 0 - x#0 |
||
02 С/П</ |
02 С/П</syntaxhighlight> |
||
In this example, the step is 3, the lowest value is 1 and the upper limit is 10. |
In this example, the step is 3, the lowest value is 1 and the upper limit is 10. |
||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE ForBy; |
||
IMPORT InOut; |
IMPORT InOut; |
||
Line 1,889: | Line 1,889: | ||
InOut.WriteLn |
InOut.WriteLn |
||
END |
END |
||
END ForBy.</ |
END ForBy.</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">FOR i := 1 TO 100 BY 2 DO |
||
IO.Put(Fmt.Int(i) & " "); |
IO.Put(Fmt.Int(i) & " "); |
||
END;</ |
END;</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">FOR I=65:3:122 DO |
||
.WRITE $CHAR(I)," "</ |
.WRITE $CHAR(I)," "</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>A D G J M P S V Y \ _ b e h k n q t w z</pre> |
<pre>A D G J M P S V Y \ _ b e h k n q t w z</pre> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">for (i = 2; i <= 8; i +=2)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="nemerle">foreach (i in [2, 4 .. 8])</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 1,916: | Line 1,916: | ||
say i_.format(3, 1) || '\0' |
say i_.format(3, 1) || '\0' |
||
end i_ |
end i_ |
||
say</ |
say</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>D:\>java lst |
<pre>D:\>java lst |
||
Line 1,927: | Line 1,927: | ||
The increment step of the Never ''for'' expression can be simple or complex and need not be contiguous. |
The increment step of the Never ''for'' expression can be simple or complex and need not be contiguous. |
||
< |
<syntaxhighlight lang="fsharp">for (i = 0; i < 10; i += 3)</syntaxhighlight> |
||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(for (i 0 10 2) |
||
(println i))</ |
(println i))</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">for x in countup(1, 10, 4): echo x</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>10 |
<pre>10 |
||
Line 1,943: | Line 1,943: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c Version 2 |
Works with oo2c Version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE LoopForStep; |
MODULE LoopForStep; |
||
IMPORT |
IMPORT |
||
Line 1,958: | Line 1,958: | ||
END |
END |
||
END LoopForStep. |
END LoopForStep. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,972: | Line 1,972: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
for(i := 0; i < 10; i += 2;) { |
for(i := 0; i < 10; i += 2;) { |
||
i->PrintLine(); |
i->PrintLine(); |
||
}; |
}; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml"># let for_step a b step fn = |
||
let rec aux i = |
let rec aux i = |
||
if i <= b then begin |
if i <= b then begin |
||
Line 1,996: | Line 1,996: | ||
6 |
6 |
||
8 |
8 |
||
- : unit = ()</ |
- : unit = ()</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">for i = 1:2:10 |
||
disp(i) |
disp(i) |
||
endfor</ |
endfor</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth"> 1 100 2 step: i [ i println ]</syntaxhighlight> |
||
=={{header|Openscad}}== |
=={{header|Openscad}}== |
||
< |
<syntaxhighlight lang="openscad">/* Loop from 3 to 9 in steps of 2 */ |
||
for ( l = [3:2:9] ) { |
for ( l = [3:2:9] ) { |
||
echo (l); |
echo (l); |
||
} |
} |
||
echo ("on a double white line.");</ |
echo ("on a double white line.");</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">for I in 2..8;2 do |
||
{System.show I} |
{System.show I} |
||
end |
end |
||
{System.show done} |
{System.show done} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Panda}}== |
=={{header|Panda}}== |
||
Panda doesn't natively have a number generator with steps, so let's add it. |
Panda doesn't natively have a number generator with steps, so let's add it. |
||
< |
<syntaxhighlight lang="panda">fun for(from,to,step) type integer,integer,integer->integer |
||
t=to.minus(from).divide(step) |
t=to.minus(from).divide(step) |
||
0..t.times(step).plus(from) |
0..t.times(step).plus(from) |
||
/test it for(1 6 2) -> 1 3 5 |
/test it for(1 6 2) -> 1 3 5 |
||
for(1 3 5)</ |
for(1 3 5)</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">forstep(n=1,10,2,print(n))</syntaxhighlight> |
||
The <code>forstep</code> construct is actually more powerful. |
The <code>forstep</code> construct is actually more powerful. |
||
For example, to print numbers with last digit relatively prime to 10: |
For example, to print numbers with last digit relatively prime to 10: |
||
< |
<syntaxhighlight lang="parigp">forstep(n=1,100,[2,4,2,2],print(n))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 2,043: | Line 2,043: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">for($i=2; $i <= 8; $i += 2) { |
||
print "$i, "; |
print "$i, "; |
||
} |
} |
||
print "who do we appreciate?\n";</ |
print "who do we appreciate?\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{libheader|Phix/basics}} |
{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;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">8</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d, "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d, "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"who do we appreciate?\n"</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"who do we appreciate?\n"</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
foreach (range(2, 8, 2) as $i) |
foreach (range(2, 8, 2) as $i) |
||
echo "$i, "; |
echo "$i, "; |
||
echo "who do we appreciate?\n"; |
echo "who do we appreciate?\n"; |
||
?></ |
?></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
<pre>2, 4, 6, 8, who do we appreciate?</pre> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(for (N 1 (> 10 N) (+ N 2)) |
||
(printsp N) )</ |
(printsp N) )</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int main() { |
||
for(int i = 2; i <= 16; i=i+2) { |
for(int i = 2; i <= 16; i=i+2) { |
||
write(i + "\n"); |
write(i + "\n"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PILOT}}== |
=={{header|PILOT}}== |
||
One of the advantages of needing to create loops manually by using conditional jumps is that a step of any integer is just as easy as a step of one. |
One of the advantages of needing to create loops manually by using conditional jumps is that a step of any integer is just as easy as a step of one. |
||
< |
<syntaxhighlight lang="pilot">R : Prints the odd numbers less than 10. |
||
C :i = 1 |
C :i = 1 |
||
*Loop |
*Loop |
||
Line 2,085: | Line 2,085: | ||
C :i = i + 2 |
C :i = i + 2 |
||
J ( i < 10 ) :*Loop |
J ( i < 10 ) :*Loop |
||
END:</ |
END:</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
declare (n, i) fixed binary; |
declare (n, i) fixed binary; |
||
Line 2,095: | Line 2,095: | ||
put skip list (i); |
put skip list (i); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">for ($i = 0; $i -lt 10; $i += 2) { |
||
$i |
$i |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
If you need a stepping iterator, write one: |
If you need a stepping iterator, write one: |
||
< |
<syntaxhighlight lang="prolog">for(Lo,Hi,Step,Lo) :- Step>0, Lo=<Hi. |
||
for(Lo,Hi,Step,Val) :- Step>0, plus(Lo,Step,V), V=<Hi, !, for(V,Hi,Step,Val). |
for(Lo,Hi,Step,Val) :- Step>0, plus(Lo,Step,V), V=<Hi, !, for(V,Hi,Step,Val). |
||
example :- |
example :- |
||
for(0,10,2,Val), write(Val), write(' '), fail. |
for(0,10,2,Val), write(Val), write(' '), fail. |
||
example.</ |
example.</syntaxhighlight> |
||
<pre>?- example. |
<pre>?- example. |
||
0 2 4 6 8 10 |
0 2 4 6 8 10 |
||
true.</pre> |
true.</pre> |
||
Adding the following two rules lets you go backwards too: |
Adding the following two rules lets you go backwards too: |
||
< |
<syntaxhighlight lang="prolog">for(Hi,Lo,Step,Hi) :- Step<0, Lo=<Hi. |
||
for(Hi,Lo,Step,Val) :- Step<0, plus(Hi,Step,V), Lo=<V, !, for(V,Lo,Step,Val).</ |
for(Hi,Lo,Step,Val) :- Step<0, plus(Hi,Step,V), Lo=<V, !, for(V,Lo,Step,Val).</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.x}} |
{{works with|Python|2.x}} |
||
< |
<syntaxhighlight lang="python">for i in xrange(2, 9, 2): |
||
print "%d," % i, |
print "%d," % i, |
||
print "who do we appreciate?"</ |
print "who do we appreciate?"</syntaxhighlight> |
||
{{works with|Python|3.x}} |
{{works with|Python|3.x}} |
||
< |
<syntaxhighlight lang="python">for i in range(2, 9, 2): |
||
print("%d, " % i, end="") |
print("%d, " % i, end="") |
||
print("who do we appreciate?")</ |
print("who do we appreciate?")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,135: | Line 2,135: | ||
The step size is specified within the loop, giving many possibilities. |
The step size is specified within the loop, giving many possibilities. |
||
<syntaxhighlight lang="quackery"> |
|||
<lang Quackery> |
|||
20 times [ i^ echo sp |
20 times [ i^ echo sp |
||
2 step ] |
2 step ] |
||
Line 2,144: | Line 2,144: | ||
1 56 times [ i^ echo sp |
1 56 times [ i^ echo sp |
||
i^ swap step ] |
i^ swap step ] |
||
cr</ |
cr</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,154: | Line 2,154: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">for(a in seq(2,8,2)) { |
||
cat(a, ", ") |
cat(a, ", ") |
||
} |
} |
||
cat("who do we appreciate?\n")</ |
cat("who do we appreciate?\n")</syntaxhighlight> |
||
Here the loop may be done implicitly by first concatenating the string and then printing: |
Here the loop may be done implicitly by first concatenating the string and then printing: |
||
< |
<syntaxhighlight lang="r">cat(paste(c(seq(2, 8, by=2), "who do we appreciate?\n"), collapse=", "))</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 2,171: | Line 2,171: | ||
(printf "~a, " i)) |
(printf "~a, " i)) |
||
(printf "who do we appreciate?~n") |
(printf "who do we appreciate?~n") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,199: | Line 2,199: | ||
Probably the most straightforward way to do this is with a sequence. With at least two values on the left-hand side, the sequence operator (<code>...</code>) can infer an arithmetic series. (With at least three values, it can infer a geometric sequence, too.) |
Probably the most straightforward way to do this is with a sequence. With at least two values on the left-hand side, the sequence operator (<code>...</code>) can infer an arithmetic series. (With at least three values, it can infer a geometric sequence, too.) |
||
<lang |
<syntaxhighlight lang="raku" line>for 2, 4 ... 8 { |
||
print "$_, "; |
print "$_, "; |
||
} |
} |
||
say 'whom do we appreciate?';</ |
say 'whom do we appreciate?';</syntaxhighlight><!-- "Whom" is infinitely more amusing. --> |
||
But there is nothing constraining the sequence to a constant step. Here's one with a ''random'' step. |
But there is nothing constraining the sequence to a constant step. Here's one with a ''random'' step. |
||
<lang |
<syntaxhighlight lang="raku" line>.say for rand, *+rand-.5 ... *.abs>2</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
Line 2,229: | Line 2,229: | ||
For that matter, the iterated object doesn't need to contain numbers. |
For that matter, the iterated object doesn't need to contain numbers. |
||
<lang |
<syntaxhighlight lang="raku" line>.say for <17/32 π banana 👀 :d(7) 🦋>;</syntaxhighlight> |
||
=={{header|Raven}}== |
=={{header|Raven}}== |
||
List of numbers: |
List of numbers: |
||
< |
<syntaxhighlight lang="raven">[ 2 4 6 8 ] each "%d, " print |
||
"who do we appreciate?\n" print</ |
"who do we appreciate?\n" print</syntaxhighlight> |
||
Range: |
Range: |
||
< |
<syntaxhighlight lang="raven">2 10 2 range each "%d, " print |
||
"who do we appreciate?\n" print</ |
"who do we appreciate?\n" print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2, 4, 6, 8, who do we appreciate? |
<pre>2, 4, 6, 8, who do we appreciate? |
||
Line 2,244: | Line 2,244: | ||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">for i 2 8 2 [ |
||
prin rejoin [i ", "]] |
prin rejoin [i ", "]] |
||
print "who do we appreciate?"</ |
print "who do we appreciate?"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,253: | Line 2,253: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx"> do x=1 to 10 by 1.5 |
||
say x |
say x |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,268: | Line 2,268: | ||
===version 2=== |
===version 2=== |
||
< |
<syntaxhighlight lang="rexx"> do thing=1 by 3/2 to 10 |
||
say thing |
say thing |
||
end</ |
end</syntaxhighlight> |
||
'''output''' is the same as above. |
'''output''' is the same as above. |
||
<br><br> |
<br><br> |
||
===version 3=== |
===version 3=== |
||
< |
<syntaxhighlight lang="rexx">Do v=1 by 3/2 While v**2<30 |
||
Say v |
Say v |
||
End |
End |
||
Say '('v'**2) is greater than 30 (30.25)'</ |
Say '('v'**2) is greater than 30 (30.25)'</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre>1 |
<pre>1 |
||
Line 2,288: | Line 2,288: | ||
we use step keyword to define step length |
we use step keyword to define step length |
||
in this example we print Even numbers between 0 and 10 |
in this example we print Even numbers between 0 and 10 |
||
< |
<syntaxhighlight lang="ring"> |
||
for i = 0 to 10 step 2 see i + nl next |
for i = 0 to 10 step 2 see i + nl next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,302: | Line 2,302: | ||
we can use step with double values as well: |
we can use step with double values as well: |
||
< |
<syntaxhighlight lang="ring"> |
||
for i = 0 to 10 step 0.5 see i + nl next |
for i = 0 to 10 step 0.5 see i + nl next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,331: | Line 2,331: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">2.step(8,2) {|n| print "#{n}, "} |
||
puts "who do we appreciate?"</ |
puts "who do we appreciate?"</syntaxhighlight> |
||
or: |
or: |
||
< |
<syntaxhighlight lang="ruby">(2..8).step(2) {|n| print "#{n}, "} |
||
puts "who do we appreciate?"</ |
puts "who do we appreciate?"</syntaxhighlight> |
||
or: |
or: |
||
< |
<syntaxhighlight lang="ruby">for n in (2..8).step(2) |
||
print "#{n}, " |
print "#{n}, " |
||
end |
end |
||
puts "who do we appreciate?"</ |
puts "who do we appreciate?"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,349: | Line 2,349: | ||
For Rust 1.28 and later: |
For Rust 1.28 and later: |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
for i in (2..=8).step_by(2) { |
for i in (2..=8).step_by(2) { |
||
print!("{}", i); |
print!("{}", i); |
||
} |
} |
||
println!("who do we appreciate?!"); |
println!("who do we appreciate?!"); |
||
}</ |
}</syntaxhighlight> |
||
An alternative which also works in earlier versions of Rust: |
An alternative which also works in earlier versions of Rust: |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let mut i = 2; |
let mut i = 2; |
||
while i <= 8 { |
while i <= 8 { |
||
Line 2,364: | Line 2,364: | ||
} |
} |
||
println!("who do we appreciate?!"); |
println!("who do we appreciate?!"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Salmon}}== |
=={{header|Salmon}}== |
||
< |
<syntaxhighlight lang="salmon">for (x; 2; x <= 8; 2) |
||
print(x, ", ");; |
print(x, ", ");; |
||
print("who do we appreciate?\n");</ |
print("who do we appreciate?\n");</syntaxhighlight> |
||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
< |
<syntaxhighlight lang="sas">data _null_; |
||
do i=1 to 10 by 2; |
do i=1 to 10 by 2; |
||
put i; |
put i; |
||
end; |
end; |
||
run;</ |
run;</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
See [[Loops/For#Sather]]: the implementation for <code>for!</code> allows to specify a step, even though the built-in <code>stepto!</code> can be used; an example of usage could be simply: |
See [[Loops/For#Sather]]: the implementation for <code>for!</code> allows to specify a step, even though the built-in <code>stepto!</code> can be used; an example of usage could be simply: |
||
< |
<syntaxhighlight lang="sather"> i :INT; |
||
loop |
loop |
||
i := for!(1, 50, 2); |
i := for!(1, 50, 2); |
||
Line 2,386: | Line 2,386: | ||
-- i := 1.stepto!(50, 2); |
-- i := 1.stepto!(50, 2); |
||
#OUT + i + "\n"; |
#OUT + i + "\n"; |
||
end;</ |
end;</syntaxhighlight> |
||
(Print all odd numbers from 1 to 50) |
(Print all odd numbers from 1 to 50) |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">for (i <- 2 to 8 by 2) println(i)</syntaxhighlight> |
||
Alternatively: |
Alternatively: |
||
< |
<syntaxhighlight lang="scala">(2 to 8 by 2) foreach println</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
The built-in ''for''-like form in Scheme is the ''do'' form: |
The built-in ''for''-like form in Scheme is the ''do'' form: |
||
< |
<syntaxhighlight lang="scheme">(do ((i 2 (+ i 2))) ; list of variables, initials and steps -- you can iterate over several at once |
||
((>= i 9)) ; exit condition |
((>= i 9)) ; exit condition |
||
(display i) ; body |
(display i) ; body |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
Some people prefer to use the recursive-style and more flexible _named let_ form: |
Some people prefer to use the recursive-style and more flexible _named let_ form: |
||
< |
<syntaxhighlight lang="scheme">(let loop ((i 2)) ; function name, parameters and starting values |
||
(cond ((< i 9) |
(cond ((< i 9) |
||
(display i) |
(display i) |
||
(newline) |
(newline) |
||
(loop (+ i 2)))))) ; tail-recursive call, won't create a new stack frame</ |
(loop (+ i 2)))))) ; tail-recursive call, won't create a new stack frame</syntaxhighlight> |
||
You can add to the language by wrapping the loop in a function: |
You can add to the language by wrapping the loop in a function: |
||
< |
<syntaxhighlight lang="scheme">(define (for-loop start end step func) |
||
(let loop ((i start)) |
(let loop ((i start)) |
||
(cond ((< i end) |
(cond ((< i end) |
||
Line 2,423: | Line 2,423: | ||
(lambda (i) |
(lambda (i) |
||
(display i) |
(display i) |
||
(newline)))</ |
(newline)))</syntaxhighlight> |
||
... or in a macro, which allows for making the <code>(lambda)</code> implicit: |
... or in a macro, which allows for making the <code>(lambda)</code> implicit: |
||
< |
<syntaxhighlight lang="scheme">(define-syntax for-loop |
||
(syntax-rules () |
(syntax-rules () |
||
((for-loop index start end step body ...) |
((for-loop index start end step body ...) |
||
Line 2,437: | Line 2,437: | ||
(for-loop i 2 9 2 |
(for-loop i 2 9 2 |
||
(display i) |
(display i) |
||
(newline))</ |
(newline))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,447: | Line 2,447: | ||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
{{works with|Scilab|5.5.1}} |
{{works with|Scilab|5.5.1}} |
||
<lang>for i=1:2:10 |
<syntaxhighlight lang="text">for i=1:2:10 |
||
printf("%d\n",i) |
printf("%d\n",i) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 2,458: | Line 2,458: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 2,467: | Line 2,467: | ||
writeln(number); |
writeln(number); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
'''for(;;)''' loop: |
'''for(;;)''' loop: |
||
< |
<syntaxhighlight lang="ruby">for (var i = 2; i <= 8; i += 2) { |
||
say i |
say i |
||
}</ |
}</syntaxhighlight> |
||
'''for-in''' loop: |
'''for-in''' loop: |
||
< |
<syntaxhighlight lang="ruby">for i in (2 .. (8, 2)) { |
||
say i |
say i |
||
}</ |
}</syntaxhighlight> |
||
'''.each''' method: |
'''.each''' method: |
||
< |
<syntaxhighlight lang="ruby">2.to(8).by(2).each { |i| |
||
say i |
say i |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">begin |
||
integer i; |
integer i; |
||
for i:=5 step 5 until 25 do outint(i, 5) |
for i:=5 step 5 until 25 do outint(i, 5) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">2 to: 8 by: 2 do: [| :i | Console ; i printString ; ', ']. |
||
inform: 'enough with the cheering already!'.</ |
inform: 'enough with the cheering already!'.</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">2 to: 8 by: 2 do: [ :i | |
||
Transcript show: i; show ', ' |
Transcript show: i; show ', ' |
||
]. |
]. |
||
Transcript showCr: 'enough with the cheering already!'</ |
Transcript showCr: 'enough with the cheering already!'</syntaxhighlight> |
||
=={{header|Spin}}== |
=={{header|Spin}}== |
||
Line 2,507: | Line 2,507: | ||
{{works with|HomeSpun}} |
{{works with|HomeSpun}} |
||
{{works with|OpenSpin}} |
{{works with|OpenSpin}} |
||
< |
<syntaxhighlight lang="spin">con |
||
_clkmode = xtal1 + pll16x |
_clkmode = xtal1 + pll16x |
||
_clkfreq = 80_000_000 |
_clkfreq = 80_000_000 |
||
Line 2,523: | Line 2,523: | ||
waitcnt(_clkfreq + cnt) |
waitcnt(_clkfreq + cnt) |
||
ser.stop |
ser.stop |
||
cogstop(0)</ |
cogstop(0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,530: | Line 2,530: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang="spl">> n, 1..10,2 |
||
#.output(n) |
#.output(n) |
||
<</ |
<</syntaxhighlight> |
||
=={{header|SSEM}}== |
=={{header|SSEM}}== |
||
Implementing loops with a step other than one is precisely as easy (or as fiddly) as implementing loops with a step equal to one. This example program uses a loop to perform integer division. It should be run with the dividend in storage location 21 and the divisor in storage location 22. To show that it works, we shall ask the machine to count from 387 in steps of -5 and to halt with the accumulator showing the number of times it has done so before producing a negative result. |
Implementing loops with a step other than one is precisely as easy (or as fiddly) as implementing loops with a step equal to one. This example program uses a loop to perform integer division. It should be run with the dividend in storage location 21 and the divisor in storage location 22. To show that it works, we shall ask the machine to count from 387 in steps of -5 and to halt with the accumulator showing the number of times it has done so before producing a negative result. |
||
< |
<syntaxhighlight lang="ssem">10101000000000100000000000000000 0. -21 to c |
||
00101000000001100000000000000000 1. c to 20 |
00101000000001100000000000000000 1. c to 20 |
||
00101000000000100000000000000000 2. -20 to c |
00101000000000100000000000000000 2. -20 to c |
||
Line 2,560: | Line 2,560: | ||
10100000000000000000000000000000 22. 5 |
10100000000000000000000000000000 22. 5 |
||
00000000000000000000000000000000 23. 0 |
00000000000000000000000000000000 23. 0 |
||
10110000000000000000000000000000 24. 13</ |
10110000000000000000000000000000 24. 13</syntaxhighlight> |
||
After executing 1,012 instructions, the computer halts with the correct quotient—77—in the accumulator. |
After executing 1,012 instructions, the computer halts with the correct quotient—77—in the accumulator. |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">forvalues i=1(2)10 { |
||
display "`i'" |
display "`i'" |
||
} |
} |
||
Line 2,572: | Line 2,572: | ||
5 |
5 |
||
7 |
7 |
||
9</ |
9</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="swift">for i in stride(from: 1, to: 10, by: 2) { |
||
print(i) |
print(i) |
||
}</ |
}</syntaxhighlight> |
||
Alternately (removed in Swift 3): |
Alternately (removed in Swift 3): |
||
< |
<syntaxhighlight lang="swift">for var i = 1; i < 10; i += 2 { |
||
print(i) |
print(i) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
Tailspin uses streams not loops |
Tailspin uses streams not loops |
||
< |
<syntaxhighlight lang="tailspin"> |
||
1..9:3 -> !OUT::write |
1..9:3 -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,595: | Line 2,595: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">for {set i 2} {$i <= 8} {incr i 2} { |
||
puts -nonewline "$i, " |
puts -nonewline "$i, " |
||
} |
} |
||
puts "enough with the cheering already!"</ |
puts "enough with the cheering already!"</syntaxhighlight> |
||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
< |
<syntaxhighlight lang="torquescript">for(%i = 0; %i < 201; %i += 2) |
||
{ |
{ |
||
echo(%i); |
echo(%i); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
LOOP i=2,9,2 |
LOOP i=2,9,2 |
||
PRINT i |
PRINT i |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,626: | Line 2,626: | ||
==={{header|Bourne Shell}}=== |
==={{header|Bourne Shell}}=== |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
< |
<syntaxhighlight lang="bash">x=2 |
||
while test $x -le 8; do |
while test $x -le 8; do |
||
echo $x |
echo $x |
||
x=`expr $x + 2` || exit $? |
x=`expr $x + 2` || exit $? |
||
done</ |
done</syntaxhighlight> |
||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
{{libheader|jot}} |
{{libheader|jot}} |
||
< |
<syntaxhighlight lang="bash">for x in `jot - 2 8 2`; do echo $x; done</syntaxhighlight> |
||
==={{header|Korn Shell}}=== |
==={{header|Korn Shell}}=== |
||
{{works with|Korn Shell}} |
{{works with|Korn Shell}} |
||
< |
<syntaxhighlight lang="bash">x=2 |
||
while [[$x -le 8]]; do |
while [[$x -le 8]]; do |
||
echo $x |
echo $x |
||
((x=x+2)) |
((x=x+2)) |
||
done</ |
done</syntaxhighlight> |
||
{{works with|Korn Shell}} |
{{works with|Korn Shell}} |
||
< |
<syntaxhighlight lang="bash">x=2 |
||
while ((x<=8)); do |
while ((x<=8)); do |
||
echo $x |
echo $x |
||
((x+=2)) |
((x+=2)) |
||
done</ |
done</syntaxhighlight> |
||
===Bourne Again Shell=== |
===Bourne Again Shell=== |
||
{{works with|Bourne Again SHell|3}} |
{{works with|Bourne Again SHell|3}} |
||
< |
<syntaxhighlight lang="bash">for (( x=2; $x<=8; x=$x+2 )); do |
||
printf "%d, " $x |
printf "%d, " $x |
||
done</ |
done</syntaxhighlight> |
||
{{works with|Bourne Again SHell|4}} |
{{works with|Bourne Again SHell|4}} |
||
Bash v4.0+ has inbuilt support for setting up a step value |
Bash v4.0+ has inbuilt support for setting up a step value |
||
< |
<syntaxhighlight lang="bash">for x in {2..8..2} |
||
do |
do |
||
echo $x |
echo $x |
||
done</ |
done</syntaxhighlight> |
||
==={{header|C Shell}}=== |
==={{header|C Shell}}=== |
||
{{libheader|jot}} |
{{libheader|jot}} |
||
< |
<syntaxhighlight lang="csh">foreach x (`jot - 2 8 2`) |
||
echo $x |
echo $x |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ursa">decl int i |
||
for (set i 2) (< i 9) (set i (int (+ i 2))) |
for (set i 2) (< i 9) (set i (int (+ i 2))) |
||
out i ", " console |
out i ", " console |
||
end for |
end for |
||
out "who do we appreciate?" endl console</ |
out "who do we appreciate?" endl console</syntaxhighlight> |
||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala">for (int i = 1; i < 10; i += 2) |
||
stdout.printf("%d\n", i);</ |
stdout.printf("%d\n", i);</syntaxhighlight> |
||
=={{header|VAX Assembly}}== |
=={{header|VAX Assembly}}== |
||
< |
<syntaxhighlight lang="vax assembly"> 0000 0000 1 .entry main,0 |
||
50 D4 0002 2 clrf r0 ;init to 0.0 |
50 D4 0002 2 clrf r0 ;init to 0.0 |
||
0004 3 loop: |
0004 3 loop: |
||
Line 2,688: | Line 2,688: | ||
000B 6 |
000B 6 |
||
04 000B 7 ret |
04 000B 7 ret |
||
000C 8 .end main</ |
000C 8 .end main</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Sub MyLoop() |
||
For i = 2 To 8 Step 2 |
For i = 2 To 8 Step 2 |
||
Debug.Print i; |
Debug.Print i; |
||
Next i |
Next i |
||
Debug.Print |
Debug.Print |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,703: | Line 2,703: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">buffer = "" |
||
For i = 2 To 8 Step 2 |
For i = 2 To 8 Step 2 |
||
buffer = buffer & i & " " |
buffer = buffer & i & " " |
||
Next |
Next |
||
WScript.Echo buffer</ |
WScript.Echo buffer</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 4 6 8</pre> |
<pre>2 4 6 8</pre> |
||
Line 2,713: | Line 2,713: | ||
=={{header|Vedit macro language}}== |
=={{header|Vedit macro language}}== |
||
This prints all odd digits in range 1 to 9: |
This prints all odd digits in range 1 to 9: |
||
< |
<syntaxhighlight lang="vedit">for (#1 = 1; #1 < 10; #1 += 2) { |
||
Num_Type(#1) |
Num_Type(#1) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Verilog}}== |
=={{header|Verilog}}== |
||
Imprime todos los números impares |
Imprime todos los números impares |
||
<syntaxhighlight lang="verilog"> |
|||
<lang Verilog> |
|||
module main; |
module main; |
||
integer i; |
integer i; |
||
Line 2,730: | Line 2,730: | ||
end |
end |
||
endmodule |
endmodule |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Vim Script}}== |
=={{header|Vim Script}}== |
||
< |
<syntaxhighlight lang="vim">for i in range(2, 10, 2) |
||
echo i |
echo i |
||
endfor</ |
endfor</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,747: | Line 2,747: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
This prints all odd digits: |
This prints all odd digits: |
||
< |
<syntaxhighlight lang="vlang">for i := 1; i<10; i+=2 { |
||
println(i) |
println(i) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Vorpal}}== |
=={{header|Vorpal}}== |
||
< |
<syntaxhighlight lang="vorpal">for(i = 2, i <= 8, i = i + 2){ |
||
i.print() |
i.print() |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
< |
<syntaxhighlight lang="wart">for i 2 (i <= 8) (i <- i+2) |
||
prn i</ |
prn i</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
There is currently no direct way to incorporate a step into a ''for'' loop but we can simulate it by declaring a second variable at the start of the loop which maps the loop variable to the value we want or we can simply use a ''while'' loop instead. |
There is currently no direct way to incorporate a step into a ''for'' loop but we can simulate it by declaring a second variable at the start of the loop which maps the loop variable to the value we want or we can simply use a ''while'' loop instead. |
||
< |
<syntaxhighlight lang="ecmascript">// Print odd numbers under 20. |
||
for (i in 1..10) { |
for (i in 1..10) { |
||
var j = 2*i - 1 |
var j = 2*i - 1 |
||
Line 2,776: | Line 2,776: | ||
k = k + 2 |
k = k + 2 |
||
} |
} |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,786: | Line 2,786: | ||
{{libheader|Wren-trait}} |
{{libheader|Wren-trait}} |
||
A further and more general approach is to use a wrapper class (such as the one in the above module) which can iterate over any sequence in a stepped fashion using Wren's ''iterator protocol''. |
A further and more general approach is to use a wrapper class (such as the one in the above module) which can iterate over any sequence in a stepped fashion using Wren's ''iterator protocol''. |
||
< |
<syntaxhighlight lang="ecmascript">import "/trait" for Stepped |
||
// Print odd numbers under 20. |
// Print odd numbers under 20. |
||
Line 2,794: | Line 2,794: | ||
// Print first plus every third element thereafter. |
// Print first plus every third element thereafter. |
||
for (i in Stepped.new(1..20, 3)) System.write("%(i) ") |
for (i in Stepped.new(1..20, 3)) System.write("%(i) ") |
||
System.print()</ |
System.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,807: | Line 2,807: | ||
thus a step by 2 can be implemented like this: |
thus a step by 2 can be implemented like this: |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
int I; |
int I; |
||
[for I:= 2 to 8 do |
[for I:= 2 to 8 do |
||
Line 2,814: | Line 2,814: | ||
]; |
]; |
||
Text(0, "who do we appreciate?"); |
Text(0, "who do we appreciate?"); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,822: | Line 2,822: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">foreach n in ([1..10,4]) {println(n)} |
||
[1..10,3].pump(Console.println)</ |
[1..10,3].pump(Console.println)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,836: | Line 2,836: | ||
</pre> |
</pre> |
||
A few others: |
A few others: |
||
< |
<syntaxhighlight lang="zkl">fcn loop(i=0){println(i); if(i<10)return(self.fcn(i+2))} |
||
(0).pump(10,Console.println,fcn(n){if(n%2)return(Void.Skip); n})</ |
(0).pump(10,Console.println,fcn(n){if(n%2)return(Void.Skip); n})</syntaxhighlight> |
||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
pub fn main() !void { |
pub fn main() !void { |
||
Line 2,847: | Line 2,847: | ||
while (i < 10) : (i += 2) |
while (i < 10) : (i += 2) |
||
try stdout_wr.print("{d}\n", .{i}); |
try stdout_wr.print("{d}\n", .{i}); |
||
}</ |
}</syntaxhighlight> |
||
{{omit from|GUISS}} |
{{omit from|GUISS}} |