Loops/For with a specified step: Difference between revisions

Zig version with for-loop added
(→‎Insitux: inclusion)
(Zig version with for-loop added)
 
(9 intermediate revisions by 6 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 1,233 ⟶ 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,258 ⟶ 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,526 ⟶ 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 2,023 ⟶ 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}}==
Line 2,040 ⟶ 2,100:
{{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>
 
Line 2,597 ⟶ 2,672:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7python">$ include "seed7_05.s7i";
$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,603 ⟶ 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,899 ⟶ 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,923 ⟶ 3,024:
{{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="ecmascriptwren">import "./iterate" for Stepped
 
// Print odd numbers under 20.
Line 2,977 ⟶ 3,078:
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const std = @import("std");
 
pub fn main() !void {
Line 2,984 ⟶ 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