Loops/Wrong ranges: Difference between revisions
→{{header|langur}}
Drkameleon (talk | contribs) (added Arturo) |
Langurmonkey (talk | contribs) |
||
(16 intermediate revisions by 10 users not shown) | |||
Line 261:
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">
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>
-2 2 1 -> [-2 -1 0 1 2]
-2 2 0 -> Error
Line 371 ⟶ 323:
</syntaxhighlight>
{{out}}
<pre>start,stop,increment,comment
-2, 2, 1,Normal : -2 -1 0 1 2
-2, 2, 0,Zero increment : -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
Line 381 ⟶ 332:
2, 2,-1,Start equal stop: negative increment : 2 1 0 -1 -2 -3 -4 -5 -6 -7
2, 2, 0,Start equal stop: zero increment : 2 2 2 2 2 2 2 2 2 2
0, 0, 0,Start equal stop equal zero: zero increment : 0 0 0 0 0 0 0 0 0 0</pre>
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="applesoftbasic">0 LIMIT = 10 :MAX=1E37
10 DATA-2,2,1,NORMAL
20 DATA-2,2,0,ZERO INCREMENT
30 DATA-2,2,-1,INCREMENTS AWAY FROM STOP VALUE
40 DATA-2,2,10,FIRST INCREMENT IS BEYOND STOP VALUE
50 DATA2,-2,1,"START MORE THAN STOP: POSITIVE INCREMENT
60 DATA2,2,1,"START EQUAL STOP: POSITIVE INCREMENT
70 DATA2,2,-1,"START EQUAL STOP: NEGATIVE INCREMENT
80 DATA2,2,0,"START EQUAL STOP: ZERO INCREMENT
90 DATA0,0,0,"START EQUAL STOP EQUAL ZERO: ZERO INCREMENT
100 FOR I = 1 TO 9
110 READ START,FINISH,INCR,COMMENT$
120 PRINT CHR$(13)COMMENT$
130 LAST = FINISH
140 REM D = SGN(FINISH - START)
150 REM IF D AND NOT (D = SGN(INCR)) THEN LAST = SGN(INCR)*MAX
160 PRINT TAB(5)CHR$(91)" "MID$(" ",(START<0)+1)START" TO "MID$(" ",(FINISH<0)+1)FINISH" STEP "MID$(" ",(INCR<0)+1)INCR" ] ";
170 COUNT = 0
180 PRINT MID$(" ",(START<0)+1);
190 FOR J = START TO LAST STEP INCR
200 PRINT MID$(" ",(COUNT=0)+1)J;
210 IF COUNT < LIMIT THEN COUNT = COUNT + 1 : NEXT J
220 IF COUNT = LIMIT THEN PRINT " ... ";:if ABS(LAST) = MAX THEN PRINT MID$("-",SGN(LAST)+2,1)"INFINITY";
230 NEXT I</syntaxhighlight>
{{out}}
<pre>
NORMAL
[ -2 TO 2 STEP 1 ] -2 -1 0 1 2
ZERO INCREMENT
[ -2 TO 2 STEP 0 ] -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 ...
INCREMENTS AWAY FROM STOP VALUE
[ -2 TO 2 STEP -1 ] -2
FIRST INCREMENT IS BEYOND STOP VALUE
[ -2 TO 2 STEP 10 ] -2
START MORE THAN STOP: POSITIVE INCREMENT
[ 2 TO -2 STEP 1 ] 2
START EQUAL STOP: POSITIVE INCREMENT
[ 2 TO 2 STEP 1 ] 2
START EQUAL STOP: NEGATIVE INCREMENT
[ 2 TO 2 STEP -1 ] 2
START EQUAL STOP: ZERO INCREMENT
[ 2 TO 2 STEP 0 ] 2
START EQUAL STOP EQUAL ZERO: ZERO INCREMENT
[ 0 TO 0 STEP 0 ] 0</pre>
==={{header|BASIC256}}===
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">arraybase 1
Line 415 ⟶ 412:
{{out}}
<pre>Igual que la entrada de Yabasic.</pre>
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 for i = 1 to 9
120 contar = 0
130 read start,fin,inc,cmt$
140 print cmt$
150 print " Bucle de ";start;"a ";fin;" en incrementos de ";inc
160 for vr = start to fin step inc
170 print " Indice del bucle = ";vr
180 contar = contar+1
190 if contar = 10 then
200 print " Saliendo de un bucle infinito"
210 goto 240
220 endif
230 next vr
240 print " Bucle terminado" : print : print
250 next i
260 end
270 data -2,2,1,"Normal",-2,2,0,"Zero increment",-2,2,-1,"Increments away from stop value"
280 data -2,2,10,"First increment is beyond stop value",2,-2,1,"Start more than stop: positive increment"
290 data 2,2,1,"Start equal stop: positive increment",2,2,-1,"Start equal stop: negative increment"
300 data 2,2,0,"Start equal stop: zero increment",0,0,0,"Start equal stop equal zero: zero increment"</syntaxhighlight>
{{out}}
<pre>Normal
Bucle de -2 a 2 en incrementos de 1
Indice del bucle = -2
Indice del bucle = -1
Indice del bucle = 0
Indice del bucle = 1
Indice del bucle = 2
Bucle terminado
Zero increment
Bucle de -2 a 2 en incrementos de 0
Bucle terminado
Increments away from stop value
Bucle de -2 a 2 en incrementos de -1
Bucle terminado
First increment is beyond stop value
Bucle de -2 a 2 en incrementos de 10
Indice del bucle = -2
Bucle terminado
Start more than stop: positive increment
Bucle de 2 a -2 en incrementos de 1
Bucle terminado
Start equal stop: positive increment
Bucle de 2 a 2 en incrementos de 1
Indice del bucle = 2
Bucle terminado
Start equal stop: negative increment
Bucle de 2 a 2 en incrementos de -1
Indice del bucle = 2
Bucle terminado
Start equal stop: zero increment
Bucle de 2 a 2 en incrementos de 0
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Saliendo de un bucle infinito
Bucle terminado
Start equal stop equal zero: zero increment
Bucle de 0 a 0 en incrementos de 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Saliendo de un bucle infinito
Bucle terminado</pre>
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">data -2,2,1,"Normal",-2,2,0,"Zero increment",-2,2,-1,"Increments away from stop value"
data -2,2,10,"First increment is beyond stop value",2,-2,1,"Start more than stop: positive increment"
data 2,2,1,"Start equal stop: positive increment",2,2,-1,"Start equal stop: negative increment"
data 2,2,0,"Start equal stop: zero increment",0,0,0,"Start equal stop equal zero: zero increment"
dim as integer i, start, fin, inc, vr, count
dim as string cmt
for i = 1 to 9
count = 0
read start, fin, inc, cmt
print cmt
print using " Looping from ### to ### in increments of ##"; start; fin; inc
for vr = start to fin step inc
print " Loop index = ",vr
count += 1
if count = 10 then
print " Breaking infinite loop"
exit for
end if
next vr
print " Loop finished"
print
print
next i
</syntaxhighlight>
{{out}}
<pre>Normal
Looping from -2 to 2 in increments of 1
Loop index = -2
Loop index = -1
Loop index = 0
Loop index = 1
Loop index = 2
Loop finished
Zero increment
Looping from -2 to 2 in increments of 0
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Loop index = -2
Breaking infinite loop
Loop finished
Increments away from stop value
Looping from -2 to 2 in increments of -1
Loop finished
First increment is beyond stop value
Looping from -2 to 2 in increments of 10
Loop index = -2
Loop finished
Start more than stop: positive increment
Looping from 2 to -2 in increments of 1
Loop finished
Start equal stop: positive increment
Looping from 2 to 2 in increments of 1
Loop index = 2
Loop finished
Start equal stop: negative increment
Looping from 2 to 2 in increments of -1
Loop index = 2
Loop finished
Start equal stop: zero increment
Looping from 2 to 2 in increments of 0
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Loop index = 2
Breaking infinite loop
Loop finished
Start equal stop equal zero: zero increment
Looping from 0 to 0 in increments of 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Loop index = 0
Breaking infinite loop
Loop finished</pre>
==={{header|GW-BASIC}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
==={{header|Minimal BASIC}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : REM 100 HOME for Applesoft BASIC : REM DELETE line for Minimal BASIC
110 DATA -2,2,1,"Normal",-2,2,0,"Zero increment"
120 DATA -2,2,-1,"Increments away from stop value"
130 DATA -2,2,10,"First increment is beyond stop value"
140 DATA 2,-2,1,"Start more than stop: positive increment"
150 DATA 2,2,1,"Start equal stop: positive increment"
160 DATA 2,2,-1,"Start equal stop: negative increment"
170 DATA 2,2,0,"Start equal stop: zero increment"
180 DATA 0,0,0,"Start equal stop equal zero: zero increment"
190 FOR j = 1 TO 9
200 LET c = 0
210 READ s, f, i, t$
220 PRINT t$
230 PRINT " Bucle de "; s; "a "; f; " en incrementos de "; i
240 FOR v = s TO f STEP i
250 PRINT " Indice del bucle = "; v
260 LET c = c + 1
270 IF c <> 10 THEN 300
280 PRINT " Saliendo de un bucle infinito"
290 GOTO 310
300 NEXT v
310 PRINT " Bucle terminado"
320 PRINT
330 NEXT j
340 END</syntaxhighlight>
==={{header|MSX Basic}}===
The [[#Chipmunk Basic|Chipmunk Basic]] solution works without any changes.
==={{header|Quite BASIC}}===
See solution [[#Minimal BASIC|Minimal BASIC]]
==={{header|Visual Basic .NET}}===
'''Compiler:''' >= Visual Studio 2012
VB.NET's For loop accepts a starting and ending value and optional step.
Since the task mentions generators and a range of values, this implementation places the for loop in an iterator function (called a generator in many other languages) and yields the iteration variable in every iteration. The resulting IEnumerable object (whose actual class is generated by the compiler) is lazily-evaluated (i.e., it is run only when a new value is requested, and only until the next Yield statement).
The number of iterations is limited to 10 by the test code.
<syntaxhighlight lang="vbnet">Module Program
Sub Main()
Example(-2, 2, 1, "Normal")
Example(-2, 2, 0, "Zero increment")
Example(-2, 2, -1, "Increments away from stop value")
Example(-2, 2, 10, "First increment is beyond stop value")
Example(2, -2, 1, "Start more than stop: positive increment")
Example(2, 2, 1, "Start equal stop: positive increment")
Example(2, 2, -1, "Start equal stop: negative increment")
Example(2, 2, 0, "Start equal stop: zero increment")
Example(0, 0, 0, "Start equal stop equal zero: zero increment")
End Sub
' Stop is a keyword and must be escaped using brackets.
Iterator Function Range(start As Integer, [stop] As Integer, increment As Integer) As IEnumerable(Of Integer)
For i = start To [stop] Step increment
Yield i
Next
End Function
Sub Example(start As Integer, [stop] As Integer, increment As Integer, comment As String)
' Add a space, pad to length 50 with hyphens, and add another space.
Console.Write((comment & " ").PadRight(50, "-"c) & " ")
Const MAX_ITER = 9
Dim iteration = 0
' The For Each loop enumerates the IEnumerable.
For Each i In Range(start, [stop], increment)
Console.Write("{0,2} ", i)
iteration += 1
If iteration > MAX_ITER Then Exit For
Next
Console.WriteLine()
End Sub
End Module</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 ------------------
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
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>
==={{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}}
<syntaxhighlight lang="yabasic">data -2,2,1,"Normal",-2,2,0,"Zero increment",-2,2,-1,"Increments away from stop value"
data -2,2,10,"First increment is beyond stop value",2,-2,1,"Start more than stop: positive increment"
data 2,2,1,"Start equal stop: positive increment",2,2,-1,"Start equal stop: negative increment"
data 2,2,0,"Start equal stop: zero increment",0,0,0,"Start equal stop equal zero: zero increment"
for i = 1 to 9
contar = 0
read start, fin, inc, cmt$
print cmt$
print " Bucle de ", start, " a ", fin, " en incrementos de ", inc
for vr = start to fin step inc
print " Indice del bucle = ", vr
contar = contar + 1
if contar = 10 then
print " Saliendo de un bucle infinito"
break
endif
next vr
print " Bucle terminado\n\n"
next i
end</syntaxhighlight>
{{out}}
<pre>Normal
Bucle de -2 a 2 en incrementos de 1
Indice del bucle = -2
Indice del bucle = -1
Indice del bucle = 0
Indice del bucle = 1
Indice del bucle = 2
Bucle terminado
Zero increment
Bucle de -2 a 2 en incrementos de 0
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Indice del bucle = -2
Saliendo de un bucle infinito
Bucle terminado
Increments away from stop value
Bucle de -2 a 2 en incrementos de -1
Bucle terminado
First increment is beyond stop value
Bucle de -2 a 2 en incrementos de 10
Indice del bucle = -2
Bucle terminado
Start more than stop: positive increment
Bucle de 2 a -2 en incrementos de 1
Bucle terminado
Start equal stop: positive increment
Bucle de 2 a 2 en incrementos de 1
Indice del bucle = 2
Bucle terminado
Start equal stop: negative increment
Bucle de 2 a 2 en incrementos de -1
Indice del bucle = 2
Bucle terminado
Start equal stop: zero increment
Bucle de 2 a 2 en incrementos de 0
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Indice del bucle = 2
Saliendo de un bucle infinito
Bucle terminado
Start equal stop equal zero: zero increment
Bucle de 0 a 0 en incrementos de 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Indice del bucle = 0
Saliendo de un bucle infinito
Bucle terminado</pre>
=={{header|C}}==
Line 600 ⟶ 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 947 ⟶ 1,489:
start: 0 stop: 0 inc: 0 |
</pre>
=={{header|Go}}==
Line 1,528 ⟶ 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,554 ⟶ 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,564 ⟶ 1,986:
val .series = series(.start .. .stop, .inc)
catch {
writeln
} else {
writeln
}
}
}
</syntaxhighlight>
{{out}}
Line 2,215 ⟶ 2,638:
─────────── end of performing DO loop number 9 with range: 0 0 0───────────
</pre>
=={{header|RPL}}==
{| class="wikitable"
!start ||stop ||increment ||Result
|-
| -2||2||1||5 loops
|-
| -2||2||0||endless loop
|-
| -2||2||-1||1 loop
|-
| -2||2||10||1 loop
|-
|2||-2||1||1 loop
|-
|2||2||1||1 loop
|-
|2||2||-1||1 loop
|-
|2||2||0||endless loop
|-
|0||0||0||endless loop
|}
=={{header|Ruby}}==
Line 2,454 ⟶ 2,900:
0 0 0 | 0,0,0,0,0,0,0,0,0,0,0,
Start equal stop equal zero: zero increment</pre>
=={{header|V (Vlang)}}==
Line 2,600 ⟶ 2,987:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var loop = Fn.new { |start, stop, inc|
Line 2,634 ⟶ 3,021:
</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:=
\\ 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,
Increments away from stop value
Range(-2, 2,
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,
Start equal stop: zero increment
Range(2, 2,
Start equal stop equal zero: zero increment
Range(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>
=={{header|zkl}}==
|