Learning Lua: Part 1: Variables and Conditional Statements

Learning Lua

Lua is a multi-paradigm scripting language originally designed to be embedded as part of other, existing programs. Often used for this purpose, Lua can be found in everything from photo editing applications to used as an internal scripting language of video games like World of Warcraft.


Lua is a loosely-typed programming language. This means that variables do not have to have a data type. In Lua, a variable is simply a container that can hold any type of value in the language and is not tied to any previous values.

Screenshot 2018-11-01 18.45.09

All that is needed to create a variable in Lua is to include a new variable name not previously used and to assign it a value. Variable names in Lua, like most other programming languages, cannot contain special characters. The only valid characters are letters, numbers, and the underscore.

In Lua, the process of assignment is used with the equal sign to “put” a value into a variable. Because Lua is a loosely-typed language, any one variable can “hold” values ranging from numbers to collections of letters and numbers within quotations called strings.

Optional Semicolons

Screenshot 2018-11-01 18.52.06

For those coming to Lua from other programming languages, the code can sometimes look odd. This is because semicolons are optional at the end of lines of code in Lua. When writing one, a single statement does not need to end with a semicolon to be valid.

Conditional Statements

Screenshot 2018-11-01 19.06.40

In programming terminology, a conditional statement is one which acts to run other code if its result is true or not. In Lua, value comparisons can test using less than (<), greater than (>), less-than-or-equal-to (<=), greater-than-or-equal-to (>=), equal (==), and not equal (~=).

When pairs of values are tested in Lua, the structure follows a if…then pattern. If the comparison is true, then any code within its chunk is run.

In Lua, a chunk of code is any set of instructions. When using conditional statements, the code chunk is “inside” the if…then structure and only run if the comparison results in a true value.

If… Then Else(if)

Screenshot 2018-11-01 19.15.39

Lus also supports else and elseif keywords. When used with the if…then statement, the else follows after and is run if the initial comparison is false. The keyword elseif tests an additional pair of values. If the initial test is false, then the next one is tested. If that is false, the code will look for another or the use of a else keyword.

White Space is Important

Chunks of code “in” an if…then statement must be written with at least one space away from the left margin in order to be valid. Often, it is recommended to ‘tab’ away from the margin for organizational purposes.

All Chunks End

Screenshot 2018-11-01 19.22.37

An if..then statement closes with the keyword end. This signals that the chunk within the if…then statement is over. This also acts in a similar purpose as curly brackets in other programing languages.

Play with the example on Repl.it!