|
The goto statement in Lua
In the past there was considerable debate in academia and industry on the merits of the use of goto statements, and indeed Lua didn't have them until release 5.2. Well used in Lua, they can provide an escape from inner loops, jumping to a label using a ::label:: format. For example:
for z=1,10 do
for y=1,10 do
for x=1,10 do
if x^2 + y^2 == z^2 then
print('found a Pythagorean triple:', x, y, z)
goto done
end
end
end
end
::done::
You cannot jump into an inner scope which includes local variables, and there are some further (sensible) limitations. This use of the goto to get out of multiple nested loops save you a whole series of nested checks or booleans.
There's a complete example showing sample output too [here]. (written 2012-04-06, updated 2012-04-07)
Associated topics are indexed under U198 - New in Lua 5.2 [3690] Changes in Lua 5.2 - Garbage Collection - (2012-04-08) [3687] Binary / bitwise operations in Lua with the standard bit32 library - (2012-04-06) [3683] Weak references in Lua - what are they, and why use them? - (2012-04-04) U103 - Lua - Conditionals and loops [3558] Python or Lua - which should I use / learn? - (2011-12-21) [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18) [2455] Lua examples - coroutines, error handling, objects, etc - (2009-10-15) [2454] For loops in Lua - (2009-10-14) [2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12) [1738] Clean code, jump free (Example in Lua) - (2008-08-06) [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04) [1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)
4ac2
Some other Articles
Can I cycle or ride my mobility scooter on the pavement?Shopkeepers take an Easter break tooThe goto statement in LuaProgramming Standards in LuaOnce upon a Maundy ThursdayMelksham Business NewsreelKicking up a stink, the Victorian way?
|
4084 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|