Loops/Wrong ranges: Difference between revisions

 
(22 intermediate revisions by 14 users not shown)
Line 152:
 
Start equal stop equal zero: zero increment : Range( 0, 0, 0) => Error: increment must be greater than 0!
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
<syntaxhighlight lang="algol68">
BEGIN
# returns the first n elements of the sequences of values specified by start, stop and increment #
PROC sequence = ( INT n, start, stop, increment )[]INT:
BEGIN
[ 1 : n ]INT s;
FOR j FROM LWB s TO UPB s DO s[ j ] := 0 OD;
INT s pos := LWB s - 1;
FOR j FROM start BY increment TO stop WHILE s pos < n DO
s[ s pos +:= 1 ] := j
OD;
s[ LWB s : s pos ]
END # sequence # ;
 
# tests the sequence procedure #
PROC test sequence = ( INT start, stop, increment, STRING legend )VOID:
BEGIN
[]INT s = sequence( 10, start, stop, increment );
print( ( legend, ": " ) );
FOR i FROM LWB s TO UPB s DO print( ( " ", whole( s[ i ], -4 ) ) ) OD;
print( ( newline ) )
END # test sequence # ;
# task trest cases #
 
test sequence( -2, 2, 1, "Normal " );
test sequence( -2, 2, 0, "Zero increment " );
test sequence( -2, 2, -1, "Increments away from stop value " );
test sequence( -2, 2, 10, "First increment is beyond stop value " );
test sequence( 2, -2, 1, "Start more than stop: positive increment " );
test sequence( 2, 2, 1, "Start equal stop: positive increment " );
test sequence( 2, 2, -1, "Start equal stop: negative increment " );
test sequence( 2, 2, 0, "Start equal stop: zero increment " );
test sequence( 0, 0, 0, "Start equal stop equal zero: zero increment" )
 
END
</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>
 
Line 210 ⟶ 261:
</pre>
 
=={{header|Applesoft BASICArturo}}==
<syntaxhighlight lang="arturo">
print "start stop increment"
 
loop @[
neg 2 2 1
neg 2 2 0
neg 2 2 neg 1
neg 2 2 10
2 neg 2 1
2 2 1
2 2 neg 1
2 2 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"
]
</syntaxhighlight>
{{out}}
<pre>
start stop increment
-2 2 1 -> [-2 -1 0 1 2]
-2 2 0 -> Error
-2 2 -1 -> [-2 -1 0 1 2]
-2 2 10 -> [-2]
2 -2 1 -> [2 1 0 -1 -2]
2 2 1 -> [2]
2 2 -1 -> [2]
2 2 0 -> Error
0 0 0 -> Error</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LOOPS_WRONG_RANGES.AWK
BEGIN {
arr[++n] = "-2, 2, 1,Normal"
arr[++n] = "-2, 2, 0,Zero increment"
arr[++n] = "-2, 2,-1,Increments away from stop value"
arr[++n] = "-2, 2,10,First increment is beyond stop value"
arr[++n] = " 2,-2, 1,Start more than stop: positive increment"
arr[++n] = " 2, 2, 1,Start equal stop: positive increment"
arr[++n] = " 2, 2,-1,Start equal stop: negative increment"
arr[++n] = " 2, 2, 0,Start equal stop: zero increment"
arr[++n] = " 0, 0, 0,Start equal stop equal zero: zero increment"
print("start,stop,increment,comment")
for (i=1; i<=n; i++) {
split(arr[i],A,",")
printf("%-52s : ",arr[i])
count = 0
for (j=A[1]; j<=A[2] && count<10; j+=A[3]) {
printf("%d ",j)
count++
}
printf("\n")
}
exit(0)
}
</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
-2, 2,-1,Increments away from stop value : -2 -3 -4 -5 -6 -7 -8 -9 -10 -11
-2, 2,10,First increment is beyond stop value : -2
2,-2, 1,Start more than stop: positive increment :
2, 2, 1,Start equal stop: positive increment : 2
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
Line 254 ⟶ 379:
[ 2 TO 2 STEP 0 ] 2
START EQUAL STOP EQUAL ZERO: ZERO INCREMENT
[ 0 TO 0 STEP 0 ] 0</pre>
</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LOOPS_WRONG_RANGES.AWK
BEGIN {
arr[++n] = "-2, 2, 1,Normal"
arr[++n] = "-2, 2, 0,Zero increment"
arr[++n] = "-2, 2,-1,Increments away from stop value"
arr[++n] = "-2, 2,10,First increment is beyond stop value"
arr[++n] = " 2,-2, 1,Start more than stop: positive increment"
arr[++n] = " 2, 2, 1,Start equal stop: positive increment"
arr[++n] = " 2, 2,-1,Start equal stop: negative increment"
arr[++n] = " 2, 2, 0,Start equal stop: zero increment"
arr[++n] = " 0, 0, 0,Start equal stop equal zero: zero increment"
print("start,stop,increment,comment")
for (i=1; i<=n; i++) {
split(arr[i],A,",")
printf("%-52s : ",arr[i])
count = 0
for (j=A[1]; j<=A[2] && count<10; j+=A[3]) {
printf("%d ",j)
count++
}
printf("\n")
}
exit(0)
}
</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
-2, 2,-1,Increments away from stop value : -2 -3 -4 -5 -6 -7 -8 -9 -10 -11
-2, 2,10,First increment is beyond stop value : -2
2,-2, 1,Start more than stop: positive increment :
2, 2, 1,Start equal stop: positive increment : 2
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|BASIC256}}===
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">arraybase 1
Line 328 ⟶ 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 513 ⟶ 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 860 ⟶ 1,489:
start: 0 stop: 0 inc: 0 |
</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|Go}}==
Line 1,224 ⟶ 1,741:
 
</pre>
 
===Another approach===
 
<syntaxhighlight lang=J>genrange=: {{
'start stop increment'=. y
start+increment*i.1+<.(stop-start)%increment
}}</syntaxhighlight>
 
Here, we generate n values (where n is defined based on the difference between the start and stop values, scaled by the increment), and scale them according to the increment and adjust the origin to match the start value.
 
That said, since J treats a negative n as a reversed sequence, what we get here is a truncated projection backwards from the start value when the increment would never reach the stop value:
 
<syntaxhighlight lang=J> genrange 9 2 _2 NB. valid
9 7 5 3
genrange _2 2 1 NB. valid
_2 _1 0 1 2
genrange _2 2 0 NB. invalid: requires at least an infinity of values
|domain error: genrange
genrange _2 2 _1 NB. projects backwards from _2
_4 _3 _2
genrange _2 2 10 NB. valid
_2
genrange 2 _2 1 NB. projects backwards from 2
4 3 2
genrange 2 2 1 NB. valid (increment is irrelevant)
2
genrange 2 2 _1 NB. valid (increment is irrelevant)
2
genrange 2 2 0 NB. valid (increment is irrelevant)
2
genrange 0 0 0 NB. valid (increment is irrelevant)
0</syntaxhighlight>
 
As an aside, J's range generation function <code>i.</code> generates indices starting from 0 and not reaching the absolute value of its argument. This corresponds to integers in the [[wp:Interval_(mathematics)|interval]] [0,n). So we add 1 to n so that our range includes the end of the interval. However, when n is negative, adding 1 to n shortens the interval rather than extending it. So, when we "project backwards" we're 2 elements short of the number we would have expected if the sign of the interval had been reversed.
 
=={{header|Java}}==
Line 1,284 ⟶ 1,835:
 
=={{header|jq}}==
{{works with|jq}}
'''The following remarks also apply to gojq, the Go implmentation of jq'''.
 
jq has a filter `range(start; stop; increment)` which emits a
Line 1,316 ⟶ 1,869:
[range(0; 0; -1)] #=> []
</pre>
 
 
 
=={{header|Julia}}==
Line 1,407 ⟶ 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,433 ⟶ 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,441 ⟶ 1,984:
val .start, .stop, .inc, .comment = .test
{
val .series = series(.start to.. .stop, .inc)
catch {
writeln $"\{{.comment;}}\nERROR: \.{{_err["msg"]:L200(...);}}\n"
} else {
writeln $"\{{.comment;}}\nresult: \{{.series;}}\n"
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 2,094 ⟶ 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,334 ⟶ 2,901:
Start equal stop equal zero: zero increment</pre>
 
=={{header|VisualV Basic .NET(Vlang)}}==
'''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|Vlang}}==
Vlang has only one loop, a 'for' statement, which supports six different syntactical forms commonly found in other C-family languages:
 
Line 2,410 ⟶ 2,918:
It appears that either #5 fits the requirements of this task so I've translated a function which generates the appropriate sequence using #1 (limited to a maximum of 10 elements as some sequences will be infinite). I've then applied #5 to the resulting sequence. All sequences include the stop value if it's actually reached.
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">struct Seq {
start int
stop int
Line 2,479 ⟶ 2,987:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var loop = Fn.new { |start, stop, inc|
Line 2,513 ⟶ 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;
=={{header|Yabasic}}==
int S, Examples, I, J, C, Empty;
{{trans|FreeBASIC}}
def Limit = 10;
<syntaxhighlight lang="yabasic">data -2,2,1,"Normal",-2,2,0,"Zero increment",-2,2,-1,"Increments away from stop value"
[Examples:= [
data -2,2,10,"First increment is beyond stop value",2,-2,1,"Start more than stop: positive increment"
[-2, 2, 1, "Normal"],
data 2,2,1,"Start equal stop: positive increment",2,2,-1,"Start equal stop: negative increment"
[-2, 2, 0, "Zero increment"],
data 2,2,0,"Start equal stop: zero increment",0,0,0,"Start equal stop equal zero: zero increment"
[-2, 2, -1, "Increments away from stop value"],
 
[-2, 2, 10, "First increment is beyond stop value"],
for i = 1 to 9
[2, -2, 1, "Start more than stop: positive increment"],
contar = 0
[2, 2, 1, "Start equal stop: positive increment"],
read start, fin, inc, cmt$
[2, 2, -1, "Start equal stop: negative increment"],
print cmt$
[2, 2, 0, "Start equal stop: zero increment"],
print " Bucle de ", start, " a ", fin, " en incrementos de ", inc
[0, 0, 0, "Start equal stop equal zero: zero increment"]
for vr = start to fin step inc
];
print " Indice del bucle = ", vr
for I:= 0 to 9-1 do
contar = contar + 1
[S:= Examples(I);
if contar = 10 then
Print("%s\n", S(Comment));
print " Saliendo de un bucle infinito"
Print("Range(%d, %d, %d) -> [", S(Start), S(Stop), S(Incr));
break
Empty:= endiftrue;
\\ for (j:= s.start, c:= 0; j <= s.stop && c < limit; j += s.incr, ++c)
next vr
J:= S(Start); C:= 0;
print " Bucle terminado\n\n"
while J <= S(Stop) and C < Limit do
next i
[Print("%d ", J);
end</syntaxhighlight>
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]
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
Range(-2, 2, Bucle0) de-> [-2 a-2 -2 en-2 incrementos-2 de-2 0-2 -2 -2 -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
Indice del bucle = -2
Saliendo de un bucle infinito
Bucle terminado
 
 
Increments away from stop value
Range(-2, 2, Bucle-1) de-> [-2 a-3 2-4 en-5 incrementos-6 de-7 -18 -9 -10 -11]
Bucle terminado
 
 
First increment is beyond stop value
Range(-2, 2, 10) -> [-2]
Bucle de -2 a 2 en incrementos de 10
Indice del bucle = -2
Bucle terminado
 
 
Start more than stop: positive increment
Range(2, -2, 1) -> []
Bucle de 2 a -2 en incrementos de 1
Bucle terminado
 
 
Start equal stop: positive increment
Range(2, 2, 1) -> [2]
Bucle de 2 a 2 en incrementos de 1
Indice del bucle = 2
Bucle terminado
 
 
Start equal stop: negative increment
Range(2, 2, Bucle-1) de-> [2 a1 0 -1 -2 en-3 incrementos-4 de-5 -16 -7]
Indice del bucle = 2
Bucle terminado
 
 
Start equal stop: zero increment
Range(2, 2, Bucle0) de-> [2 a2 2 en2 incrementos2 de2 02 2 2 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
Indice del bucle = 2
Saliendo de un bucle infinito
Bucle terminado
 
 
Start equal stop equal zero: zero increment
Range(0, 0, Bucle0) de-> [0 a0 0 en0 incrementos0 de0 0 0 0 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|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}}==
885

edits