Loops/For with a specified step: Difference between revisions
Zig version with for-loop added
(add RPL) |
(Zig version with for-loop added) |
||
(23 intermediate revisions by 13 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 614 ⟶ 624:
30 NEXT</syntaxhighlight>
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
Line 622 ⟶ 632:
Print
Sleep</syntaxhighlight>
==={{header|Gambas}}===
Line 963 ⟶ 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,213 ⟶ 1,206:
# Prints even numbers from 0 to 100
for i = 0 step 2 to 100
print i
.
Line 1,254 ⟶ 1,243:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">public program()
{
for(int i := 2
{
console.writeLine
}
}</syntaxhighlight>
Line 1,279 ⟶ 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,425 ⟶ 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,439 ⟶ 1,464:
11
</syntaxhighlight>
=={{header|Go}}==
Line 1,525 ⟶ 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="
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,544 ⟶ 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,718 ⟶ 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,985 ⟶ 2,043:
=={{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}}==
Line 2,547 ⟶ 2,672:
=={{header|Seed7}}==
<syntaxhighlight lang="
$ include "seed7_05.s7i";
const proc: main is func
Line 2,553 ⟶ 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}}==
Line 2,724 ⟶ 2,875:
<syntaxhighlight lang="bash">for x in `jot - 2 8 2`; do echo $x; done</syntaxhighlight>
==={{header|
<syntaxhighlight lang="ksh">x=0
while (((x += 2) <= 8))
do
print -r "$x"
done</syntaxhighlight>
{{works with|
<syntaxhighlight lang="
do
print -r "$x"
done</syntaxhighlight>
Line 2,851 ⟶ 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="
for (i in 1..10) {
var j = 2*i - 1
Line 2,875 ⟶ 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="
// Print odd numbers under 20.
Line 2,929 ⟶ 3,078:
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
pub fn main() !void {
Line 2,936 ⟶ 3,086:
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>
|