3 Variables
Variable Assignment, Usage, and Naming
Hasan Malik and Rutwa Engineer
Learning Objectives
- Learn why we need variables
- Explore with different types of variables
- Learn the conventions of variable naming
Introduction
This module explains the concept of variables in Lua.
Variables
A variable is like a container that stores information to be reused later in your program. Instead of writing the same value repeatedly, you can give it a variable name that will be referred to in your code.
Think of a variable as a labeled box. You can put something in the box (like a number or a word), and you can use the label (the variable name) to get it whenever you need it.
A variable must have a data type, like a string, number, or boolean (there are other data types as well, but we haven’t learned them yet!).
Creating Variables in Lua
Let’s create a number
variable.
Example
x = 1
y = 2
print(x + y)
Output:
3
Here, we created two variables: x
and y
.
The number
value 1
was assigned to x
. We say that x
stores the values 1
. Similarly, y
stores the value 2
.
When we perform the operation x + y
, the program interprets it as 1 + 2
, because x
stores 1 and y
stores 2. Thus, you see the output 3
on your screen.
Now, let’s create a string variable.
Example
name = “James”
print(“Hello”, name)
print(“Goodbye”, name)
Output:
Hello James
Goodbye James
Here, we only use one variable: name
, which stores the string value “James”
.
The statement used to create a variable is called the assignment statement. “=” is called the assignment operator. In the above examples, can you figure out which statements are the assignment statements?
Show/Hide
The assignment statements are x = 1, y = 2, and name = ”James”.
You can update, or re-assign, variables:
Example
name = “James”
name = “Angela”
print(“Hello”, name)
print(“Goodbye”, name)
Output:
Hello Angela
Goodbye Angela
This happens because Lua always uses the most recent variable assignment.
Note
In print statements, you can use a comma to separate between the separate components of the print statement and automatically add a space between them.
We have seen a couple of examples of this usage already!
A Note on Variable Names
In the above examples, we used x, y, and name as our variable names.
In Lua, variable names can consist of letters, numbers or underscores (_). However, the name must not start with a number.
Also note that variable names never have quotation marks around them, even if they are of the string data type. Quotation marks are only used when typing strings directly, e.g. when writing “Goodbye” above, or when initializing “James” as a string variable.
Moreover, variable names are case-sensitive. name and NAME are two different variables. For example:
Example
name = “George”
NAME = “Amelia”
print(name, NAME)
Output:
George Amelia
In programming, you are strongly advised to use meaningful variable names. In general, your variable names should make it clear to anyone who reads your code what you are trying to do.
Local Variables