Loops/Wrong ranges: Difference between revisions

 
(36 intermediate revisions by 25 users not shown)
Line 47:
*   [[Loops/Wrong ranges]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F displayRange(first, last, step)
print(‘(#2, #2, #2): ’.format(first, last, step), end' ‘’)
I step == 0
print(‘not allowed.’)
E
print(Array((first..last).step(step)))
 
L(f, l, s) [(-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)]
displayRange(f, l, s)</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|Ada}}==
Ada uses the concept of a range extensively, both in the definition of programmer-defined scalar types and subtypes and also in the iteration of arrays. In Ada a range is defined in the Ada 2012 Language Reference Manual as
 
''A range has a lower bound and an upper bound and specifies a subset of the values of some scalar type (the type of the range). A range with lower bound L and upper bound R is described by “L .. R”. If R is less than L, then the range is a null range, and specifies an empty set of values. Otherwise, the range specifies the values of the type from the lower bound to the upper bound, inclusive.''
 
The following solution follows the logic of a range as specified in the task description.
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
 
procedure Main is
procedure print_range(start : integer; stop : integer; step : integer) is
Num : Integer := start;
begin
Put("Range(" & start'Image & ", " & stop'image &
", " & step'image & ") => ");
if stop < start then
Put_Line("Error: stop must be no less than start!");
elsif step not in positive then
Put_Line("Error: increment must be greater than 0!");
elsif start = stop then
Put_Line(start'image);
else
while num <= stop loop
Put(num'Image);
num := num + step;
end loop;
New_Line;
end if;
end print_range;
 
type test_record is record
start : integer;
stop : integer;
step : integer;
comment : unbounded_string := null_unbounded_string;
end record;
 
tests : array(1..9) of test_record :=
( 1 => (-2, 2, 1, To_Unbounded_String("Normal")),
2 => (-2, 2, 0, To_Unbounded_String("Zero increment")),
3 => (-2, 2, -1, To_Unbounded_String("Increments away from stop value")),
4 => (-2, 2, 10, To_Unbounded_String("First increment is beyond stop value")),
5 => (2, -1, 1, To_Unbounded_String("Start more than stop: positive increment")),
6 => (2, 2, 1, To_Unbounded_String("Start equal stop: positive increment")),
7 => (2, 2, -1, To_Unbounded_String("Start equal stop: negative increment")),
8 => (2, 2, 0, To_Unbounded_String("Start equal stop: zero increment")),
9 => (0, 0, 0, To_Unbounded_String("Start equal stop equal zero: zero increment")));
 
 
begin
for test of tests loop
Put(Test.Comment); Put(" : ");
print_range(test.start, test.stop, test.step);
New_line;
end loop;
end Main;</syntaxhighlight>
{{out}}
<pre>
Normal : Range(-2, 2, 1) => -2-1 0 1 2
 
Zero increment : Range(-2, 2, 0) => Error: increment must be greater than 0!
 
Increments away from stop value : Range(-2, 2, -1) => Error: increment must be greater than 0!
 
First increment is beyond stop value : Range(-2, 2, 10) => -2
 
Start more than stop: positive increment : Range( 2, -1, 1) => Error: stop must be no less than start!
 
Start equal stop: positive increment : Range( 2, 2, 1) => 2
 
Start equal stop: negative increment : Range( 2, 2, -1) => Error: increment must be greater than 0!
 
Start equal stop: zero increment : Range( 2, 2, 0) => Error: increment must be greater than 0!
 
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>
 
=={{header|ALGOL W}}==
Using the Algol W for loop and limiting the sequence to 10 values. The Algol W for loop considers the sign of the increment value when deciding whether to terminate when the loop counter exceeds the stop value (positive increment) or is smaller than the stop value (negative increment).
<langsyntaxhighlight lang="algolw">begin
% sets the first n elements of s to the sequences of values specified by start, stop and increment %
% s( 0 ) is set to the number of elements of s that have been set, in case the sequence ends before n %
Line 90 ⟶ 247:
testSequence( 2, 2, 0, "Start equal stop: zero increment" );
testSequence( 0, 0, 0, "Start equal stop equal zero: zero increment" )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 103 ⟶ 260:
Start equal stop equal zero: zero increment : 0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|Arturo}}==
<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">
<lang AWK>
# syntax: GAWK -f LOOPS_WRONG_RANGES.AWK
BEGIN {
Line 130 ⟶ 321:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>start,stop,increment,comment
<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 142 ⟶ 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>
 
</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
dim start(9) : dim fin(9) : dim inc(9) : dim cmt$(9)
start[1] = -2 : fin[1] = 2 : inc[1] = 1 : cmt$[1] = "Normal"
start[2] = -2 : fin[2] = 2 : inc[2] = 0 : cmt$[2] = "Zero increment"
start[3] = -2 : fin[3] = 2 : inc[3] = -1 : cmt$[3] = "Increments away from stop value"
start[4] = -2 : fin[4] = 2 : inc[4] = 10 : cmt$[4] = "First increment is beyond stop value"
start[5] = 2 : fin[5] = -2 : inc[5] = 1 : cmt$[5] = "Start more than stop: positive increment"
start[6] = 2 : fin[6] = 2 : inc[6] = 1 : cmt$[6] = "Start equal stop: positive increment"
start[7] = 2 : fin[7] = 2 : inc[7] = -1 : cmt$[7] = "Start equal stop: negative increment"
start[8] = 2 : fin[8] = 2 : inc[8] = 0 : cmt$[8] = "Start equal stop: zero increment"
start[9] = 0 : fin[9] = 0 : inc[9] = 0 : cmt$[9] = "Start equal stop equal zero: zero increment"
 
for i = 1 to 9
contar = 0
print cmt$[i]
print " Bucle de "; start[i]; " a "; fin[i]; " en incrementos de "; inc[i]
for vr = start[i] to fin[i] step inc[i]
print " Índice del bucle = "; vr
contar = contar + 1
if contar = 10 then
print " Saliendo de un bucle infinito"
exit for
endif
next vr
print " Bucle terminado" & chr(10) & chr(10)
next i
end</syntaxhighlight>
{{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}}==
C's 'for' statement appears to fit the bill here and so we use it directly to generate the required ranges of numbers though, as some of the ranges will be infinite, we limit the output to a maximum of 10 numbers.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define TRUE 1
Line 189 ⟶ 938:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 224 ⟶ 973:
{{trans|Visual Basic .NET}}
Behavior for naïve translation is different from VB.NET (and identical to C), as it does not handle an "overshot" iteration variable when the increment is negative.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
 
Line 272 ⟶ 1,021:
Console.WriteLine();
}
}</langsyntaxhighlight>
 
{{out|note=identical to C}}
Line 288 ⟶ 1,037:
{{trans|C|version=287385}}
Just for fun; some strictly unnecessary changes made to be closer to idiomatic C#.
<langsyntaxhighlight lang="csharp">using System;
 
static class Program {
Line 325 ⟶ 1,074:
return 0;
}
}</langsyntaxhighlight>
 
{{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}}==
<langsyntaxhighlight lang="lisp">
(dolist (lst '((-2 2 1 "Normal") ; Iterate the parameters list `start' `stop' `increment' and `comment'
(-2 2 0 "Zero increment")
Line 350 ⟶ 1,163:
(reverse result))) ; The in(de)creased numbers into result
(push i result))) ; Add the number to result
</syntaxhighlight>
</lang>
 
{{out}}
Line 366 ⟶ 1,179:
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Wrong_ranges;
 
Line 422 ⟶ 1,235:
Example(0, 0, 0, 'Start equal stop equal zero: zero increment');
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Normal ------------------------------------------- -2 -1 0 1 2
Line 436 ⟶ 1,249:
Version with TEnumerate:
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Wrong_rangesEnumerator;
 
Line 520 ⟶ 1,333:
Example(0, 0, 0, 'Start equal stop equal zero: zero increment');
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>Normal ------------------------------------------- -2 -1 0 1 2
Line 533 ⟶ 1,346:
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
example
-- Loops/Wrong ranges
Line 548 ⟶ 1,361:
end
 
</syntaxhighlight>
</lang>
{{out}}
In this example, we are using the "symbolic form" of the Eiffel across loop, such as: ⟳ ¦ ⟲
Line 563 ⟶ 1,376:
=={{header|Factor}}==
<code><range></code> divides by the step value, so a step of 0 causes a divide by zero exception. For the purpose of getting through all the examples, the exceptions are dropped and execution continues, which in general should be avoided.
<langsyntaxhighlight lang="factor">USING: continuations formatting io kernel math.ranges
prettyprint sequences ;
 
Line 578 ⟶ 1,391:
[ "%2d %2d %2d <range> => " printf ]
[ try-range ] 3bi
] each</langsyntaxhighlight>
{{out}}
<pre>
Line 591 ⟶ 1,404:
0 0 0 <range> => Exception: divide by zero.
</pre>
 
=={{header|Fermat}}==
Although the examples are listed together, I ran them one at a time.
<syntaxhighlight lang="fermat">for a = -2 to 2 by 1 do !(a,' '); od;
for a = -2 to 2 by 0 do !(a,' '); od;
for a = -2 to 2 by -1 do !(a,' '); od;
for a = -2 to 2 by 10 do !(a,' '); od;
for a = 2 to -2 by 1 do !(a,' '); od;
for a = 2 to 2 by 1 do !(a,' '); od;
for a = 2 to 2 by -1 do !(a,' '); od;
for a = 2 to 2 by 0 do !(a,' '); od;
for a = 0 to 0 by 0 do !(a,' '); od;</syntaxhighlight>
{{out}}
<pre>
-2 -1 0 1 2
-2 -2 -2 -2 -2 .....
<no output>
-2
<no output>
2
2
2 2 2 2 2 2 2 2 2 2 .....
0 0 0 0 0 0 0 0 0 0 .....
</pre>
 
=={{header|Forth}}==
Line 596 ⟶ 1,433:
Forth, being a language that assumes a smart programmer, has absolutely no qualms with integer overflow or zero increments; it simply does what you told it to, not what you meant. This is, of course, dangerous code, so another looping construct is provided that ''does'' check whether the bounds and increment are valid.
===DO===
<langsyntaxhighlight lang="forth">: test-seq ( start stop inc -- )
cr rot dup ." start: " 2 .r
rot dup ." stop: " 2 .r
Line 609 ⟶ 1,446:
2 2 -1 test-seq
2 2 0 test-seq
0 0 0 test-seq</langsyntaxhighlight>
{{out}}
Some of these loop infinitely, and some under/overflow, so for the sake of brevity long outputs will be truncated by <code>...</code>.
Line 625 ⟶ 1,462:
===?DO===
This is almost exactly the same as the above DO, but the bounds are checked for validity first.
<langsyntaxhighlight lang="forth">: test-seq ( start stop inc -- )
cr rot dup ." start: " 2 .r
rot dup ." stop: " 2 .r
Line 638 ⟶ 1,475:
2 2 -1 test-seq
2 2 0 test-seq
0 0 0 test-seq</langsyntaxhighlight>
{{out}}
Some of these loop infinitely, and some under/overflow, so for the sake of brevity long outputs will be truncated by <code>...</code>. If there is no output beyond the <code>|</code> symbol, then the loop was not entered.
Line 652 ⟶ 1,489:
start: 0 stop: 0 inc: 0 |
</pre>
 
=={{header|FreeBASIC}}==
<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
</lang>
{{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 777 ⟶ 1,502:
 
It appears that either #1 or #4 fits the requirements of this task so I've written 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 #4 to the resulting sequence. All sequences include the stop value if it's actually reached.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 814 ⟶ 1,539:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 847 ⟶ 1,572:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List
 
main = putStrLn $ showTable True '|' '-' '+' table
Line 871 ⟶ 1,596:
top = replicate (length hr) hor
bss = map (\ps -> map (flip replicate ' ') $ zipWith (-) ms ps) $ vss
zss@(z:zs) = zipWith (\us bs -> (concat $ zipWith (\x y -> (ver:x) ++ y) us bs) ++ [ver]) contents bss</langsyntaxhighlight>
{{out}}
<pre>+-----+----+---------+-------------------------------------------+---------------------+---------------------------------+
Line 891 ⟶ 1,616:
Instantiation of an a priori invalid range is a fatal error.
 
<langsyntaxhighlight lang="huginn">import Algorithms as algo;
 
class Example {
Line 925 ⟶ 1,650:
);
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,016 ⟶ 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}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,061 ⟶ 1,820:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,073 ⟶ 1,832:
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|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
(possibly empty) stream of numbers if given numeric inputs.
 
It meets the task criterion, e.g. `range(0;1;0.4)` generates 3 numbers.
 
In fact, it is quite similar to C's `for (i=start; i < stop; i+=increment)`
except that if `increment` is 0 or in the "wrong" direction, then nothing is emitted.
 
None of the cases enumerated in the task description results in an error,
and in all but two cases, the result is the empty stream.
 
To streamline the presentation of results, rather than showing the stream
of values generated by stream(start;stop;increment), we will show the
array of the generated values.
<pre>
[range(-2; 2; 1)] #=> [-2,-1,0,1]
 
[range(-2; 2; 0)] #=> []
 
[range(-2; 2; -1)] #=> []
 
[range(-2; 2; 10)] # [-2]
 
[range(2; -2; 1)] #=> []
 
[range(2; 2; 1)] #=> []
 
[range(2; 2; 0)] #=> []
 
[range(0; 0; -1)] #=> []
</pre>
 
=={{header|Julia}}==
Julia has a start:increment:stop iterator syntax, which allows negative increments, but not zero increments.
<syntaxhighlight lang="julia">
<lang Julia>
collect(-2:1:2) # → [-2, -1, 0, 1, 2]
collect(-2:0:2) # fails
Line 1,087 ⟶ 1,882:
collect(2:0:2) # fails
collect(0:0:0) # fails
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Although Kotlin's 'for' statement can deal with a range of integers, the increment must be positive and so it cannot be used for this task. We therefore use instead a 'while' statement to generate the same sequence as a C language 'for' statement would (limited to a maximum of 10 elements as some sequences will be infinite) and wrap it in a function.
<langsyntaxhighlight lang="scala">// Version 1.2.70
 
class Example(val start: Int, val stop: Int, val incr: Int, val comment: String)
Line 1,130 ⟶ 1,925:
println()
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,163 ⟶ 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).
 
<lang langur>val .data = q:block END
start stop increment comment
-2 2 1 Normal
Line 1,189 ⟶ 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,197 ⟶ 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"
}
}
}
}</lang>
</syntaxhighlight>
 
{{out}}
Line 1,234 ⟶ 2,022:
result: []
</pre>
 
=={{header|Lua}}==
{{works with|Lua|5.0 - 5.4, and perhaps beyond, but not before}}
Lua >= 5.4 will not allow a zero increment, causes a runtime error (trapped below).
Lua 4.0 - 5.3 will allow a zero increment, causing either: no execution (start<stop) or infinite execution (start>=stop).
''(though Lua 4.0 is not supported in the code below for other reasons.)''
Lua <4.0 didn't have numeric for loops, so behaviour would depend on how an equivalent repeat/while loop was written.
<syntaxhighlight lang="lua">tests = {
{ -2, 2, 1, "Normal" },
{-2, 2, 0, "Zero increment" }, -- 5.4 error
{-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" }, -- 5.4 error
{ 0, 0, 0, "Start equal stop equal zero: zero increment" } -- 5.4 error
}
unpack = unpack or table.unpack -- polyfill 5.2 vs 5.3
for _,test in ipairs(tests) do
start,stop,incr,desc = unpack(test)
io.write(string.format("%-44s (%2d, %2d, %2d) : ",desc,start,stop,incr))
local sta,err = pcall(function()
local n = 0
for i = start,stop,incr do
io.write(string.format("%2d, ", i))
n=n+1 if n>=10 then io.write("...") break end
end
io.write("\n")
end)
if err then io.write("RUNTIME ERROR!\n") end
end</syntaxhighlight>
{{out}}
Using 5.3:
<pre>Normal (-2, 2, 1) : -2, -1, 0, 1, 2,
Zero increment (-2, 2, 0) :
Increments away from stop value (-2, 2, -1) :
First increment is beyond stop value (-2, 2, 10) : -2,
Start more than stop: positive increment ( 2, -2, 1) :
Start equal stop: positive increment ( 2, 2, 1) : 2,
Start equal stop: negative increment ( 2, 2, -1) : 2,
Start equal stop: zero increment ( 2, 2, 0) : 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, 0, 0, 0, ...</pre>
Using 5.4:
<pre>Normal (-2, 2, 1) : -2, -1, 0, 1, 2,
Zero increment (-2, 2, 0) : RUNTIME ERROR!
Increments away from stop value (-2, 2, -1) :
First increment is beyond stop value (-2, 2, 10) : -2,
Start more than stop: positive increment ( 2, -2, 1) :
Start equal stop: positive increment ( 2, 2, 1) : 2,
Start equal stop: negative increment ( 2, 2, -1) : 2,
Start equal stop: zero increment ( 2, 2, 0) : RUNTIME ERROR!
Start equal stop equal zero: zero increment ( 0, 0, 0) : RUNTIME ERROR!</pre>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple"># Normal
seq(-2..2, 1);
 
Line 1,262 ⟶ 2,103:
# Start equal stop equal zero: zero increment
seq(0..0, 0);
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,275 ⟶ 2,116:
0
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
All functions like Do, Table, Range, Sum, NSum, Product and NProduct use same 'iterator specification':
<syntaxhighlight lang="mathematica">Table[i, {i, -2, 2, 1}]
Table[i, {i, -2, 2, 0}]
Table[i, {i, -2, 2, -1}]
Table[i, {i, -2, 2, 10}]
Table[i, {i, 2, -2, 1}]
Table[i, {i, 2, 2, 1}]
Table[i, {i, 2, 2, -1}]
Table[i, {i, 2, 2, 0}]
Table[i, {i, 0, 0, 0}]</syntaxhighlight>
{{out}}
<pre>{-2, -1, 0, 1, 2}
During evaluation of In[…]:= Table::iterb: Iterator {i,-2,2,0} does not have appropriate bounds.
Table[i, {i, -2, 2, 0}]
{}
{-2}
{}
{2}
{2}
{2}
{0}</pre>
 
=={{header|Nim}}==
Line 1,281 ⟶ 2,145:
The following program display the values yielded by “countup” our “countdown” for the given ranges:
 
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat
 
proc displayRange(first, last, step: int) =
Line 1,294 ⟶ 2,158:
(-2, 2, 10), (2, -2, 1), (2, 2, 1),
(2, 2, -1), (2, 2, 0), (0, 0, 0)]:
displayRange(f, l, s)</langsyntaxhighlight>
 
{{out}}
Line 1,309 ⟶ 2,173:
=={{header|Perl}}==
None of these sequences are 'errors', though some are of infinite length, and #5 has a length of zero.
<langsyntaxhighlight lang="perl">for $i (
[ -2, 2, 1], #1 Normal
[ -2, 2, 0], #2 Zero increment
Line 1,333 ⟶ 2,197:
return $term > $stop ? '' : $term;
}
}</langsyntaxhighlight>
{{out}}
<pre>start: -2 stop: 2 incr: 1 | -2 -1 0 1 2
Line 1,346 ⟶ 2,210:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
Phix for loops do not allow a zero step (neither are any floating point values permitted).<br>
Phix for loops do not allow a zero step, neither are any floating point values permitted.<br>
The following shows the behaviour of both for and while loops, and the latter has a couple of
additional commented out termination checks that might be appropriate in some cases.
<lang Phix>procedure test(integer start, stop, step, string legend, bool bFor)
sequence res = {}
if bFor then
try
for i=start to stop by step do
res &= i
if length(res)>9 then exit end if
end for
res = sprint(res)
catch e
res = e[E_USER]
end try
else
integer i = start
while (step>=0 and i<=stop)
or (step<=0 and i>=stop) do
res &= i
if length(res)>9 then exit end if
-- if i=stop then exit end if
-- if step=0 then exit end if
i += step
end while
res = sprint(res)
end if
printf(1,"%-43s: %s\n",{legend,res})
end procedure
 
<!--<syntaxhighlight lang="phix">-->
for i=1 to 2 do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">stop</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">legend</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bFor</span><span style="color: #0000FF;">)</span>
?iff(i=1?"for":"while")
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
test(-2, 2, 1, "Normal" ,i=1)
<span style="color: #008080;">if</span> <span style="color: #000000;">bFor</span> <span style="color: #008080;">then</span>
test(-2, 2, 0, "Zero increment" ,i=1)
<span style="color: #008080;">try</span>
test(-2, 2,-1, "Increments away from stop value" ,i=1)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">start</span> <span style="color: #008080;">to</span> <span style="color: #000000;">stop</span> <span style="color: #008080;">by</span> <span style="color: #000000;">step</span> <span style="color: #008080;">do</span>
test(-2, 2,10, "First increment is beyond stop value" ,i=1)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
test( 2,-2, 1, "Start more than stop: positive increment" ,i=1)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)></span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
test( 2, 2, 1, "Start equal stop: positive increment" ,i=1)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
test( 2, 2,-1, "Start equal stop: negative increment" ,i=1)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
test( 2, 2, 0, "Start equal stop: zero increment" ,i=1)
<span style="color: #008080;">catch</span> <span style="color: #000000;">e</span>
test( 0, 0, 0, "Start equal stop equal zero: zero increment",i=1)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">e</span><span style="color: #0000FF;">[</span><span style="color: #000000;">E_USER</span><span style="color: #0000FF;">]</span>
puts(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">try</span>
end for</lang>
<span style="color: #008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">start</span>
<span style="color: #008080;">while</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">step</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">stop</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">step</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">stop</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">i</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)></span><span style="color: #000000;">9</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">-- if i=stop then exit end if
-- if step=0 then exit end if</span>
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">step</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-43s: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">legend</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">?</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"for"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"while"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Normal"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Zero increment"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Increments away from stop value"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"First increment is beyond stop value"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Start more than stop: positive increment"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Start equal stop: positive increment"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Start equal stop: negative increment"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Start equal stop: zero increment"</span> <span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Start equal stop equal zero: zero increment"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
 
{{out}}
<pre>
Line 1,413 ⟶ 2,282:
Start equal stop equal zero: zero increment: {0,0,0,0,0,0,0,0,0,0}
</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">loops:procedure options (main);
declare i fixed binary;
 
Line 1,446 ⟶ 2,316:
put skip list (' 0 to 0 by 0: infinite loop, prints 0');
 
end loops;</langsyntaxhighlight>
Output:
<pre>
Line 1,461 ⟶ 2,331:
=={{header|Python}}==
Python has the [https://docs.python.org/3/library/functions.html#func-range range] function.
<langsyntaxhighlight lang="python">import re
from itertools import islice # To limit execution if it would generate huge values
# list(islice('ABCDEFG', 2)) --> ['A', 'B']
Line 1,495 ⟶ 2,365:
else:
print(' =', str(values[:22])[:-1], '...')
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,527 ⟶ 2,397:
=={{header|R}}==
Aside from the second case, this behaviour is explained by two sentences of seq's documentation: "''generates '''from''', '''from'''+'''by''', ..., up to the sequence value less than or equal to '''to'''. Specifying '''to''' - '''from''' and '''by''' of opposite signs is an error.''" As we can see, '''from''' is always included whenever an error is not thrown and '''to''' will be missed if it cannot be reached.
<langsyntaxhighlight rlang="rsplus">seq(from = -2, to = 2, by = 1)#Output: -2 -1 0 1 2
seq(from = -2, to = 2, by = 0)#Fails: "invalid '(to - from)/by'"
seq(from = -2, to = 2, by = -1)#Fails: As in the notes above - "Specifying to - from and by of opposite signs is an error."
Line 1,535 ⟶ 2,405:
seq(from = 2, to = 2, by = -1)#Output: 2
seq(from = 2, to = 2, by = 0)#Output: 2
seq(from = 0, to = 0, by = 0)#Output: 0</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require racket/sandbox)
Line 1,559 ⟶ 2,429:
(with-handlers ([exn:fail:resource? (thunk* 'timeout)])
(with-limits 1 #f
(sequence->list (in-range st ed inc))))))</langsyntaxhighlight>
 
{{out}}
Line 1,604 ⟶ 2,474:
Also note: The iterator function for the sequence is literally a function. It is any expression that produces a value. These sequences all use simple arithmatic increments but that is not a limitation of the sequence operator.
 
<syntaxhighlight lang="raku" perl6line># Given sequence definitions
# start stop inc. Comment
for -2, 2, 1, # Normal
Line 1,640 ⟶ 2,510:
 
# Iterate strings backwards.
put 'xp' … 'xf';</langsyntaxhighlight>
{{out}}
<pre>Start: -2, Stop: 2, Increment: 1 | -2 -1 0 1 2
Line 1,664 ⟶ 2,534:
 
The REXX language will cause the '''do''' loop index to be checked at the "head" of the '''do''' loop to see if the index falls within the specified iteration range &nbsp; (if there is one).
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates several versions of DO loops with "unusual" iterations. */
@.=; @.1= ' -2 2 1 ' /*"normal". */
@.2= ' -2 2 0 ' /*"normal", zero increment.*/
Line 1,689 ⟶ 2,559:
say center(' end of performing DO loop number ' k " with range: " x y z, 79, '─')
say
end /*k*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|:}}
<pre>
Line 1,768 ⟶ 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}}==
A Range with a step (without a block) results in an ArthmeticSequence (an object). A step size of zero is perfectly valid.
To illustrate, a representation of the ArithmicSequence and it's size are shown.
<langsyntaxhighlight lang="ruby">examples = [
[ -2, 2, 1],
[ -2, 2, 0],
Line 1,788 ⟶ 2,681:
puts "#{as.inspect} size: #{as.size}"
end
</syntaxhighlight>
</lang>
{{out}}<pre>((-2..2).step(1)) size: 5
((-2..2).step(0)) size: Infinity
Line 1,801 ⟶ 2,694:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: testLoop (in integer: start, in integer: stop, in integer: incr, in string: comment) is func
Line 1,837 ⟶ 2,730:
testLoop( 2, 2, 0, "Start equal stop: zero increment");
testLoop( 0, 0, 0, "Start equal stop equal zero: zero increment");
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,870 ⟶ 2,763:
 
=={{header|Smalltalk}}==
The basic loop is in the expression: <langsyntaxhighlight lang="smalltalk">startExpr to:stopExpr by:incExpr do:[..]</langsyntaxhighlight>
The rest in the code is sugar for a nice output and a breakout if running endless.
 
<langsyntaxhighlight lang="smalltalk">MAX_ITER := 15.
#(
(-2 2 1 'Normal')
Line 1,904 ⟶ 2,797:
] valueWithExit.
Transcript cr.
].</langsyntaxhighlight>
{{out}}
<pre>Normal............................................ -2 -1 0 1 2
Line 1,919 ⟶ 2,812:
=={{header|Vala}}==
{{trans|C#}}
<langsyntaxhighlight lang="vala">static void example(int start, int stop, int increment, string comment) {
const int MAX_ITER = 9;
int iteration = 0;
Line 1,940 ⟶ 2,833:
example(2, 2, 0, "Start equals stop: zero increment");
example(0, 0, 0, "Start equals stop equal zero: zero increment");
}</langsyntaxhighlight>
 
{{out}}
Line 1,956 ⟶ 2,849:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub LoopsWrongRanges()
Call Example(-2, 2, 1, "Normal")
Call Example(-2, 2, 0, "Zero increment")
Line 1,981 ⟶ 2,874:
Debug.Print comment & vbCrLf
End Sub
</langsyntaxhighlight>{{out}}<pre>-2 2 1 | -2,-1,0,1,2,
Normal
 
Line 2,008 ⟶ 2,901:
Start equal stop equal zero: zero increment</pre>
 
=={{header|VisualV Basic .NET(Vlang)}}==
Vlang has only one loop, a 'for' statement, which supports six different syntactical forms commonly found in other C-family languages:
'''Compiler:''' >= Visual Studio 2012
 
1. A C-like 'for' loop with initialization, condition and increment sections (`for i := 0; i < 10; i += 2 {`).
VB.NET's For loop accepts a starting and ending value and optional step.
 
2. The 'while' loop functionality (condition only) (`for i <= 100 {`)
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).
 
3. Infinite loop, equivalent to for(;;) (all sections omitted) (`for {`)
The number of iterations is limited to 10 by the test code.
 
4. Looping over a range of values (`for i in 0..5 {`).
<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
 
5. Looping over map/array (`for key, value in m {` or `for idx, value in l {` or without idx `for value in l {`)
' 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
 
6. Custom iterators (see [docs](https://github.com/vlang/v/blob/master/doc/docs.md#custom-iterators))
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) & " ")
 
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.
Const MAX_ITER = 9
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">struct Seq {
start int
stop int
incr int
comment string
}
const examples = [
Seq{-2, 2, 1, "Normal"},
Seq{-2, 2, 0, "Zero increment"},
Seq{-2, 2, -1, "Increments away from stop value"},
Seq{-2, 2, 10, "First increment is beyond stop value"},
Seq{2, -2, 1, "Start more than stop: positive increment"},
Seq{2, 2, 1, "Start equal stop: positive increment"},
Seq{2, 2, -1, "Start equal stop: negative increment"},
Seq{2, 2, 0, "Start equal stop: zero increment"},
Seq{0, 0, 0, "Start equal stop equal zero: zero increment"},
]
fn sequence(s Seq, limit int) []int {
mut seq := []int{}
for i, c := s.start, 0; i <= s.stop && c < limit; i, c = i+s.incr, c+1 {
seq << i
}
return seq
}
fn main() {
limit := 10
for ex in examples {
println(ex.comment)
print("Range($ex.start, $ex.stop, $ex.incr) -> ")
println(sequence(ex, limit))
println('')
}
}</syntaxhighlight>
 
{{out}}
Dim iteration = 0
<pre>
' The For Each loop enumerates the IEnumerable.
Normal
For Each i In Range(start, [stop], increment)
Range(-2, 2, 1) -> [-2, -1, 0, 1, 2]
Console.Write("{0,2} ", i)
 
Zero increment
iteration += 1
Range(-2, 2, 0) -> [-2, -2, -2, -2, -2, -2, -2, -2, -2, -2]
If iteration > MAX_ITER Then Exit For
Next
 
Increments away from stop value
Console.WriteLine()
Range(-2, 2, -1) -> [-2, -3, -4, -5, -6, -7, -8, -9, -10, -11]
End Sub
End Module</lang>
 
First increment is beyond stop value
{{out}}
Range(-2, 2, 10) -> [-2]
<pre>Normal ------------------------------------------- -2 -1 0 1 2
 
Zero increment ----------------------------------- -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
Start more than stop: positive increment
Increments away from stop value ------------------
Range(2, -2, 1) -> []
First increment is beyond stop value ------------- -2
 
Start more than stop: positive increment ---------
Start equal stop: positive increment ------------- 2
Range(2, 2, 1) -> [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: zeronegative increment ------ 0 0 0 0 0 0 0 0 0 0</pre>
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|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var loop = Fn.new { |start, stop, inc|
Line 2,088 ⟶ 3,006:
[-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]
]
for (test in tests) loop.call(test[0], test[1], test[2])</langsyntaxhighlight>
 
{{out}}
Line 2,103 ⟶ 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;
<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</lang>
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}}==
<langsyntaxhighlight lang="zkl">// zero increment (ie infnite loop) throws an error
// if stop is "*", the loop is has no end (ie infinite)
// stop is included unless step steps skips it
Line 2,232 ⟶ 3,148:
T( 2,2,1),T( 2,2,-1),T( 2,2,0),T( 0,0,0),
T(0.0, (0.0).pi, 0.7853981633974483), T("a","e",1), T("e","a",1) )
.apply2(looper); // apply2 is apply (map) without saving results</langsyntaxhighlight>
{{out}}
<pre>
885

edits