Loops/Wrong ranges: Difference between revisions

(Added Chipmunk Basic, GW-BASIC, Minimal BASIC, MSX Basic and Quite BASIC. Grouping BASIC dialects)
 
(13 intermediate revisions by 8 users not shown)
Line 262:
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">print "start stop increment"
print "start stop increment"
 
loop @[
0-neg 2 2 1
0-neg 2 2 0
0-neg 2 2 0- neg 1
0-neg 2 2 10
2 2 0- neg 2 1
2 2 2 1
2 2 2 0- neg 1
2 2 2 0
0 0 0 0 ]
] [start stop increment] ->
print [
pad ~"|start|" 2 pad ~"|stop|" 7 pad ~"|increment|" 7
pad "->" 9 try? -> @range.step: increment start stop
else -> "Error"
try? -> @range.step: increment start stop
else -> "Error"
]
</syntaxhighlight>
{{out}}
<pre>
<pre>start stop increment
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,018 ⟶ 1,078:
{{out}}
Same as original C.
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
struct LoopData {
int32_t start;
int32_t stop;
int32_t increment;
std::string comment;
};
 
void print_vector(const std::vector<int32_t>& list) {
std::cout << "[";
for ( uint64_t i = 0; i < list.size(); ++i ) {
std::cout << list[i];
if ( i < list.size() - 1 ) {
std::cout << ", ";
}
}
std::cout << "]";
}
 
void runTest(const LoopData& loop_data) {
std::vector<int32_t> values{};
for ( int32_t i = loop_data.start; i <= loop_data.stop; i += loop_data.increment ) {
values.emplace_back(i);
if ( values.size() >= 10 ) {
break;
}
}
 
std::cout << std::left << std::setw(45) << loop_data.comment; print_vector(values);
std::cout << ( values.size() == 10 ? " (loops forever)" : "" ) << std::endl;
}
 
int main() {
runTest(LoopData(-2, 2, 1, "Normal"));
runTest(LoopData(-2, 2, 0, "Zero increment"));
runTest(LoopData(-2, 2, -1, "Increments away from stop value"));
runTest(LoopData(-2, 2, 10, "First increment is beyond stop value"));
runTest(LoopData(2, -2, 1, "Start more than stop: positive increment"));
runTest(LoopData(2, 2, 1, "Start equal stop: positive increment"));
runTest(LoopData(2, 2, -1, "Start equal stop: negative increment"));
runTest(LoopData(2, 2, 0, "Start equal stop: zero increment"));
runTest(LoopData(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] (loops forever)
Increments away from stop value [-2, -3, -4, -5, -6, -7, -8, -9, -10, -11] (loops forever)
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] (loops forever)
Start equal stop: zero increment [2, 2, 2, 2, 2, 2, 2, 2, 2, 2] (loops forever)
Start equal stop equal zero: zero increment [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] (loops forever)
</pre>
 
=={{header|Common Lisp}}==
Line 1,834 ⟶ 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.
Langur has series() and pseries() functions, which generate arrays. These are not limited to integers for start, stop, or increment values. The pseries() function never returns a descending series (returns empty array instead).
 
You don't always have to convert a range to a series explicitly. Since 0.6.15, the functions map(), fold(), foldfrom(), and zip() accept ranges in place of arrays.
 
In the following example, we could have just started with an array of arrays, but following the Python example, we process the .data string into a table.
 
{{trans|Python}}
<syntaxhighlight lang="langur">val .data = qs:block END
{{works with|langur|0.10}}
Langur 0.7.0 changed the implicit exception variable from .err to _err, and 0.7.1 allows you to map to multiple functions.
 
Prior to 0.10, multi-variable declaration/assignment would use parentheses around the variable names (.start, .stop, .inc, .comment).
 
<syntaxhighlight lang="langur">val .data = q:block END
start stop increment comment
-2 2 1 Normal
Line 1,860 ⟶ 1,975:
 
var .table = submatches(RE/([^ ]+) +([^ ]+) +([^ ]+) +(.+)\n?/, .data)
 
val .f = toNumber
val .f = fn .x: number .x
for .i in 2..len(.table) {
.table[.i] = map [.f, .f, .f, _], .table[.i]
Line 1,870 ⟶ 1,986:
val .series = series(.start .. .stop, .inc)
catch {
writeln $"\{{.comment;}}\nERROR: \.{{_err["msg"]:L200(...);}}\n"
} else {
writeln $"\{{.comment;}}\nresult: \{{.series;}}\n"
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,870 ⟶ 2,987:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var loop = Fn.new { |start, stop, inc|
Line 2,902 ⟶ 3,019:
[ 2 2 0] -> 2 2 2 2 2 2 2 2 2 2
[ 0 0 0] -> 0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
XPL0's 'for' statement only handles increments and decrements by one.
However, a straightforward translation of the 'for' loop in the C example
solves the task.
<syntaxhighlight lang "XPL0">include xpllib; \for Print
 
def \S\ Start, Stop, Incr, Comment;
int S, Examples, I, J, C, Empty;
def Limit = 10;
[Examples:= [
[-2, 2, 1, "Normal"],
[-2, 2, 0, "Zero increment"],
[-2, 2, -1, "Increments away from stop value"],
[-2, 2, 10, "First increment is beyond stop value"],
[2, -2, 1, "Start more than stop: positive increment"],
[2, 2, 1, "Start equal stop: positive increment"],
[2, 2, -1, "Start equal stop: negative increment"],
[2, 2, 0, "Start equal stop: zero increment"],
[0, 0, 0, "Start equal stop equal zero: zero increment"]
];
for I:= 0 to 9-1 do
[S:= Examples(I);
Print("%s\n", S(Comment));
Print("Range(%d, %d, %d) -> [", S(Start), S(Stop), S(Incr));
Empty:= true;
\\ for (j:= s.start, c:= 0; j <= s.stop && c < limit; j += s.incr, ++c)
J:= S(Start); C:= 0;
while J <= S(Stop) and C < Limit do
[Print("%d ", J);
Empty:= false;
J:= J + S(Incr); C:= C+1;
];
if not Empty then ChOut(0, $08 \BS\);
Print("]\n\n");
]
]</syntaxhighlight>
{{out}}
<pre>
Normal
Range(-2, 2, 1) -> [-2 -1 0 1 2]
 
Zero increment
Range(-2, 2, 0) -> [-2 -2 -2 -2 -2 -2 -2 -2 -2 -2]
 
Increments away from stop value
Range(-2, 2, -1) -> [-2 -3 -4 -5 -6 -7 -8 -9 -10 -11]
 
First increment is beyond stop value
Range(-2, 2, 10) -> [-2]
 
Start more than stop: positive increment
Range(2, -2, 1) -> []
 
Start equal stop: positive increment
Range(2, 2, 1) -> [2]
 
Start equal stop: negative increment
Range(2, 2, -1) -> [2 1 0 -1 -2 -3 -4 -5 -6 -7]
 
Start equal stop: zero increment
Range(2, 2, 0) -> [2 2 2 2 2 2 2 2 2 2]
 
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>
 
889

edits