Loops/For with a specified step: Difference between revisions
Zig version with for-loop added
Langurmonkey (talk | contribs) (added langur language example) |
(Zig version with for-loop added) |
||
(108 intermediate revisions by 51 users not shown) | |||
Line 2:
[[Category:Simple]]
;Task:
Demonstrate a ''for-loop'' where the step-value is greater than one.
Line 22 ⟶ 24:
* [[Loops/Wrong ranges]]
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">L(i) (1..9).step(2)
print(i)</syntaxhighlight>
{{out}}
<pre>
1
3
5
7
9
</pre>
=={{header|360 Assembly}}==
;Basic - Algol style
The opcode BXH uses 3 registers, one for index one for step and one for limit.
<
LOOPFORS CSECT
USING LOOPFORS,R12
Line 45 ⟶ 60:
XDEC DS CL12 temp for edit
YREGS
END LOOPFORS</
{{out}}
<pre>
Line 52 ⟶ 67:
;Basic - Fortran style
The opcode BXLE uses 3 registers, one for index one for step and one for limit.
<
LA R3,BUF idx=0
LA R5,5 from 5
Line 61 ⟶ 76:
LA R3,4(R3) idx=idx+4
BXLE R5,R6,LOOPJ next j
XPRNT BUF,80 print buffer</
;Structured Macros
<
LA R3,BUF idx=0
LA R5,5 from 5
Line 74 ⟶ 89:
AR R5,R6 i=i+step
ENDDO , next i
XPRNT BUF,80 print buffer</
;Structured Macros HLASM
<
LA R3,BUF idx=0
DO FROM=(R5,5),TO=(R7,25),BY=(R6,5) for i=5 to 25 step 5
Line 83 ⟶ 98:
LA R3,4(R3) idx=idx+4
ENDDO , next i
XPRNT BUF,80 print buffer</
=={{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.
<syntaxhighlight lang="6502asm">define ArrayPointerLo $00 ;define some helpful labels.
define ArrayPointerHi $01
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
setArray: ;Easy6502 doesn't let us define arbitrary bytes so the best option is to fill the range at runtime.
lda #$10
ldx #0
loop_setArray:
sta $1200,x
clc
adc #$10
inx
cpx #$08
bcc loop_setArray
; stores this sequence of hex values starting at $1200: $10 $20 $30 $40 $50 $60 $70 $80
ClearMem: ;clear $D000-$D0FF
lda #0
ldx #0
loop_clearMem:
sta $D000,x
inx
bne loop_clearMem
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; end of prep work, the real code begins here.
lda #$12 ;high byte of array address
sta ArrayPointerHi
lda #$00 ;low byte of array address
sta ArrayPointerLo ;these are used to look up the array rather than hard-code it in.
ldx #$0E ;the loop counter, gets decremented twice per iteration to skip the odd addresses.
ldy #7 ;the index into the source array.
;on the 6502 looping backwards is almost always faster.
loop_fill:
lda (ArrayPointerLo),y ;loads from the array's base address, plus Y
sta $D000,x ;store at $D000+X
dey ;decrement array index
dex
dex ;decrement destination index twice
bpl loop_fill ;if destination index equals #$FF, we are done.
brk ;end of program</syntaxhighlight>
{{out}}
<pre>
d000: 10 00 20 00 30 00 40 00 50 00 60 00 70 00 80 00
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program loopstep64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ MAXI, 20
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "Counter = @ \n" // message result
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x4,0 // init counter
1: // begin loop
mov x0,x4
ldr x1,qAdrsZoneConv // display value
bl conversion10 // call function with 2 parameter (x0,x1)
ldr x0,qAdrszMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at first @ character
bl affichageMess // display message
add x4,x4,2 // increment counter by 2
cmp x4,MAXI //
ble 1b // loop
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC Main()
BYTE i
FOR i=0 TO 70 STEP 7
DO
PrintF("%B ",i)
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/For_with_a_specified_step.png Screenshot from Atari 8-bit computer]
<pre>
0 7 14 21 28 35 42 49 56 63 70
</pre>
=={{header|Ada}}==
Line 92 ⟶ 233:
Looper_3 most closely adheres to the requirements of this task, and achieves this by using a second range for the indices.
<
use Loopers;
Line 156 ⟶ 297:
end Loopers;
</syntaxhighlight>
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<
print( i )
od</
=={{header|Aime}}==
<
i = 0;
Line 173 ⟶ 314:
}
o_newline();</
=={{header|ALGOL 60}}==
Line 214 ⟶ 355:
* '''until'''<sup>(C)</sup> - for late loop termination.
* '''foreach'''<sup>(S)</sup> - for working on arrays in [[wp:Parallel computing|parallel]].
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
for i := 3 step 2 until 9 do write( i )
end.</syntaxhighlight>
=={{header|ALGOL-M}}==
<
INTEGER I;
FOR I := 1 STEP 3 UNTIL 19 DO
WRITE( I );
END</
=={{header|AppleScript
<
log i
end repeat</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
Line 375 ⟶ 516:
</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang
print
]</syntaxhighlight>
{{out}}
<pre>0
Line 390 ⟶ 531:
=={{header|AutoHotkey}}==
<
iterations := 5
step := 10
Line 400 ⟶ 541:
MsgBox, % A_Index
}
ExitApp</
=={{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.
=={{header|AWK}}==
<
for (i= 2; i <= 8; i = i + 2) {
print i
}
print "Ain't never too late!"
}</
−
=={{header|Axe}}==
Axe does not support a step size other than 1. However, one can modify the increment variable inside the loop to accomplish the same task.
This example increments by 2:
<
Disp I▶Dec,i
I++
End</
=={{header|
<syntaxhighlight lang="bait">
fun main() {
// Print all single digit odd numbers
for i := 1; i < 10; i += 2 {
println(i)
}
}
</syntaxhighlight>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<
==={{header|BaCon}}===
This prints all odd digits:
<
FOR i = 1 TO 10 STEP 2
PRINT i
NEXT</
==={{header|Basic|QuickBasic}}===
{{works with|QuickBasic|4.5}}
<
print i; ", ";
next i
print "who do we appreciate?"</
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">for i = 1 to 21 step 2
print i; " ";
next i
end</syntaxhighlight>
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> FOR n = 2 TO 8 STEP 1.5
PRINT n
NEXT</syntaxhighlight>
{{out}}
<pre>
2
3.5
5
6.5
8
</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 for i = 1 to 21 step 2
20 print i;
30 next i</syntaxhighlight>
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 10 STEP 2
20 PRINT I
30 NEXT</syntaxhighlight>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
For i As Integer = 1 To 21 Step 2
Print i; " ";
Next
Print
Sleep</syntaxhighlight>
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=cdd9b10b64ac4d78b75c364061f25641 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount As Short
For siCount = 1 To 50 Step 5
Print "Gambas is great!"
Next
End</syntaxhighlight>
<pre>Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!
Gambas is great!</pre>
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|PC-BASIC|any}}
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 21 STEP 2
20 PRINT I;
30 NEXT I</syntaxhighlight>
==={{header|IS-BASIC}}===
<
110 PRINT I
120 NEXT</
==={{header|
<syntaxhighlight lang="lb">for i = 2 to 8 step 2
print i; ", ";
next i
print "who do we appreciate?"
end</syntaxhighlight>
==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="microsoftsmallbasic">For i = 0 To 100 Step 2
TextWindow.WriteLine(i)
EndFor</syntaxhighlight>
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
{{works with|Run BASIC}}
{{works with|Yabasic}}
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 21 STEP 2
20 PRINT I; " ";
30 NEXT I
40 END</syntaxhighlight>
==={{header|MSX Basic}}===
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 21 STEP 2
20 PRINT I;
30 NEXT I</syntaxhighlight>
==={{header|NS-HUBASIC}}===
<syntaxhighlight lang="ns-hubasic">10 FOR I=1 TO 10 STEP 2
20 PRINT I
30 NEXT</syntaxhighlight>
{{out}}
<pre> 1 3 5 7 9 11 13 15 17 19 21</pre>
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">For i = -15 To 25 Step 5
Debug i
Next i</syntaxhighlight>
{{out}}
<pre>-15
-10
-5
0
5
10
15
20
25</pre>
Decrementing with step
<syntaxhighlight lang="purebasic">For i = 10 To 0 Step -2
Debug i
Next ; i is optional</syntaxhighlight>
{{out}}
<pre>10
8
6
4
2
0</pre>
==={{header|QB64}}===
<syntaxhighlight lang="qbasic">For i% = 0 to 10 Step 2
Print i%
Next 'To be more explicit use "Next i%"
</syntaxhighlight>
{{out}}
A newline is inserted automatically after the Print statement
<pre>0
2
4
6
8
10</pre>
We can also decrement with stepping
<syntaxhighlight lang="qbasic">For i% = 10 to 0 Step -2
Print i%
Next i </syntaxhighlight>
{{out}}
<pre>10
8
6
4
2
9</pre>
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">FOR i = 1 TO 21 STEP 2
PRINT i;
NEXT i</syntaxhighlight>
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">10 FOR I = 1 TO 21 STEP 2
20 PRINT I; " ";
30 NEXT I</syntaxhighlight>
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">for i = 2 to 8 step 2
print i; ", ";
next i
print "who do we appreciate?"</syntaxhighlight>
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
==={{header|smart BASIC}}===
Notice how the ampersand (&) is used to concatenate the variable with the text instead of a semicolon.
<
PRINT n & "..";
NEXT n
PRINT "who do we appreciate?"
END</
==={{header|
Prints numbers from 0 to 100 stepping by 5.
<syntaxhighlight lang="ti83b">:For(I,0,100,5
:Disp I
:End</syntaxhighlight>
==={{header|TI-89 BASIC}}===
<syntaxhighlight lang="ti89b">Local i
For i, 0, 100, 5
Disp i
EndFor</syntaxhighlight>
==={{Header|Tiny BASIC}}===
<syntaxhighlight lang="qbasic"> REM TinyBasic does not have a for-loop construct.
REM Equivalent using conditional jump:
LET i = 1
10 IF i > 21 THEN GOTO 20
PRINT i
LET i = i + 2
GOTO 10
20 END</syntaxhighlight>
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">
FOR i = 1 TO 21 STEP 2
PRINT i; " ";
NEXT i
END
</syntaxhighlight>
{{out}}
<pre>1 3 5 7 9 11 13 15 17 19 21</pre>
Since TrueBasic does not distinguish between integer or real values, we can increment using decimal values as well
<syntaxhighlight lang="qbasic">FOR i = 1 TO 5 STEP .5
PRINT i
NEXT i
END</syntaxhighlight>
{{out}}
<pre>1
1.5
2
2.5
3
3.5
4
4.5
5</pre>
==={{header|Visual Basic}}===
{{works with|Visual Basic|VB6 Standard}}
<syntaxhighlight lang="vb">Sub MyLoop()
For i = 2 To 8 Step 2
Debug.Print i;
Next i
Debug.Print
End Sub</syntaxhighlight>
{{out}}
<pre> 2 4 6 8 </pre>
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .NET|.NET Core 3.0}}
<syntaxhighlight lang="vbnet">Imports System.Console
Module Program
Sub Main()
For i = 2 To 8 Step 2
Write($"{i}, ")
Next
WriteLine("who do we appreciate?")
End Sub
End Module</syntaxhighlight>
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
{{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
Dim i As Integer, buffer As String
buffer = ""
For i = 2 To 8 Step 2
buffer = buffer & i & " "
Next i
Debug.Print(buffer)
End Sub
End Class</syntaxhighlight>
{{out}}
<pre>2 4 6 8 </pre>
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "forby"
DECLARE FUNCTION Entry()
FUNCTION Entry()
FOR i% = 0 TO 100 STEP 2
PRINT FORMAT$("###", i%)
NEXT i%
END FUNCTION
END PROGRAM</syntaxhighlight>
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">for i = 1 to 21 step 2
print i, " ";
next i
print
end</syntaxhighlight>
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="basic">10 FOR l = 2 TO 8 STEP 2
20 PRINT l; ", ";
30 NEXT l
40 PRINT "Who do we appreciate?"</syntaxhighlight>
=={{header|Batch File}}==
<
for /l %%A in (1,2,10) do (
echo %%A
)</
{{Out}}
<pre>>Sample.BAT
Line 473 ⟶ 917:
></pre>
=={{header|bc}}==
<
i
}</
=={{header|Befunge}}==
{{trans|C}}
<
@_^#`9:+2<</
=={{header|C}}==
This prints all odd digits:
<
for(i = 1; i < 10; i += 2)
printf("%d\n", i);</
=={{header|C sharp|C#}}==
<
class Program {
Line 539 ⟶ 946:
Console.WriteLine("who do we appreciate?");
}
}</
=={{header|C++}}==
This prints all odd digits:
<syntaxhighlight lang="cpp">for (int i = 1; i < 10; i += 2)
std::cout << i << std::endl;</syntaxhighlight>
=={{header|C3}}==
Print all odd digits:
<syntaxhighlight lang="c3">for (int i = 1; i < 10; i += 2) io::printfn("%d", i);</syntaxhighlight>
=={{header|Ceylon}}==
<
for(i in (2..8).by(2)) {
Line 548 ⟶ 964:
}
print("who do we appreciate?");
}</
=={{header|Chapel}}==
<syntaxhighlight lang="chapel">
// Can be set on commandline via --N=x
config const N = 3;
for i in 1 .. 10 by N {
writeln(i);
}
</syntaxhighlight>
{{out}}
<pre>
$ ./loopby
1
4
7
10
$ ./loopby --N=4
1
5
9
</pre>
=={{header|ChucK}}==
Chuck style
<syntaxhighlight lang="c">
SinOsc s => dac;
for (0 => int i; i < 2000; 5 +=> i )
{
i => s.freq;
100::ms => now;
}
</syntaxhighlight>
General purpose style:
<syntaxhighlight lang="c">
for (0 => int i; i < 2000; 5 +=> i )
{
<<< i >>>;
}
</syntaxhighlight>
=={{header|Clojure}}==
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.
<
(println i)
(when (< i 10)
Line 559 ⟶ 1,017:
(doseq [i (range 0 12 2)]
(println i))</
=={{header|CLU}}==
<syntaxhighlight lang="clu">% This prints all odd digits
start_up = proc ()
po: stream := stream$primary_output()
for i: int in int$from_to_by(1, 10, 2) do
stream$putl(po, int$unparse(i))
end
end start_up</syntaxhighlight>
=={{header|COBOL}}==
<
PROGRAM-ID. Display-Odd-Nums.
Line 575 ⟶ 1,044:
GOBACK
.</
=={{header|ColdFusion}}==
<
<cfloop from="0" to="99" step="3" index="i">
<Cfoutput>#i#</Cfoutput>
</cfloop>
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(format t "~{~S, ~}who do we appreciate?~%" (loop for i from 2 to 8 by 2 collect i))
</syntaxhighlight>
{{out}}
Line 594 ⟶ 1,063:
</pre>
=== 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
((> n 20)) ; Break condition
(print n)) ; On every loop print value
</syntaxhighlight>
{{out}}
<pre>
0
2
4
8
10
13
15
17
20
</pre>
=={{header|D}}==
<
void main() {
Line 628 ⟶ 1,094:
foreach (i; iota(1, 10, 2))
writeln(i);
}</
{{out}}
<pre>1
Line 642 ⟶ 1,108:
=={{header|Dao}}==
<
# max value: 9
# step: 2
for( i = 1 : 2 : 9 ) io.writeln( i )</
=={{header|Dart}}==
<syntaxhighlight lang="dart">main() {
for (int i = 1; i <= 21; i += 2) print(i);
}</syntaxhighlight>
=={{header|Delphi}}==
Line 651 ⟶ 1,122:
It would have to be simulated using something like a While loop.
<
{$APPTYPE CONSOLE}
var
i: Integer;
Line 663 ⟶ 1,134:
Inc(i, 2);
end;
end.</
{{out}}
<pre>2
Line 672 ⟶ 1,142:
=={{header|Dragon}}==
<
show
}
showln "who do we appreciate?"</
=={{header|DWScript}}==
<
for i := 2 to 8 step 2 do
PrintLn(i);</
{{out}}
Line 689 ⟶ 1,158:
6
8</pre>
=={{header|Dyalect}}==
<syntaxhighlight lang="dyalect">//Prints odd numbers from 1 to 10
for i in 1^2..10 {
print(i)
}</syntaxhighlight>
=={{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.
An ordinary while loop can of course be used:
<syntaxhighlight lang="e">var i := 2
while (i <= 8) {
print(`$i, `)
i += 2
}
println("who do we appreciate?")</
A programmer frequently in need of iteration with an arbitrary step should define an appropriate range object:
<
def range {
to iterate(f) {
Line 720 ⟶ 1,193:
print(`$i, `)
}
println("who do we appreciate?")</
The least efficient, but perhaps convenient, solution is to iterate over successive integers and discard undesired ones:
<
print(`$i, `)
}
println("who do we appreciate?")</
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
# Prints even numbers from 0 to 100
for i = 0 step 2 to 100
print i
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Steps may be integers, float, rationals.
<
(for ((i (in-range 0 15 2))) (write i))
0 2 4 6 8 10 12 14
Line 740 ⟶ 1,221:
(for ((x (in-range 0 15 PI))) (write x))
0 3.141592653589793 6.283185307179586 9.42477796076938 12.566370614359172
</syntaxhighlight>
=={{header|Ela}}==
<
for m s n | n > m = do return ()
Line 751 ⟶ 1,232:
for m s (n+s)
_ = for 10 2 0 ::: IO</
{{out}}
Line 762 ⟶ 1,243:
=={{header|Elena}}==
ELENA
<
{
for(int i := 2
{
console.writeLine
}
}</
=={{header|Elixir}}==
<
def for_step(n, step) do
IO.inspect Enum.take_every(1..n, step)
Line 778 ⟶ 1,259:
end
Loops.for_step(20, 3)</
{{out}}
Line 785 ⟶ 1,266:
</pre>
or
<
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]</
=={{header|
<syntaxhighlight lang="emal">
for int i = 2; i
writeLine("who do we appreciate?")
</syntaxhighlight>
{{out}}
<pre>
2, 4, 6, 8, who do we
</pre>
=={{header|Erlang}}==
<
%% for_loop/4 by Bengt Kleberg.
-module(loop_step).
Line 821 ⟶ 1,297:
for_loop( I+Step, End, Step, Do );
for_loop( _I, _End, _Step, _Do ) -> ok.
</syntaxhighlight>
{{out}}
<pre>
* * ok
</pre>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
FOR N=2 TO 8 STEP 1.5 DO
PRINT(N)
END FOR
</syntaxhighlight>
{{out}}
<pre>
2
3.5
5
6.5
8
</pre>
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
for i = 1 to 10 by 2 do
? i
end for
</syntaxhighlight>
As a note, <code>? something</code> is shorthand for:
<syntaxhighlight lang="euphoria">
print(1, something)
puts(1, "\n")
</syntaxhighlight>
<code>print()</code> differs from <code>puts()</code> in that <code>print()</code> will print out the actual <code>sequence</code> it is given.
If it is given an <code>integer</code>, or an <code>atom</code>
(Any number that is not an <code>integer</code>), it will print those out as-is.
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">for i in 2..2..8 do
printf "%d, " i
printfn "done"</syntaxhighlight>
{{out}}
<pre>2, 4, 6, 8, done
</pre>
=={{header|Factor}}==
Prints odd digits.
<
=={{header|FALSE}}==
<
=={{header|Fantom}}==
<
class Main
{
Line 866 ⟶ 1,366:
}
}
</syntaxhighlight>
=={{header|FBSL}}==
<
DIM n AS INTEGER
Line 878 ⟶ 1,378:
PRINT ", who will we obliterate?"
PAUSE
</syntaxhighlight>
=={{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>
=={{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.
<
=={{header|Forth}}==
<
9 2 do
i .
2 +loop
." who do we appreciate?" cr ;</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
print *, i
end do</
{{works with|Fortran|77 and later}}
<
INTEGER I
Line 907 ⟶ 1,411:
STOP
END</
=={{header|
<syntaxhighlight lang="frink">
for a = 1 to 100 step 5
println[a]
</syntaxhighlight>
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
println[a]
</syntaxhighlight>
=={{header|FutureBasic}}==
<
Str15 s(11)
long i
s(0) = "Somewhere"
Line 949 ⟶ 1,440:
print s(i);
next
HandleEvents
</syntaxhighlight>
{{out}}
<pre>Somewhere over the rainbow
Bluebirds fly.</pre>
=={{header|GML}}==
<syntaxhighlight lang="gml">for(i = 0; i < 10; i += 2)
show_message(string(i))</syntaxhighlight>
=={{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.
<
Print(i, "\n");
od;
Line 991 ⟶ 1,463:
9
11
</syntaxhighlight>
=={{header|Go}}==
This prints all odd digits:
<
fmt.Printf("%d\n", i)
}</
=={{header|Groovy}}==
"for" loop:
<
print "${i} "
}
println "Who do we appreciate?"</
"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.
<
print "${it} "
}
println "Who do we appreciate?"</
{{out}}
Line 1,022 ⟶ 1,490:
=={{header|Haskell}}==
<
main = do forM_ [2,4..8] (\x -> putStr (show x ++ ", "))
putStrLn "who do we appreciate?"</
=={{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.
<syntaxhighlight lang="haxe">class Step {
var end:Int;
var step:Int;
var index:Int;
public inline function new(start:Int, end:Int, step:Int) {
this.index = start;
this.end = end;
this.step = step;
}
public inline function hasNext() return step > 0 ? end >= index : index >= end;
public inline function next() return (index += step) - step;
}
class Main {
static function main() {
for (i in new Step(2, 8, 2)) {
Sys.print('$i ');
}
Sys.println('WHOM do we appreciate? GRAMMAR! GRAMMAR! GRAMMAR!');
}
}</syntaxhighlight>
{{out}}
<pre>2 4 6 8 WHOM do we appreciate? GRAMMAR! GRAMMAR! GRAMMAR!</pre>
=={{header|hexiscript}}==
<
println i
endfor</
=={{header|HicEst}}==
<
WRITE() i
ENDDO</
=={{header|HolyC}}==
This prints all odd digits:
<syntaxhighlight lang="holyc">U8 i;
for (i = 1; i < 10; i += 2)
Print("%d\n", i);</syntaxhighlight>
=={{header|Hy}}==
<syntaxhighlight lang="clojure">(for [i (range 1 10 2)] (print i))</syntaxhighlight>
=={{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.
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="python">
every 1 to 10 by 2 # the simplest case that satisfies the task, step by 2
every 1 to 10 # no
every EXPR1 to EXPR2 by EXPR3 do EXPR4 # general case - EXPRn can be complete expressions including other generators such as to-by, every's do is optional
steps := [2,3,5,7] # a list
Line 1,053 ⟶ 1,560:
every writes( (TO_BY_EXPR) | "\n", " " ) # if you want to see how any of these work
</syntaxhighlight>
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.
Line 1,059 ⟶ 1,566:
If in doubt parenthesize.
=={{
<syntaxhighlight lang="insitux">
(for
(continue))
;or
(loop 5 i
(print (* i 2)))
</syntaxhighlight>
=={{header|Io}}==
<
write(i,", ")
)
write("who do we appreciate?")</
=={{header|J}}==
<
2 4 6 8 who do we appreciate?</
Note that an expression of the form <code>(start, step) (p. i.) count</code> will generate the specified numbers (<code>p.</code> is J's polynomial primitive, <code>i.</code> is J's index generator). So, to generate the above sequence of integers we could have used:
<syntaxhighlight lang=J> 0 2 (p. i.) 5
0 2 4 6 8</syntaxhighlight>
Or, using an "actual" for loop:
<
r=.$0
for_n. 2 * >: i.4 do.
Line 1,084 ⟶ 1,602:
' who do we appreciate?' ,~ ":n
)
2 4 6 8 who do we appreciate?</
That said, note also that J's '''steps''' verb lets us specify how many steps to take:
<
_8 _7 _6 _5 _4 _3 _2 _1 0 1 2 3 4 5 6 7 8
i:8j8
_8 _6 _4 _2 0 2 4 6 8</
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:
<
Example use:
<
2 4 6 8 10 12 14 16 18 20
(#~ 0 = 3&|) 1 thru 20
3 6 9 12 15 18
(#~ 1 = 3&|) 1 thru 20
1 4 7 10 13 16 19</
And, of course, like filtering in any language, this approach supports non-constant step sizes, either by applying a function to each argument individually:
<
2 3 5 7 11 13 17 19</
Or, by inserting a combining function between each value:
<syntaxhighlight lang="j"> (-&{.,])/ 1 thru 20
_10 11 _9 12 _8 13 _7 14 _6 15 _5 16 _4 17 _3 18 _2 19 _1 20</syntaxhighlight>
Other structural approaches can also be viable...
=={{header|Java}}==
<
System.out.print(i + ", ");
}
System.out.println("who do we appreciate?");</
=={{header|JavaScript}}==
<
i;
for (i = 2; i <= 8; i += 2) {
Line 1,124 ⟶ 1,649:
}
output += 'who do we appreciate?';
document.write(output);</
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,130 ⟶ 1,655:
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:
<
// range(iMin, iMax)
// range(iMin, iMax, dI)
Line 1,150 ⟶ 1,675:
console.log(
range(2, 8, 2).join(', ') + ', who do we appreciate ?'
);</
Output:
Line 1,156 ⟶ 1,681:
=={{header|jq}}==
To generate the stream: 2,4,6,8:<
def range(m;n;step): range(0; ((n-m)/step) ) | m + (. * step);
range(2;9;2)</
'''Example''':
<
(""; . + "\($i), ") +
"whom do we appreciate?"</
=={{header|Julia}}==
<
print(i, ", ")
end
println("whom do we appreciate?")</
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
for (i in 1 .. 21 step 2) print("$i ")
}</
{{out}}
Line 1,185 ⟶ 1,710:
=={{header|LabVIEW}}==
{{VI solution|LabVIEW_Loops_For_with_a_specified_step.png}}
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def loops_for_with_a_specified_step
{lambda {:a :b :step}
{if {>= :a :b}
then (end of loop)
else :a {loops_for_with_a_specified_step {+ :a :step} :b :step}}}}
-> loops_for_with_a_specified_step
{loops_for_with_a_specified_step 0 10 2}
-> 0 2 4 6 8 (end of loop)
a more simple way:
{S.map {lambda {:i} :i} {S.serie 0 9 2}}
-> 0 2 4 6 8
</syntaxhighlight>
=={{header|Lang5}}==
<
: >>say.(*) . ;
1 10 2 <range> >>say.</
=={{header|
<
writeln .i
}</
{{out}}
Line 1,204 ⟶ 1,747:
=={{header|Lasso}}==
<
loop_count
'\r' // for formatting
^}</
=={{header|
<syntaxhighlight lang="ldpl"># Display the even numbers up to twenty.
data:
i is number
procedure:
for i from 0 to 21 step 2 do
display i lf
repeat</syntaxhighlight>
{{out}}
<pre>
0
2
4
6
8
10
12
14
16
18
20
</pre>
=={{header|LIL}}==
The '''inc''' command accepts a value to add to the variable, 1 if not specified.
<
{{out}}
<pre># for {set i 1} {$i < 15} {inc i 3} {print $i}
Line 1,232 ⟶ 1,791:
=={{header|Lingo}}==
Lingo loops don't support a "step" parameter, so it has to be implemented manually:
<
repeat with i = 0 to 10
put i
i = i + (step-1)
end repeat</
{{out}}
<pre>
Line 1,246 ⟶ 1,805:
=={{header|Lisaac}}==
<
i.print;
'\n'.print;
};</
=={{header|LiveCode}}==
<
put n after loopn
if n is not 10 then put comma after loopn
end repeat
put loopn</
Output<syntaxhighlight lang
=={{header|Logo}}==
<
=={{header|Lua}}==
<
for i=2,9,2 do
print(i)
end
</syntaxhighlight>
{{out}}
Line 1,276 ⟶ 1,835:
8
</pre>
=={{header|M2000 Interpreter}}==
===A for loop===
Line 1,283 ⟶ 1,843:
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">
Module LoopFor {
Locale 1036
Line 1,304 ⟶ 1,864:
}
LoopFor
</syntaxhighlight>
{{Out}}
<pre>
Line 1,311 ⟶ 1,871:
</pre>
===Iterator step 2===
<syntaxhighlight lang="m2000 interpreter">
a=("A", "B", "C", "D", "E", "F", "Z")
k=Each(a)
Line 1,327 ⟶ 1,887:
}
Print
</syntaxhighlight>
{{out}}
<pre>
Line 1,335 ⟶ 1,895:
=={{header|M4}}==
<
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
Line 1,342 ⟶ 1,902:
for(`x',`1',`5',`3',`x
')
</syntaxhighlight>
{{out}}
Line 1,351 ⟶ 1,911:
=={{header|Maple}}==
<
i;
end do;</
{{out}}
<pre>
Line 1,362 ⟶ 1,922:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Do[
Print
{i, 1, 20, 4}]</
{{out}}
<pre>1
Line 1,372 ⟶ 1,931:
9
13
17</pre>
=={{header|MATLAB}} / {{header|Octave}}==
<
printf('%d\n',k)
end; </
A vectorized version of the code is
<
=={{header|Maxima}}==
<
/* 1
3
5
7 */</
=={{header|MAXScript}}==
<
Output:
<syntaxhighlight lang="maxscript">
0
2
Line 1,402 ⟶ 1,961:
10
OK
</syntaxhighlight>
=={{header|
Printing the even numbers in <tt>[0,10)</tt>:
{{works with|min|0.19.6}}
<syntaxhighlight lang="min">0 (dup 10 >=) 'pop (puts 2 +) () linrec</syntaxhighlight>
=={{header|MiniScript}}==
<
print i
end for</
{{out}}
Line 1,424 ⟶ 1,981:
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 П0 ИП0 3 + П0 1 0 - x#0
02 С/П</
In this example, the step is 3, the lowest value is 1 and the upper limit is 10.
=={{header|Modula-2}}==
<
IMPORT InOut;
Line 1,441 ⟶ 1,998:
InOut.WriteLn
END
END ForBy.</
=={{header|Modula-3}}==
<
IO.Put(Fmt.Int(i) & " ");
END;</
=={{header|MUMPS}}==
<
.WRITE $CHAR(I)," "</
{{out}}
<pre>A D G J M P S V Y \ _ b e h k n q t w z</pre>
=={{header|Nemerle}}==
<
<
=={{header|NetRexx}}==
<
options replace format comments java crossref savelog symbols nobinary
Line 1,480 ⟶ 2,025:
say i_.format(3, 1) || '\0'
end i_
say</
{{out}}
<pre>D:\>java lst
Line 1,491 ⟶ 2,036:
The increment step of the Never ''for'' expression can be simple or complex and need not be contiguous.
<
=={{header|
<syntaxhighlight lang
(println i))</syntaxhighlight>
=={{header|Nim}}==
<syntaxhighlight lang="nim">
for n in 5 .. 9: # 5 to 9 (9-inclusive)
echo n
echo "" # spacer
for n in 5 ..< 9: # 5 to 9 (9-exclusive)
echo n
echo "" # spacer
for n in countup(0, 16, 4): # 0 to 16 step 4
echo n
echo "" # spacer
for n in countdown(16, 0, 4): # 16 to 0 step -4
echo n
</syntaxhighlight>
{{out}}
<pre>
5
6
7
8
9
5
6
7
8
0
4
8
12
16
16
12
8
4
0
</pre>
=={{header|N/t/roff}}==
Works with gnu nroff. Example from groff manual, with minimal modifications.
<syntaxhighlight lang="nroff">
.nr a 0 3
.while (\na < 19) \{\
\n+a
.\}
</syntaxhighlight>
{{out}}
<pre>3 6 9 12 15 18 21
</pre>
=={{header|Nu}}==
Here <code>each {}</code> is used to convert from a range to a list, so that it can be consumed by <code>every</code>
<syntaxhighlight lang="nu">
for i in (0..10 | each {} | every 2) {print $i}
</syntaxhighlight>
{{out}}
<pre>
0
2
4
6
8
10
</pre>
=={{header|Oberon-2}}==
Works with oo2c Version 2
<
MODULE LoopForStep;
IMPORT
Line 1,515 ⟶ 2,134:
END
END LoopForStep.
</syntaxhighlight>
Output:
<pre>
Line 1,527 ⟶ 2,146:
1
</pre>
=={{header|Objeck}}==
<
for(i := 0; i < 10; i += 2;) {
i->PrintLine();
};
</syntaxhighlight>
=={{header|OCaml}}==
<
let rec aux i =
if i <= b then begin
Line 1,552 ⟶ 2,172:
6
8
- : unit = ()</
=={{header|Octave}}==
<
disp(i)
endfor</
=={{header|Oforth}}==
<
=={{header|Openscad}}==
<
for ( l = [3:2:9] ) {
echo (l);
}
echo ("on a double white line.");</
=={{header|Oz}}==
<
{System.show I}
end
{System.show done}
</syntaxhighlight>
=={{header|Panda}}==
Panda doesn't
<
t=to.minus(from).divide(step)
0..t.times(step).plus(from)
/test it for(1 6 2) -> 1 3 5
for(1 3 5)</
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">forstep(n=1,10,2,print(n))</syntaxhighlight>
The <code>forstep</code> construct is actually more powerful.
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}}==
Line 1,599 ⟶ 2,219:
=={{header|Perl}}==
<
print "$i, ";
}
print "who do we appreciate?\n";</
=={{header|
{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: #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: #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|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/Loops/For_with_a_specified_step
by Galileo, 11/2022 #/
include ..\Utilitys.pmt
( 2 8 2 ) for print ", " print endfor
"who do we appreciate?" print</syntaxhighlight>
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?
=== Press any key to exit ===</pre>
=={{header|PHP}}==
<
foreach (range(2, 8, 2) as $i)
echo "$i, ";
echo "who do we appreciate?\n";
?></
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
=={{header|PicoLisp}}==
<
(printsp N) )</
=={{header|Pike}}==
<
for(int i = 2; i <= 16; i=i+2) {
write(i + "\n");
}
}</
=={{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.
<
C :i = 1
*Loop
Line 1,649 ⟶ 2,273:
C :i = i + 2
J ( i < 10 ) :*Loop
END:</
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
declare (n, i) fixed binary;
Line 1,659 ⟶ 2,283:
put skip list (i);
end;
</syntaxhighlight>
=={{header|Plain English}}==
Plain English has only one type of loop: an infinite loop that can be given a conditional break or exit. So there is nothing particularly special about this.
<syntaxhighlight lang="plainenglish">
To run:
Start up.
Put 0 into a number.
Loop.
If the number is greater than 50, break.
Convert the number to a string.
Write the string to the console.
Add 5 to the number.
Repeat.
Wait for the escape key.
Shut down.</syntaxhighlight>
=={{header|PowerShell}}==
<
$i
}</
=={{header|Prolog}}==
If you need a stepping iterator, write one:
<
for(Lo,Hi,Step,Val) :- Step>0, plus(Lo,Step,V), V=<Hi, !, for(V,Hi,Step,Val).
example :-
for(0,10,2,Val), write(Val), write(' '), fail.
example.</
<pre>?- example.
0 2 4 6 8 10
true.</pre>
Adding the following two rules lets you go backwards too:
<
for(Hi,Lo,Step,Val) :- Step<0, plus(Hi,Step,V), Lo=<V, !, for(V,Lo,Step,Val).</
=={{header|Python}}==
{{works with|Python|2.x}}
<
print "%d," % i,
print "who do we appreciate?"</
{{works with|Python|3.x}}
<
print("%d, " % i, end="")
print("who do we appreciate?")</
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
=={{header|Quackery}}==
The step size is specified within the loop, giving many possibilities.
<syntaxhighlight lang="quackery">
20 times [ i^ echo sp
2 step ]
cr
1024 times [ i^ 1+ echo sp
i^ 1+ step ]
cr
1 56 times [ i^ echo sp
i^ swap step ]
cr</syntaxhighlight>
{{Out}}
<pre>0 2 4 6 8 10 12 14 16 18
1 2 4 8 16 32 64 128 256 512 1024
0 1 1 2 3 5 8 13 21 34 55
</pre>
=={{header|R}}==
<
cat(a, ", ")
}
cat("who do we appreciate?\n")</
Here the loop may be done implicitly by first concatenating the string and then printing:
<
=={{header|Racket}}==
<
#lang racket
Line 1,718 ⟶ 2,374:
(printf "~a, " i))
(printf "who do we appreciate?~n")
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2010.07}}
Depending on how you define your terms, this task is either trivial or impossible in Raku. <code>for</code> in Raku doesn't have a step value, it's an iteration operator. It iterates through any iterable object passed to it and sets the topic variable to each iterated value in turn. If you close one eye and squint, I guess you could say it has a step value of 1 (or more accurately, Next) and that isn't really changeable. Whatever iterable you pass to it though can have pretty much any value in pretty much any order you desire, so effectively the "step" value is completely unconstrained.
Examples of iterables (not exhaustive):
* Things that do a positional role:
:* Array
:* List
:* Range
:* Sequence
* Things that do an associative role:
:* Bag
:* BagHash
:* Hash
:* Map
:* Mix
:* MixHash
:* QuantHash
:* Set
:* SetHash
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.)
<syntaxhighlight lang="raku" line>for 2, 4 ... 8 {
print "$_, ";
}
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.
<syntaxhighlight lang="raku" line>.say for rand, *+rand-.5 ... *.abs>2</syntaxhighlight>
{{out|Sample output}}
<pre>0.1594860240843563
-0.11336537297314198
-0.04195945218519992
-0.024844489074366427
-0.20616093727620433
-0.17589258387167517
-0.40547336592612593
0.04561929494516015
0.4886003890463373
0.7843094215547495
0.6413619589945883
1.0694380727281951
1.472290164849169
1.8310404939418325
2.326272380988639
</pre>
For that matter, the iterated object doesn't need to contain numbers.
<syntaxhighlight lang="raku" line>.say for <17/32 π banana 👀 :d(7) 🦋>;</syntaxhighlight>
=={{header|Raven}}==
List of numbers:
<
"who do we appreciate?\n" print</
Range:
<
"who do we appreciate?\n" print</
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?
Line 1,733 ⟶ 2,447:
=={{header|REBOL}}==
<
prin rejoin [i ", "]]
print "who do we appreciate?"</
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
=={{header|REXX}}==
===version 1===
<
say x
end</
{{out}}
<pre>
Line 1,758 ⟶ 2,471:
===version 2===
<
say thing
end</
'''output''' is the same as above.
<br><br>
===version 3===
<
Say v
End
Say '('v'**2) is greater than 30 (30.25)'</
{{output}}
<pre>1
Line 1,778 ⟶ 2,491:
we use step keyword to define step length
in this example we print Even numbers between 0 and 10
<
for i = 0 to 10 step 2 see i + nl next
</syntaxhighlight>
{{out}}
Line 1,792 ⟶ 2,505:
we can use step with double values as well:
<
for i = 0 to 10 step 0.5 see i + nl next
</syntaxhighlight>
{{out}}
<pre>
Line 1,820 ⟶ 2,533:
</pre>
=={{header|RPL}}==
Specific increment is given as an argument to the <code>STEP</code> instruction at the end of each loop. Usually, it is a constant value, but it could be a variable if it makes sense.
≪ 1 10 '''FOR''' j
j
2 '''STEP'''
≫
=={{header|Ruby}}==
<
puts "who do we appreciate?"</
or:
<
puts "who do we appreciate?"</
or:
<
print "#{n}, "
end
puts "who do we appreciate?"</
or:
<syntaxhighlight lang="ruby">for n in 2.step(by: 2, to: 8)
print "#{n}, "
end
puts "who do we appreciate?"</syntaxhighlight>
{{out}}
<pre>
2, 4, 6, 8, who do we appreciate?
</pre>
=={{header|Rust}}==
For Rust 1.28 and later:
<
for i in (2..=8).step_by(2) {
print!("{}", i);
}
println!("who do we appreciate?!");
}</syntaxhighlight>
An alternative which also works in earlier versions of Rust:
<
let mut i = 2;
while i <= 8 {
Line 1,863 ⟶ 2,578:
}
println!("who do we appreciate?!");
}</
=={{header|Salmon}}==
<
print(x, ", ");;
print("who do we appreciate?\n");</
=={{header|SAS}}==
<
do i=1 to 10 by 2;
put i;
end;
run;</
=={{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:
<
loop
i := for!(1, 50, 2);
Line 1,885 ⟶ 2,600:
-- i := 1.stepto!(50, 2);
#OUT + i + "\n";
end;</
(Print all odd numbers from 1 to 50)
=={{header|Scala}}==
<
Alternatively:
<
=={{header|Scheme}}==
The built-in ''for''-like form in Scheme is the ''do'' form:
<
((>= i 9)) ; exit condition
(display i) ; body
(newline))</
Some people prefer to use the recursive-style and more flexible _named let_ form:
<
(cond ((< i 9)
(display i)
(newline)
(loop (+ i 2)))))) ; tail-recursive call, won't create a new stack frame</
You can add to the language by wrapping the loop in a function:
<
(let loop ((i start))
(cond ((< i end)
Line 1,922 ⟶ 2,637:
(lambda (i)
(display i)
(newline)))</
... or in a macro, which allows for making the <code>(lambda)</code> implicit:
<
(syntax-rules ()
((for-loop index start end step body ...)
Line 1,936 ⟶ 2,651:
(for-loop i 2 9 2
(display i)
(newline))</
{{out}}
Line 1,946 ⟶ 2,661:
=={{header|Scilab}}==
{{works with|Scilab|5.5.1}}
<syntaxhighlight lang="text">for i=1:2:10
printf("%d\n",i)
end</
{{out}}
<pre>1
Line 1,957 ⟶ 2,672:
=={{header|Seed7}}==
<syntaxhighlight lang="python">
$ include "seed7_05.s7i";
const proc: main is func
Line 1,963 ⟶ 2,679:
var integer: number is 0;
begin
for number range
writeln(number);
end for;
writeln; # spacer
for number range 10 downto 0 step 2 do
writeln(number);
end for;
end func;
</syntaxhighlight>
{{out}}
<pre>
0
2
4
6
8
10
10
8
6
4
2
0
</pre>
=={{header|Sidef}}==
'''for(;;)''' loop:
<
say i
}</
'''for-in''' loop:
<
say i
}</
'''.each''' method:
<
say i
}</
=={{header|Simula}}==
<
integer i;
for i:=5 step 5 until 25 do outint(i, 5)
end</
=={{header|Slate}}==
<
inform: 'enough with the cheering already!'.</
=={{header|Smalltalk}}==
<
Transcript show: i; show ', '
].
Transcript showCr: 'enough with the cheering already!'</
=={{header|Spin}}==
Line 2,006 ⟶ 2,747:
{{works with|HomeSpun}}
{{works with|OpenSpin}}
<
_clkmode = xtal1 + pll16x
_clkfreq = 80_000_000
Line 2,022 ⟶ 2,763:
waitcnt(_clkfreq + cnt)
ser.stop
cogstop(0)</
{{out}}
<pre>
Line 2,029 ⟶ 2,770:
=={{header|SPL}}==
<
#.output(n)
<</
=={{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.
<
00101000000001100000000000000000 1. c to 20
00101000000000100000000000000000 2. -20 to c
Line 2,059 ⟶ 2,800:
10100000000000000000000000000000 22. 5
00000000000000000000000000000000 23. 0
10110000000000000000000000000000 24. 13</
After executing 1,012 instructions, the computer halts with the correct quotient—77—in the accumulator.
=={{header|Stata}}==
<
display "`i'"
}
Line 2,071 ⟶ 2,812:
5
7
9</
=={{header|Swift}}==
This prints all odd digits:
<
print(i)
}</
Alternately (removed in Swift 3):
<
print(i)
}</
=={{header|Tailspin}}==
Tailspin uses streams not loops
<syntaxhighlight lang="tailspin">
1..9:3 -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>
147
</pre>
=={{header|Tcl}}==
<
puts -nonewline "$i, "
}
puts "enough with the cheering already!"</
=={{header|TorqueScript}}==
<
{
echo(%i);
}</
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
LOOP i=2,9,2
PRINT i
ENDLOOP
</syntaxhighlight>
{{out}}
<pre>
Line 2,126 ⟶ 2,866:
==={{header|Bourne Shell}}===
{{works with|Bourne Shell}}
<
while test $x -le 8; do
done</
{{works with|Bourne Shell}}
{{libheader|jot}}
<
==={{header|
<syntaxhighlight lang="ksh">x=0
while (((x += 2) <= 8))
do
done</syntaxhighlight>
{{works with|ksh93}}
<syntaxhighlight lang="ksh">for x in {2..8..2}
do
print -r "$x"
done</syntaxhighlight>
===Bourne Again Shell===
{{works with|Bourne Again SHell|3}}
<
printf "%d, " $x
done</
{{works with|Bourne Again SHell|4}}
Bash v4.0+ has inbuilt support for setting up a step value
<
do
echo $x
done</
==={{header|C Shell}}===
{{libheader|jot}}
<
echo $x
end</
=={{header|Ursa}}==
{{trans|Python}}
<
for (set i 2) (< i 9) (set i (int (+ i 2)))
out i ", " console
end for
out "who do we appreciate?" endl console</
=={{header|Vala}}==
<
stdout.printf("%d\n", i);</
=={{header|VAX Assembly}}==
<
50 D4 0002 2 clrf r0 ;init to 0.0
0004 3 loop:
Line 2,189 ⟶ 2,926:
000B 6
04 000B 7 ret
000C 8 .end main</
=={{header|VBA}}==
<
For i = 2 To 8 Step 2
Debug.Print i;
Next i
Debug.Print
End Sub</
{{out}}
<pre>
Line 2,210 ⟶ 2,941:
=={{header|VBScript}}==
<
For i = 2 To 8 Step 2
buffer = buffer & i & " "
Next
WScript.Echo buffer</
{{out}}
<pre>2 4 6 8</pre>
=={{header|Vedit macro language}}==
This prints all odd digits in range 1 to 9:
<syntaxhighlight lang="vedit">for (#1 = 1; #1 < 10; #1 += 2) {
Num_Type(#1)
}</syntaxhighlight>
=={{header|Verilog}}==
Imprime todos los números impares
<syntaxhighlight lang="verilog">
module main;
integer i;
initial begin
for(i = 1; i <= 21; i = i + 2) $write(i);
$finish ;
end
endmodule
</syntaxhighlight>
=={{header|Vim Script}}==
<
echo i
endfor</
{{Out}}
Line 2,230 ⟶ 2,983:
10</pre>
=={{header|V (Vlang)}}==
This prints all odd digits:
<syntaxhighlight lang="v (vlang)">for i := 1; i<10; i+=2 {
println(i)
}</syntaxhighlight>
=={{header|
<syntaxhighlight lang="vorpal">for(i = 2, i <= 8, i = i + 2){
i.print()
}</syntaxhighlight>
=={{header|Wart}}==
<syntaxhighlight lang="wart">for i 2 (i <= 8) (i <- i+2)
prn i</syntaxhighlight>
=={{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.
<syntaxhighlight lang="wren">// Print odd numbers under 20.
for (i in 1..10) {
var j = 2*i - 1
System.write("%(j) ")
}
System.print("\n")
// Do the same using a 'while' loop.
var k = 1
while (k < 20) {
System.write("%(k) ")
k = k + 2
}
System.print()</syntaxhighlight>
{{out}}
<pre>
1 3 5 7 9 11 13 15 17 19
</pre>
<br>
{{libheader|Wren-iterate}}
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="wren">import "./iterate" for Stepped
// Print odd numbers under 20.
for (i in Stepped.new(1..20, 2)) System.write("%(i) ")
System.print()
// Print first plus every third element thereafter.
for (i in Stepped.new(1..20, 3)) System.write("%(i) ")
System.print()</syntaxhighlight>
{{out}}
<pre>
1 3 5 7 9 11 13 15 17 19
1 4 7 10 13 16 19
</pre>
=={{header|XPL0}}==
Line 2,275 ⟶ 3,045:
thus a step by 2 can be implemented like this:
<
int I;
[for I:= 2 to 8 do
Line 2,282 ⟶ 3,052:
];
Text(0, "who do we appreciate?");
]</
{{out}}
Line 2,290 ⟶ 3,060:
=={{header|zkl}}==
<
[1..10,3].pump(Console.println)</
{{out}}
<pre>
Line 2,304 ⟶ 3,074:
</pre>
A few others:
<
(0).pump(10,Console.println,fcn(n){if(n%2)return(Void.Skip); n})</
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
pub fn main() !void {
const stdout_wr = std.io.getStdOut().writer();
var i: u8 = 1;
while (i < 10) : (i += 2)
try stdout_wr.print("{d}\n", .{i});
}
</syntaxhighlight>
===With for-loop===
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = @import("std").io.getStdOut().writer();
pub fn main() !void {
for (1..10) |n| {
if (n % 2 == 0) continue;
try stdout.print("{d}\n", .{n});
}
}
</syntaxhighlight>
{{out}}
<pre>
1
3
5
7
9
</pre>
|