Variables and Output

Objective

After working through this lesson, you'll be able to:

  • Understand and use different data types in Lua.

  • Print values and variables.

Introduction

In Math class, you learned that a variable is a symbol for a number that you don't know yet. In Computer Science, a variable is information that is stored in a program.

Variables in Lua

  • A variable name refers to a particular object.

  • A variable is a location in memory where a programmer can store a value.

  • Variables are created when first assigned.

Lua is a dynamically-typed language. This means that variables do not have types, only values do.

By default, all variables will point to nil until they are assigned a value or initialized.

Variables are case sensitive which means num, NUM, Num are different variables.

Variables start with a letter or underscore.

  • a

  • A

  • _xyz

  • ABC

  • SleepyBear

Illegal Variable Names

  • Jail Break

  • /me

  • 1roblox

  • #lol

  • $dollars

  • ?whoami

Reserved Keywords

Variable names cannot be one of Lua's reserved words or keywords.

Examples: and, break, do, else, elseif, end, false, for, function, if, in, local, nil, not, or, repeat, return, then, true, until and while.

Advice

  • Generally, keep your variable names short, sweet, to the point and easy to remember because you're the one who has to use them.

  • And someone else (your instructor) will be looking at your code.

Data Types

Data Type

Explanation

number

positive or negative numbers

string

a set of characters (letters or numbers) enclosed by single or double quotations

boolean

values are either true or false

nil

non-value, to represent the absence of a useful value

userdata

allows arbitrary C data to be stored in Lua variables

function

represents a method that is written in Lua or C

table

can be used to represent arrays, sets, etc.

thread

represents independent threads of execution and used to implement coroutines

type function

The type function enables you to know the type of the variable.

username = "profpy"
print(type(username))
robux = 30000
print(type(robux))

Comments

Short Comments

  • -- This is a comment

  • Can used on its own line or at the end of another line.

-- This is a comment
name = "profpy" -- so is this

Long Comments

  • Start with two hyphens, followed by open brackets, text and closed brackets.

-- [[ These
lines are commented
out
]]

Assignment

The = in Lua means assigning the variable on the left to the value on the right. It does not mean equality.

robux = 100000
friend = "Bernie"

Multiple Assignment

In Lua, you can simultaneously assign different or the same values to different variables.

a, b, c, d, e, f = 1, 2, 3, 4, 5, 6

Output

The print function outputs to the console. To print a string, enclose a pair of single or double quotes.

print("Hello World")
print('Hello World')

You do not need quotes to print the value of a variable

x = 1
print(x)

Strings

  • Strings are a sequence of characters that can be used to represent text.

  • They can be contained in a pair of single or double quotes.

Escape sequences

Escape sequences consist of an escape character, which will always be a backslash in Lua and an identifier.

Escape Sequence

Description

\n

A new line

\t

A horizontal tab

\"

A double quote

\'

A single quote

\\

A backslash

\###

###is a number from 0 to 255 corresponding to an ASCII character

Concatenation

Concatenation is the operation joining strings end to end.

String concatenation in Lua is two dots ..

print("hello" .. "world")

Coercion

Coercion is the conversion of a value of one data type to a value of another data type.

Lua provides automatic coercion between string and number values.

print("99" + 1) -- This result in 100

Numbers

Scientific Notation

print(5e2) -- 5 times 10 to the 2nd power
print(5e-2)

Hexadecimal Numbers

print(0x1) -- 1
print(0x10) -- 16