Loops/Wrong ranges: Difference between revisions
→{{header|langur}}
Drkameleon (talk | contribs) m (updated expected output) |
Langurmonkey (talk | contribs) |
||
(11 intermediate revisions by 6 users not shown) | |||
Line 283:
</syntaxhighlight>
{{out}}
<pre>
start stop increment
-2 2 1 -> [-2 -1 0 1 2]
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,923 ⟶ 1,975:
END
# Process data string into table.
# We could have just started with a list of lists, of course.
var table = submatches(RE/([^ ]+) +([^ ]+) +([^ ]+) +(.+)\n?/, data)
for i in 2..len(table) {
table[i] = map([number, number, number, _], table[i])
}
for
val
{
val
catch {
writeln
} else {
writeln
}
}
}
</syntaxhighlight>
{{out}}
Line 2,117 ⟶ 2,172:
( 2, 2, 0): not allowed.
( 0, 0, 0): not allowed.</pre>
=={{header|PascalABC.NET}}==
{{trans|Nim}}
<syntaxhighlight lang="delphi">
procedure DisplayRange(first, last, step: integer);
begin
Print($'({first,2}, {last,2}, {step,2}): ');
if step = 0 then
Println('not allowed')
else Println(Range(first, last, step));
end;
begin
var ranges := |(-2, 2, 1), (-2, 2, 0), (-2, 2, -1),
(-2, 2, 10), (2, -2, 1), (2, 2, 1),
(2, 2, -1), (2, 2, 0), (0, 0, 0)|;
foreach var (f, l, s) in ranges do
DisplayRange(f,l,s);
end.
</syntaxhighlight>
{{out}}
<pre>
(-2, 2, 1): [-2,-1,0,1,2]
(-2, 2, 0): not allowed
(-2, 2, -1): []
(-2, 2, 10): [-2]
( 2, -2, 1): []
( 2, 2, 1): [2]
( 2, 2, -1): [2]
( 2, 2, 0): not allowed
( 0, 0, 0): not allowed
</pre>
=={{header|Perl}}==
Line 2,934 ⟶ 3,023:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var loop = Fn.new { |start, stop, inc|
Line 3,033 ⟶ 3,122:
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>
|