See the Troubleshooters.Com Bookstore.
if boolean_expression_evaluates_true thenSo only if the boolean expression evaluates true do you do the code. The words if, then and end are keywords. Words if and then delineate the condition to be evaluated for truth or falsehood, while words then and end delineate the code to be run if the condition is true, assuming there are no else or elseif keywords, which are discussed later.
do_this_code()
end
== |
Equal |
~= |
Not equal |
<= |
Less than or equal |
>= |
Greater than or equal |
< |
Less than |
> |
Greater than |
Operator Precedence From lowest to highest (from loosest to tightest binding) |
|
or |
Logical or: Loosest binding |
and |
Logical and |
< > <= >= ~= == |
Relational operators less than, greater than, less than or equal, greater than or equal, not equal, equal |
.. |
String concatenation |
+ - |
Arithemetic add and subtract |
* / |
Arithmetic multiply and divide |
not - (unary) |
Boolean not and arithmetic unary minus |
^ |
Exponentiation: tightest binding |
a * 2 + 3 < a + 3 * 4 or a < 25 and a < a * 5 - 4 |
Original expression |
|
(a * 2) + 3 < a + (3 * 4) or a < 25 and a < (a * 5) - 4 | * is highest precedence, tightest binding in the expression, so start by putting parentheses around all * |
|
((a * 2) + 3) < (a + (3 * 4)) or a < 25 and a < ((a * 5) - 4) | + and - are next highest precidence, so put parentheses around them |
|
(((a * 2) + 3) < (a + (3 * 4))) or (a < 25) and (a < ((a * 5) - 4)) | Relationals are the next highest priority, so enclose them in parentheses |
|
(((a * 2) + 3) < (a + (3 * 4))) or ((a < 25) and (a < ((a * 5) - 4))) | And is the next highest, so enclose both sides of the and in parentheses |
|
((((a * 2) + 3) < (a + (3 * 4))) or ((a < 25) and (a < ((a * 5) - 4)))) | |
Last but not least, well,
actually yes the least, is or, so put parentheses around both sides of
the or. This last set of parentheses enclose the entire expression, so
you know you're done. |
#!/usr/bin/luaThe first statement above uses precedence to decide what to do next. The second statement uses parentheses to explicitly direct Lua to do the same thing as precedence would make it do. The third statement strongarms it away from what precedence would normally indicate, and changes the result.
print(true or false and true and false) -- prints true
print(true or ((false and true) and false)) -- means same as above, prints true
print((true or false) and (true and false)) -- prints false
if boolean_expression_evaluates_true thenWhat if you want to take different actions depending on several boolean statements. Or putting it another way, what if a variable can take on several different values and you want to perform different actions depending on the variable? Watch this:
do_this_code()
else
do_other_code()
end
if contribution < 20 thenOccasionally you might have elseif without else. For instance:
membergroup = "Fan"
elseif contribution < 100 then
membergroup = "Supporter"
elseif contribution < 500 then
membergroup = "Silver"
elseif contribution < 2000 then
membergroup = "Gold"
elseif contribution < 5000 then
membergroup = "Platinum"
else
membergroup = "Cornerstone"
end
if breed = "collie" thenIn the preceding, you want to take action for collies, greyhounds and pugs, and take no action on other dogs.
print "Big and fluffy"
elseif breed = "greyhound" then
print "Fast"
elseif breed = "pug" then
print "Cute"
end
if a < b thenNow both you and I know that a must either be less, equal or more than b, any other situation is clearly impossible, so it's kind of silly to include that last else clause. That's nice, but let the programmer who has never come across an impossible situation cast the first stone. Personally, I often find value in coding for the impossible for the same reason my keyboard has a backspace key -- I sometimes make mistakes.
return -1
elseif a == b then
return 0
elseif a > b then
return 1
else
io.stderr:write("Internal error, aborting!")
os.exit(1)
end
if contribution < 20 thenIn the preceding code, membergroup3 was in scope only from its declaration until elsif contribution < 2000 then. In general, you figure out your local's scope by finding the smallest block bordered on top by then, do, elseif, else or repeat, and bordered on the bottom by a matching end, elseif, else or until. If there are contained blocks between the local's declaration and the end of its block, it's visible in those contained blocks.
local membergroup1 = "Fan"
return membergroup1
elseif contribution < 100 then
local membergroup2 = "Supporter"
return membergroup2
elseif contribution < 500 then
local membergroup3 = "Silver"
return membergroup3
elseif contribution < 2000 then
local membergroup4 = "Gold"
return membergroup4
elseif contribution < 5000 then
local membergroup5 = "Platinum"
return membergroup5
else
local membergroup6 = "Cornerstone"
return membergroup6
end
local myname = tempname or "default"In the preceding, if tempname contains a string, you assign the tempname to myname, and evaluate the whole expression. Because the whole expression is a single or, tempname is true unless it is nil, and if tempname is true then obviously the whole or clause is true, and evaluation stops. But if tempname is nil then it evaluates false, the value of the whole expression is still in doubt, so the "default" is evaluated and passed on to myname. This is an often used idiom to handle defaults.
local handle = io.open("junk.jnk", "r") or not print("open failed") and os.exit(1)There are better ways to do the preceding -- it's just being used as an example of how short circuit logic can be used to actually perform actions.
[ Troubleshooters.com | Code Corner | Email Steve Litt ]
Copyright
(C) 2011 by Steve Litt -- Legal