Loops/Wrong ranges: Difference between revisions
→{{header|langur}}
(New post.) |
Langurmonkey (talk | contribs) |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 262:
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">
print "start stop increment"
loop @[
2
2
2
2
0
print [
pad ~"|start|" 2 pad ~"|stop|" 7 pad ~"|increment|" 7
pad "->" 9 try? -> @range.step: increment start stop
else -> "Error"
]
</syntaxhighlight>
{{out}}
<pre>
start stop increment
-2 2 1 -> [-2 -1 0 1 2]
-2 2 0 -> Error
Line 725 ⟶ 726:
Start equal stop equal zero: zero increment ------ 0 0 0 0 0 0 0 0 0 0</pre>
==={{header|uBasic/4tH}}===
<syntaxhighlight lang="qbasic">Push -2, 2, 1, "Normal"
Push -2, 2, 0, "Zero increment"
Push -2, 2, -1, "Increments away from stop value"
Push -2, 2, 10, "First increment is beyond stop value"
Push 2, -2, 1, "Start more than stop: positive increment"
Push 2, 2, 1, "Start equal stop: positive increment"
Push 2, 2, -1, "Start equal stop: negative increment"
Push 2, 2, 0, "Start equal stop: zero increment"
Push 0, 0, 0, "Start equal stop equal zero: zero increment"
Do While Used()
Print "(";Show (Pop());")"
i = Pop() : e = Pop () : s = Pop() : y = 0
Print "FOR X=";s;" TO ";e;" STEP ";i
For x = s To e Step i : Print x;" "; : While Set (y, y+1) < 10 : Next
Print Show(Iif (y < 10, Iif (y = 0, "None", ""), ".. infinite")) : Print
Loop</syntaxhighlight>
{{Out}}
<pre>(Start equal stop equal zero: zero increment)
FOR X=0 TO 0 STEP 0
0 0 0 0 0 0 0 0 0 0 .. infinite
(Start equal stop: zero increment)
FOR X=2 TO 2 STEP 0
2 2 2 2 2 2 2 2 2 2 .. infinite
(Start equal stop: negative increment)
FOR X=2 TO 2 STEP -1
2
(Start equal stop: positive increment)
FOR X=2 TO 2 STEP 1
2
(Start more than stop: positive increment)
FOR X=2 TO -2 STEP 1
None
(First increment is beyond stop value)
FOR X=-2 TO 2 STEP 10
-2
(Increments away from stop value)
FOR X=-2 TO 2 STEP -1
None
(Zero increment)
FOR X=-2 TO 2 STEP 0
-2 -2 -2 -2 -2 -2 -2 -2 -2 -2 .. infinite
(Normal)
FOR X=-2 TO 2 STEP 1
-2 -1 0 1 2
0 OK, 0:718 </pre>
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
Line 1,898 ⟶ 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 a data string into a table.
{{trans|Python}}
<syntaxhighlight lang="langur">val .data = qs:block END
start stop increment comment
-2 2 1 Normal
Line 1,924 ⟶ 1,975:
var .table = submatches(RE/([^ ]+) +([^ ]+) +([^ ]+) +(.+)\n?/, .data)
val .f = fn .x: number .x
for .i in 2..len(.table) {
.table[.i] = map [.f, .f, .f, _], .table[.i]
Line 1,934 ⟶ 1,986:
val .series = series(.start .. .stop, .inc)
catch {
writeln
} else {
writeln
}
}
}
</syntaxhighlight>
{{out}}
Line 2,934 ⟶ 2,987:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var loop = Fn.new { |start, stop, inc|
Line 3,033 ⟶ 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>
|