Loops/Wrong ranges: Difference between revisions

(→‎{{header|Lua}}: added Lua solution)
 
(24 intermediate revisions by 16 users not shown)
Line 51:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F displayRange(first, last, step)
print(‘(#2, #2, #2): ’.format(first, last, step), end' ‘’)
I step == 0
Line 61:
(-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 82:
 
The following solution follows the logic of a range as specified in the task description.
<langsyntaxhighlight 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;
Line 132:
New_line;
end loop;
end Main;</langsyntaxhighlight>
{{out}}
<pre>
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>
 
=={{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 196 ⟶ 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 210 ⟶ 261:
</pre>
 
=={{header|Applesoft BASICArturo}}==
<syntaxhighlight lang="arturo">
<lang ApplesoftBasic>0 LIMIT = 10 :MAX=1E37
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
20 DATA-2,2,0,ZERO INCREMENT
Line 234 ⟶ 359:
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</langsyntaxhighlight>
{{out}}
<pre>
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}}==
<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)
}
</lang>
{{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}}
<langsyntaxhighlight 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"
Line 325 ⟶ 409:
print " Bucle terminado" & chr(10) & chr(10)
next i
end</langsyntaxhighlight>
{{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 373 ⟶ 938:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 408 ⟶ 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 456 ⟶ 1,021:
Console.WriteLine();
}
}</langsyntaxhighlight>
 
{{out|note=identical to C}}
Line 472 ⟶ 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 509 ⟶ 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 534 ⟶ 1,163:
(reverse result))) ; The in(de)creased numbers into result
(push i result))) ; Add the number to result
</syntaxhighlight>
</lang>
 
{{out}}
Line 550 ⟶ 1,179:
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Wrong_ranges;
 
Line 606 ⟶ 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 620 ⟶ 1,249:
Version with TEnumerate:
{{libheader| System.SysUtils}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Wrong_rangesEnumerator;
 
Line 704 ⟶ 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 717 ⟶ 1,346:
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
example
-- Loops/Wrong ranges
Line 732 ⟶ 1,361:
end
 
</syntaxhighlight>
</lang>
{{out}}
In this example, we are using the "symbolic form" of the Eiffel across loop, such as: ⟳ ¦ ⟲
Line 747 ⟶ 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 762 ⟶ 1,391:
[ "%2d %2d %2d <range> => " printf ]
[ try-range ] 3bi
] each</langsyntaxhighlight>
{{out}}
<pre>
Line 778 ⟶ 1,407:
=={{header|Fermat}}==
Although the examples are listed together, I ran them one at a time.
<langsyntaxhighlight 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;
Line 786 ⟶ 1,415:
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;</langsyntaxhighlight>
{{out}}
<pre>
Line 804 ⟶ 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 817 ⟶ 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 833 ⟶ 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 846 ⟶ 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 860 ⟶ 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 985 ⟶ 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 1,022 ⟶ 1,539:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,055 ⟶ 1,572:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List
 
main = putStrLn $ showTable True '|' '-' '+' table
Line 1,079 ⟶ 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 1,099 ⟶ 1,616:
Instantiation of an a priori invalid range is a fatal error.
 
<langsyntaxhighlight lang="huginn">import Algorithms as algo;
 
class Example {
Line 1,133 ⟶ 1,650:
);
}
}</langsyntaxhighlight>
 
{{out}}
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}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,269 ⟶ 1,820:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
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}}==
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,331 ⟶ 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,374 ⟶ 1,925:
println()
}
}</langsyntaxhighlight>
 
{{output}}
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).
 
<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"
}
}
}
}</lang>
</syntaxhighlight>
 
{{out}}
Line 1,485 ⟶ 2,029:
''(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.
<langsyntaxhighlight Lualang="lua">tests = {
{ -2, 2, 1, "Normal" },
{-2, 2, 0, "Zero increment" }, -- 5.4 error
Line 1,509 ⟶ 2,053:
end)
if err then io.write("RUNTIME ERROR!\n") end
end</langsyntaxhighlight>
{{out}}
Using 5.3:
Line 1,533 ⟶ 2,077:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple"># Normal
seq(-2..2, 1);
 
Line 1,559 ⟶ 2,103:
# Start equal stop equal zero: zero increment
seq(0..0, 0);
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,575 ⟶ 2,119:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
All functions like Do, Table, Range, Sum, NSum, Product and NProduct use same 'iterator specification':
<langsyntaxhighlight Mathematicalang="mathematica">Table[i, {i, -2, 2, 1}]
Table[i, {i, -2, 2, 0}]
Table[i, {i, -2, 2, -1}]
Line 1,583 ⟶ 2,127:
Table[i, {i, 2, 2, -1}]
Table[i, {i, 2, 2, 0}]
Table[i, {i, 0, 0, 0}]</langsyntaxhighlight>
{{out}}
<pre>{-2, -1, 0, 1, 2}
Line 1,601 ⟶ 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,614 ⟶ 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,629 ⟶ 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,653 ⟶ 2,197:
return $term > $stop ? '' : $term;
}
}</langsyntaxhighlight>
{{out}}
<pre>start: -2 stop: 2 incr: 1 | -2 -1 0 1 2
Line 1,671 ⟶ 2,215:
additional commented out termination checks that might be appropriate in some cases.
 
<!--<langsyntaxhighlight Phixlang="phix">-->
<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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 1,712 ⟶ 2,256:
<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>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 1,740 ⟶ 2,284:
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">loops:procedure options (main);
declare i fixed binary;
 
Line 1,772 ⟶ 2,316:
put skip list (' 0 to 0 by 0: infinite loop, prints 0');
 
end loops;</langsyntaxhighlight>
Output:
<pre>
Line 1,787 ⟶ 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,821 ⟶ 2,365:
else:
print(' =', str(values[:22])[:-1], '...')
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,853 ⟶ 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 lang="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,861 ⟶ 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,885 ⟶ 2,429:
(with-handlers ([exn:fail:resource? (thunk* 'timeout)])
(with-limits 1 #f
(sequence->list (in-range st ed inc))))))</langsyntaxhighlight>
 
{{out}}
Line 1,930 ⟶ 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,966 ⟶ 2,510:
 
# Iterate strings backwards.
put 'xp' … 'xf';</langsyntaxhighlight>
{{out}}
<pre>Start: -2, Stop: 2, Increment: 1 | -2 -1 0 1 2
Line 1,990 ⟶ 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 2,015 ⟶ 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 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}}==
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 2,114 ⟶ 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 2,127 ⟶ 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 2,163 ⟶ 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 2,196 ⟶ 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 2,230 ⟶ 2,797:
] valueWithExit.
Transcript cr.
].</langsyntaxhighlight>
{{out}}
<pre>Normal............................................ -2 -1 0 1 2
Line 2,245 ⟶ 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 2,266 ⟶ 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 2,282 ⟶ 2,849:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub LoopsWrongRanges()
Call Example(-2, 2, 1, "Normal")
Call Example(-2, 2, 0, "Zero increment")
Line 2,307 ⟶ 2,874:
Debug.Print comment & vbCrLf
End Sub
</langsyntaxhighlight>{{out}}<pre>-2 2 1 | -2,-1,0,1,2,
Normal
 
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.
 
<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</lang>
 
{{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,409 ⟶ 2,917:
 
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|goGo}}
<syntaxhighlight lang="v (vlang)">struct Seq {
start int
stop int
incr int
comment string
}
Line 2,445 ⟶ 2,953:
println('')
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,479 ⟶ 2,987:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var loop = Fn.new { |start, stop, inc|
Line 2,498 ⟶ 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,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;
<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,642 ⟶ 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