Coding - Conditions
The "If" statement is a very common tool found in almost every programming language. This statement is a very basic, direct order to Lua. This can be really useful in creating your own scripts. "If" tells the code to do something if a condition is true or not. This is a simple if statement:
The if statement can be a little confusing.
The local age = 18 part of this script sets a "Local" variable named age as 18. Local just means that the variable will only exist within the control structure. In this case the if statement.
The above example basically means "if age is equal to 20 then say 'You're 20!' and stop."
The if statement looks like this to Lua:
Condition has to be true to run the code block after it. A code block is code that only runs under certain conditions. In this case, the code block runs if the condition is true.
Else is used to define the opposite of the condition, so if a condition is false, then else would tell it to execute the following code, a good usage of else is shown below.
The above example checks if the variable "age" is 20, if this is true, then it prints to console "You're 20!", but if the variable "age" is NOT 20, then it prints "You're NOT 20!" to console.
The equal to condition is true if the thing on the left and right of == are equal.
The "not equal to" condition is true if the thing on the left and right of ~= aren't equal. You can also use != instead of ~=
The code below is the same as the code above.
The "less than" condition is true if the thing on the left is less than the thing on the right.
The "greater than" condition is true if the thing on the left is more than the thing on the right.
The "less than or equal" condition is true if the thing on the left is less or equal to the thing on the right.
The "greater than or equal" condition is true if the thing on the left is more or equal to the thing on the right.
This is a lot for first time Lua scripters, however, if you practice enough with using Conditionals, you will get the hang of it eventually.
Last, I want to show you how to do else if. Else if is used to say, basically, "If it's not that, then if..." in lua. This is more precise than using else, but else if is NOT effective all the time
What this script is doing is trying to figure out if you can go to high school. If you're not under 15, and you're not over 18, then you can come in.
Give it a try yourself. Try writing a simple if statement in notepad. If you need help just look at the examples in here. Start with the simple ones and work up.
Next we will learn about tables.