5) Logic Statements

[www.lua.org]

if CONDITION then --Activates if condition was true end

if CONDITION then --Activates if condition was true else --Activates if condition was false end

[www.tutorialspoint.com]

Our First Logic Statements

if 5 > 6 then print("5 is greater than 6") end if 6 > 4 then print('6 is greater than 5') end if 5 == 0 then print("Five is equal to ZERO?!") else print("No, five isn't equal to zero.") end

Comparing Variables

trueOrFalse = true

if trueOrFalse then print('trueOrFalse was true.') trueOrFalse = false else print('trueOrFalse was false.') trueOrFalse = true end

are generally called "if statements". They are used to tell your code what you want it to do in a given situation. When the statement is activated (say, by pressing a button) the logic contained in its statement will only be activated if the condition given is true. They are always formatted as:You can also add "else" to that, so that if the statement is false, something ELSE happens instead. Notice here I added commenting using two minus signs in a row. The engine will ignore anything on a line after --.What you place in the area I labeled CONDITION in these examples are called relational and conditional operators. Using them, you can compare many things to eachother. They produce what is called a boolian value (a variable value that is either "true" or "false").We will try a few of these out.Now enter into that function these statements:When those lines are used and the button pressed, you will see that only the print functions located in the TRUE statement were printed. Also, because 5==0 is a false statement it activated the print function located in the "else" part of the logic.Once again,We are going to be creating a new variable, then altering it. The new variable will be a bool. Bool values can only be true, false or nil (nil means neither). Bool values are always written in all lower case. First, we will create our variable just beneath our object and checker GUIDs being established.Then, in buttonClicked, we will establish some logic to check if trueOrFalse is, well, true or false. If it is true, we'll print that it was true and switch it over to false. If the button is clicked again, it will print that it was false and switch it to true.We could have also written that as "if trueOrFalse == true then" but that is unnecesary. Remember, the IF statement just needs to be fed True or False. And since trueOrFalse is already one of those, we can skip the operators.