Loops/Wrong ranges: Difference between revisions

(4 intermediate revisions by 2 users not shown)
Line 1,958:
 
=={{header|langur}}==
In the following example, we could have just started with a list of lists, but following the Python example, we process thea .data string into a table.
 
{{trans|Python}}
<syntaxhighlight lang="langur">val .data = qqs:block END
start stop increment comment
-2 2 1 Normal
Line 1,975:
 
var .table = submatches(RE/([^ ]+) +([^ ]+) +([^ ]+) +(.+)\n?/, .data)
 
val .f = ffn .x: number .x
for .i in 2..len(.table) {
.table[.i] = map [.f, .f, .f, _], .table[.i]
Line 1,985 ⟶ 1,986:
val .series = series(.start .. .stop, .inc)
catch {
writeln $"\{{.comment;}}\nERROR: \.{{_err["msg"]:L200(...);}}\n"
} else {
writeln $"\{{.comment;}}\nresult: \{{.series;}}\n"
}
}
}
}</syntaxhighlight>
 
{{out}}
Line 3,084 ⟶ 3,086:
Start equal stop equal zero: zero increment
Range(0, 0, 0) -> [0 0 0 0 0 0 0 0 0 0]
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">
const std = @import("std");
const stdout = @import("std").io.getStdOut().writer();
 
const limit = 10;
 
fn printRange(start: isize, stop: isize, step: isize, comment: []const u8) !void {
try stdout.print("{s}\r\x1b[43C: ", .{comment});
var c: u8 = 0;
var n = start;
while (n <= stop) : (n += step) {
c += 1;
if (c > limit) break;
try stdout.print("{d} ", .{n});
}
try stdout.print("\n", .{});
}
 
pub fn main() !void {
try printRange(-2, 2, 1, "Normal");
try printRange(-2, 2, 0, "Zero increment");
try printRange(-2, 2, -1, "Increments away from stop value");
try printRange(-2, 2, 10, "First increment is beyond stop value");
try printRange(2, -2, 1, "Start more than stop: positive increment");
try printRange(2, 2, 1, "Start equal stop: positive increment");
try printRange(2, 2, -1, "Start equal stop: negative increment");
try printRange(2, 2, 0, "Start equal stop: zero increment");
try printRange(0, 0, 0, "Start equal stop equal zero: zero increment");
}
</syntaxhighlight>
{{out}}
<pre>
Normal : -2 -1 0 1 2
Zero increment : -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
Increments away from stop value : -2 -3 -4 -5 -6 -7 -8 -9 -10 -11
First increment is beyond stop value : -2
Start more than stop: positive increment :
Start equal stop: positive increment : 2
Start equal stop: negative increment : 2 1 0 -1 -2 -3 -4 -5 -6 -7
Start equal stop: zero increment : 2 2 2 2 2 2 2 2 2 2
Start equal stop equal zero: zero increment: 0 0 0 0 0 0 0 0 0 0
</pre>
 
990

edits