|
Short circuit evaluation (lazy operator) in Lua
In Lua, if a partial expression evaluates to true the following section will NOT be evaluated if it follows an or because the whole thing cannot be true.
If a partial expression evaluates to false, the following section will NOT be evaluated if it follow an and because the whole thing must be true.
If you find this a bit theoretic, try a real life example ...
If you're going to buy a kebab OR Fish and Chips and you find the kebab shop open, you won't bother to carry on up the road to the chippy because you have already met your needs.
If you require a hammer AND nails, but you can't find a hammer, you won't go and search for the nails because even if you find them, they're not going to be much use to you!
Let's see some code ...
function agelimit(incoming)
return type(incoming) == "number" and incoming or 18
end
-- And the code to call that function ...
print (agelimit(10))
print (agelimit(12))
print (agelimit("fourteen"))
print (agelimit(16))
And when you run that, you get ...
[trainee@easterton u106]$ lua lazy
10
12
18
16
[trainee@easterton u106]$
If you're wanting to learn to program in Lua and you're looking for a training course, please ask us (email info@wellho.net). Although we're not [yet?] offering a scheduled public course, we are running private courses for two different groups:
• Delegates with no prior programming experience
• Delegates who have programming experience, but are new to Lua.
Course can be run for groups of 3 to 10 delegates, and vary in length from 2 to 4 days depending on the background of the delegates, the size of the class, and just how deep you want us to go into certain aspects.
Some sample quotations:
• At our training centre - Melksham, Wiltshire, England
• At your office in the UK - example - Coventry, England
• At your office in the Ireland - example - Dublin
• At your office in the USA - example - New York State (written 2008-06-22 00:42:39)
Associated topics are indexed under U103 - Lua - Conditionals and loopsU106 - Lua - Functions.
Some other Articles
Some sideways thoughts on the newscannot restore segment prot after reloc message - PHP / httpdVirtual Hosting on Apache httpd - a primer exampleFTP - how not to corrupt data (binary v ascii)Short circuit evaluation (lazy operator) in LuaTwo PHP include files to make easy web site indexing and navigationPlenty of car parking at Well House Manor, Hotel, MelkshamAccounts in PHP - an OO demoAdding a button to a web page to print the pageAstroturfing - the online definition
|
2259 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 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).
|
|