Loops/For with a specified step: Difference between revisions

Zig version with for-loop added
(→‎{{header|Ruby}}: add sample using named arguments)
(Zig version with for-loop added)
 
(29 intermediate revisions by 16 users not shown)
Line 565:
I++
End</syntaxhighlight>
 
=={{header|Bait}}==
<syntaxhighlight lang="bait">
fun main() {
// Print all single digit odd numbers
for i := 1; i < 10; i += 2 {
println(i)
}
}
</syntaxhighlight>
 
=={{header|BASIC}}==
Line 602 ⟶ 612:
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}}===
Line 608 ⟶ 624:
30 NEXT</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Line 616 ⟶ 632:
Print
Sleep</syntaxhighlight>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">
include "ConsoleWindow"
 
dim as Str15 s(11)
dim as long i
 
s(0) = "Somewhere"
s(2) = " over"
s(4) = " the"
s(6) = " rainbow" + chr$(13)
s(8) = "Bluebirds"
s(10) = " fly."
 
for i = 0 to 10 step 2
print s(i);
next
</syntaxhighlight>
Output:
<pre>
Somewhere over the rainbow
Bluebirds fly.
</pre>
 
==={{header|Gambas}}===
Line 651 ⟶ 643:
 
End</syntaxhighlight>
<pre>Gambas is great!
Gambas is great!
Gambas is great!
Line 660 ⟶ 652:
Gambas is great!
Gambas is great!
Gambas is great!</pre>
 
Gambas is great!
==={{header|GW-BASIC}}===
</pre>
{{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}}===
Line 670 ⟶ 667:
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">for i = 2 to 8 step 2
for i = 2 to 8 step 2
print i; ", ";
next i
print "who do we appreciate?"
end</syntaxhighlight>
end
</syntaxhighlight>
 
==={{header|Microsoft Small Basic}}===
<syntaxhighlight lang="microsoftsmallbasic">For i = 0 To 100 Step 2
For i = 0 To 100 Step 2
TextWindow.WriteLine(i)
EndFor</syntaxhighlight>
 
</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}}===
Line 689 ⟶ 704:
20 PRINT I
30 NEXT</syntaxhighlight>
 
{{out}}
<pre> 1 3 5 7 9 11 13 15 17 19 21</pre>
<pre>
1 3 5 7 9 11 13 15 17 19 21
</pre>
 
==={{header|PureBasic}}===
Line 700 ⟶ 712:
Next i</syntaxhighlight>
{{out}}
<pre>-15
-15
-10
-5
Line 709 ⟶ 720:
15
20
25</pre>
</pre>
 
Decrementing with step
Line 717 ⟶ 727:
Next ; i is optional</syntaxhighlight>
{{out}}
<pre>10
10
8
6
4
2
0</pre>
0
</pre>
 
==={{header|QB64}}===
Line 733 ⟶ 741:
{{out}}
A newline is inserted automatically after the Print statement
<pre>0
0
2
4
6
8
10</pre>
</pre>
 
We can also decrement with stepping
<syntaxhighlight lang="qbasic">For i% = 10 to 0 Step -2
Print i%
Next i </syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>10
 
<pre>
10
8
6
4
2
9</pre>
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}}===
Line 786 ⟶ 800:
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}}===
Line 795 ⟶ 820:
</syntaxhighlight>
{{out}}
<pre>1 3 5 7 9 11 13 15 17 19 21</pre>
<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
FOR i = 1 TO 5 STEP .5
PRINT i
NEXT i
END</syntaxhighlight>
END
</syntaxhighlight>
{{out}}
<pre>1
1
1.5
2
Line 816 ⟶ 836:
4
4.5
5</pre>
5
</pre>
 
==={{header|Visual Basic}}===
Line 828 ⟶ 847:
End Sub</syntaxhighlight>
{{out}}
<pre> 2 4 6 8 </pre>
2 4 6 8
</pre>
 
==={{header|Visual Basic .NET}}===
Line 844 ⟶ 861:
End Module</syntaxhighlight>
{{out}}
<pre>2, 4, 6, 8, who do we appreciate?</pre>
<pre>
2, 4, 6, 8, who do we appreciate?
</pre>
 
{{works with|Visual Basic .NET|2011}}
Line 860 ⟶ 875:
End Class</syntaxhighlight>
{{out}}
<pre>2 4 6 8 </pre>
2 4 6 8
</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "forby"
PROGRAM "forby"
 
DECLARE FUNCTION Entry()
Line 876 ⟶ 888:
NEXT i%
END FUNCTION
END PROGRAM</syntaxhighlight>
</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 941 ⟶ 952:
<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}}==
Line 1,097 ⟶ 1,112:
# step: 2
for( i = 1 : 2 : 9 ) io.writeln( i )</syntaxhighlight>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">main() {
for (int i = 1; i <= 21; i += 2) print(i);
}</syntaxhighlight>
 
=={{header|Delphi}}==
Line 1,186 ⟶ 1,206:
# Prints even numbers from 0 to 100
for i = 0 step 2 to 100
print i
.
# Decimal step value
for i = 0 step 1.23 to 100
print i
.
Line 1,227 ⟶ 1,243:
 
=={{header|Elena}}==
ELENA 46.x
<syntaxhighlight lang="elena">public program()
{
for(int i := 2,; i <= 8,; i += 2 )
{
console.writeLine:(i)
}
}</syntaxhighlight>
Line 1,252 ⟶ 1,268:
<syntaxhighlight lang="elixir">iex(1)> Stream.iterate(1, &(&1+2)) |> Enum.take(10)
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
for int i = 2; i <= 8; i+= 2 do write(i + ", ") end
writeLine("who do we appreciate?")
</syntaxhighlight>
{{out}}
<pre>
2, 4, 6, 8, who do we appreciate?
</pre>
 
=={{header|Erlang}}==
Line 1,398 ⟶ 1,424:
println[a]
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
Str15 s(11)
long i
 
s(0) = "Somewhere"
s(2) = " over"
s(4) = " the"
s(6) = " rainbow" + chr$(13)
s(8) = "Bluebirds"
s(10) = " fly."
 
for i = 0 to 10 step 2
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}}==
Line 1,412 ⟶ 1,464:
11
</syntaxhighlight>
 
=={{header|GML}}==
<syntaxhighlight lang="gml">for(i = 0; i < 10; i += 2)
show_message(string(i))</syntaxhighlight>
 
=={{header|Go}}==
Line 1,498 ⟶ 1,546:
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="iconpython">
every 1 to 10 by 2 # the simplest case that satisfies the task, step by 2
 
every 1 to 10 # no toby, step is by 1 by default
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,517 ⟶ 1,565:
In cases where the by is used it might seem more natural to be right associative.
If in doubt parenthesize.
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(for i (range 0 10 2)
(print i)
(continue))
 
;or
(loop 5 i
(print (* i 2)))
</syntaxhighlight>
 
=={{header|Io}}==
Line 1,528 ⟶ 1,588:
2 4 6 8 who do we appreciate?</syntaxhighlight>
 
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:
Or, using an actual for loop:
 
<syntaxhighlight lang=J> 0 2 (p. i.) 5
0 2 4 6 8</syntaxhighlight>
 
Or, using an "actual" for loop:
 
<syntaxhighlight lang="j"> 3 :0''
Line 1,559 ⟶ 1,624:
1 4 7 10 13 16 19</syntaxhighlight>
 
And, of course, like filtering in any language, this approach supports non-constant step sizes, either by applying a function to each argument individually:
 
<syntaxhighlight lang="j"> (#~ 1&p:) 1 thru 20
2 3 5 7 11 13 17 19</syntaxhighlight>
 
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}}==
Line 1,679 ⟶ 1,751:
'\r' // for formatting
^}</syntaxhighlight>
 
=={{header|LDPL}}==
<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}}==
Line 1,946 ⟶ 2,043:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">for x in countup(1, 10, 4): echo x</syntaxhighlight>
 
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>10
1
5
6
9</pre>
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}}==
Line 2,369 ⟶ 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}}==
<syntaxhighlight lang="ruby">2.step(8,2) {|n| print "#{n}, "}
Line 2,502 ⟶ 2,672:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7python">$ include "seed7_05.s7i";
$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,508 ⟶ 2,679:
var integer: number is 0;
begin
for number range 10 to 10 step 2 do # 10 is inclusive
writeln(number);
end for;
 
end func;</syntaxhighlight>
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}}==
Line 2,679 ⟶ 2,875:
<syntaxhighlight lang="bash">for x in `jot - 2 8 2`; do echo $x; done</syntaxhighlight>
 
==={{header|Korn Shellksh}}===
<syntaxhighlight lang="ksh">x=0
{{works with|Korn Shell}}
while (((x += 2) <= 8))
<syntaxhighlight lang="bash">x=2
do
while [[$x -le 8]]; do
print -r "$x"
echo $x
((x=x+2))
done</syntaxhighlight>
{{works with|Korn Shellksh93}}
<syntaxhighlight lang="bashksh">for x= in {2..8..2}
do
while ((x<=8)); do
print -r "$x"
echo $x
((x+=2))
done</syntaxhighlight>
 
Line 2,806 ⟶ 3,000:
=={{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="ecmascriptwren">// Print odd numbers under 20.
for (i in 1..10) {
var j = 2*i - 1
Line 2,828 ⟶ 3,022:
</pre>
<br>
{{libheader|Wren-traititerate}}
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="ecmascriptwren">import "./traititerate" for Stepped
 
// Print odd numbers under 20.
Line 2,884 ⟶ 3,078:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const std = @import("std");
 
pub fn main() !void {
Line 2,891 ⟶ 3,086:
while (i < 10) : (i += 2)
try stdout_wr.print("{d}\n", .{i});
}
}</syntaxhighlight>
</syntaxhighlight>
 
===With for-loop===
{{omit from|GUISS}}
<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>
19

edits