Loops/Wrong ranges: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Lua}}: added Lua solution) |
||
Line 1,478: | Line 1,478: | ||
result: [] |
result: [] |
||
</pre> |
</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. |
|||
<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</lang> |
|||
{{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}}== |
=={{header|Maple}}== |