This is what I posted in a topic yesterday… :)/>
Conditionals are definitely a very important part of programming that everyone needs to know… that being said here goes…
Explaination of if's and boolean logic:Spoiler
If statements
An if statement is a way of performing particular instructions given a certain condition… these conditions can be a single boolean (true/false) variable or a set of statements that produce a boolean… basic Lua syntax of an if statement is:
if <condition> then
-- instructions to perform when condition is true
end
Boolean Logic
An if statement's conditional can then be furthered using boolean logic ( not, and, or ) using not, and, or all can effect the condition.
code example
if ( active and running ) or not waiting then
end
where active, running and waiting are all boolean variables
A not effects in this way
not true = false
not false = true
An and effects in this way
true and true = true
true and false = false
false and true = false
false and false = false
An or effects in this way
true or true = true
true or false = true
false or true = true
false or false = false
As shown in the coding example you can also use brackets to make sure that those statements are evaluated first. For examples using
if not ( running and listening ) then
would only do the actions if running AND listening are false
Ifelse statements
Ifelse statements work in the exact same way as with the if statements except they provide one advantage, if we wished to do something based on a condition, without ifelse we would do this
if <condition> then
-- what to do when condition is true
end
if not <condition> then
-- what to do when condition is not true
end
however with ifelse statements we can do the following
if <condition> then
-- what to do when condition is true
else
-- what to do when condition is false
end
now ifelse statements can also be extended when dealing with variable equality as follows
if input == "this" then
elseif input == "that" then
elseif input == "the other" then
end
we can also end the above code with an aditional else, just incase it doesn't match anything
if input == "this" then
elseif input == "that" then
elseif input == "the other" then
else
end
Nested ifs
We can also have if and ifelse statements inside another if or ifelse statement. the basic syntax is as follows
if <first condition> then
if <second condition> then
else
end
end
This code will only reach the second condition IF the first condition is true… Pro tip:Spoiler
Lets say that we want to assign a variable x to 10 if a boolean is true or 20 if it is false, normally people would use an if statement as follows
local x
if someBoolean then
x = 10
else
x = 20
end
However we can use a (what is referred to in other languages) ternary operator…
A ternary operator works in this way
<condition> and <value if true> or <value if false>
so for the example above we would do the following
x = running and 10 or 20
now lets say we want to assign to the variable x the value 20 ONLY if the variable y has no value (is nil) we can do the following
x = y or 20
we can also assign a boolean variable like so
var = <value 1> and <value 2>
This will only assign true to var if BOTH value 1 and value 2 are not false or nil
We can also use
var = <value 1> or <value 2>
This will assign true to var if either value 1 or value 2 is not false or nil
Obviously this can be extended into any combination not, and, or and brackets to gain order… but beware, the longer it is the more complicated it gets
var = not ( <value 1> and <value 2> or <value 3> ) and <value 4> or not <value 5>
Off the top of my head I think I have covered everything, I hope I'm not forgetting anything and that it all makes sense, it made sense in my head ;)/>