Conditional statements instruct the CPU to execute a portion of code only if some input condition is met. You might also see this referred to as a branch statement because there are multiple possible branches of execution.
The last few examples in the previous section are one type of conditional statement. But they have their limitations and are really only suitable for evaluating very simple expressions and results.
The ‘if-else‘ statement makes up the bread-and-butter of conditional statements.
if 1 < 2 then print("Hello world!") end --Output: Hello world!
This is the basic format of the if statement.
‘if‘ is what is known as a keyword.
Keywords are special words reserved by the language and have specific meaning and purpose for that language. As you progress, you will encounter many more keywords.
In this case, the keyword if begins the statement, followed by the condition to check. If the condition check is true, then execute the code associated block of code. ‘end’ closes the statement.
You may have wondered why this called the ‘if-else‘ statement if ‘else‘ is not found anywhere.
This is because the else part is optional. In the previous example, if the condition is false, no code is executed.
An optional else statement ensures that if the condition check is false, there is a “default” block of code that is executed.
if 1 > 2 then print("Hello world!") else print(3.14) end --Output: 3.14
Coding Style
Thus far we have kept everything on a single line.
But you are likely already beginning to see how things can turn into a mess, making code difficult to understand.
All code needs testing, optimizing, and maintenance. And that means looks matter!
Coding style is the set of guidelines for how to write and lay out your source code in such a way that it is easy to read and understand. The goal is to create “self-documenting” code that is easy to read and understand.
As your projects become bigger and more advanced, it becomes more challenging to track how all the pieces fit together. And poorly styled code only makes that much more difficult for you or anyone else who has to read your code.
Throughout this series, I will offer styling guidelines but ultimately, it will be up to you to decide how you will style (or not style) your own code.
So don’t create yourself a nightmare.
It’s important that you adopt good coding practices early on so as to not develop bad habits that will come back to bite you later on.
And the first guideline is to not undervalue the use of whitespace.
Lua has few restrictions on how tabs, spaces, and line breaks are used. So use that to break your code into more elegant and readable blocks.
if 1 > 2 then
print("Hello world!") --Output: Hello world!
else
print(3.14) --Output: 3.14
end
You can immediately see how separating each statement and indenting blocks of code make it much easier to read.
The ‘elseif’ Statement
The if statement also supports additional alternate statements. This is done by inserting subsequent ‘elseif-then‘ statements following the initial if statement.
if 1 > 2 then
print("if statement")
elseif 1 == 2 then
print("elseif statement #1")
elseif 1 == 1 then --condition met
print("elseif statement #2")
else
print("else statement")
end
Statements are always evaluated from top to bottom. If a condition is met, only the block of code associated with that branch is executed and any preceding or proceeding statements are skipped.
You can check multiple conditions by chaining together operators. This example illustrates the importance of whitespace usage.
if 1 < 2 and --true
3 > 2 and --true
2 == 2 and --true
0 < -1 --false
then
print("Hello world!")
elseif (1 <= 2 and 1 ~= 3) or --true
3 < 0 --false
then
print(3.14)
end
--Output: 3.14
It is permissible to have other if statements inside of if statements. This is called ‘nesting.’
if 1 < 2 then
print("if statement")
if 3 > 2 then
print("nested if statement")
end
else
print("do nothing")
end
--Output: if statement
--Ouptut: nested if statement
As a general rule, try not to nest more than about 2-3 “deep.” In reality, that’s hardly ever feasible, so later on we’ll learn how to get around that.