I want to know what statements mean such as: if, end, for, local, else, elseif and function. If theirs a Wiki page on it'll help out.
Tesouro provided a Wiki link to the article on Conditional Statements
I will summarize its contents in a different way.
A "statement" is something which states that something should be done. So, workspace:BreakJoints()
or apple = 9
or c = model:GetChildren()
are all examples of statements.
Normally in Lua, statements are executed in the order that they are written:
print(1); print(2); print(3);
Will obviously print
1 2 3
Unfortunately, not very much can be done with this sort of layout. For instance, how could we print whether or not a variable is divisible by 5?
We could at least print the remainder:
print("if", n % 5," is 0 then n = ",n,"is divisible by 5");
Which could give output like:
if 0 is 0 then n = 10 is divisible by 5
if 4 is 0 then n = 24 is divisible by 5
Unfortunately, we can't actually do something different if that is 0, because our statements are not conditional, they happen always without condition.
This brings us to the simplest control structure (a structure which "controls" how statements are executed), the if .. then
statement.
if ... then ... else ... end
does the sort of thing we would like for the above. For the above, it could look like this:
if n % 5 == 0 then print(n,"is divisible by 5"); else print(n,"is NOT divisible by 5"); end
We have two blocks of statements now. The first block is between then
and else
. This will only happen if
the condition (n % 5 == 0
) evaluates to true
.
The second block, between the else
and the end
will only happen if the condition evaluates to false
.
The else
block on an if
statement is optional. We could make it only remark if the condition is true:
if n % 5 == 0 then print(n / 5); end -- Otherwise it's a fraction so we don't need to show it
Blocks of statements can, of course, contain other conditionals. Maybe if it's not divisible by 5 we want to show that it's divisible by 4:
if n % 5 == 0 then print(n," div by 5"); else if n % 4 == 0 then print(n," NOT div by 5 BUT is div by 4"); else print(n," NOT div by 5 and NOT div by 4"); end end
Notice, though, that we now have one additional if .. then .. end
block. Since if we have to include several of these the number of ends starts growing, we can use elseif
to keep it short. An elseif
is just like else if
except that it does not require its own end
:
if n % 5 == 0 then print(n," div by 5"); elseif n % 4 == 0 then print(n," NOT div by 5 BUT is div by 4"); else print(n," NOT div by 5 and NOT div by 4"); end
This can make the code simpler, although these still remain relatively uncommon in practice.
I am not going to provide as in-depth of a description of loops. Here is a quick description:
Let's imagine we want to print the numbers 1 to 7.
print(1); print(2); print(3); print(4); print(5); print(6); print(7);
That works. It's obviously very repetitive and clunky. You could easily make a typo. Also, it doesn't work at all if you don't know ahead of time how many you want to print (unless you use a mess of if statements).
We can see, however, that there is a clear structure to the repetition. Each repetition is in the form of:
print(i);
Where i
is just a variable starting at 1
and increasing by 1
up to 7
.
for
loops allow us to write this succinctly:
for i = 1,7 do print(i); end
There is considerably more to this topic, but this is where my answer will end. Good luck with other materials!