Introduction to Lua and Writing Your First Program
Lesson Overview
In this lesson, you will learn what Lua is, why it is widely used, and how Lua programs are structured. By the end of this lesson, you will write and run your first Lua program and understand every line of code inside it.
This lesson assumes no prior programming experience and focuses on building strong fundamentals.
Lesson Objectives
After completing this lesson, you will be able to:
- Understand what Lua is and why it exists
- Identify where Lua is commonly used
- Understand how a Lua program runs
- Write and execute a basic Lua script
- Use the print function to display output
What Is Lua?
Lua is a lightweight, high-level scripting language designed to be fast, simple, and easy to embed into other applications. It was created to give developers a flexible way to add logic and automation without the complexity of larger programming languages.
Lua focuses on:
- Simplicity
- Speed
- Portability
- Easy integration with other software
Unlike many languages, Lua is small in size but powerful in capability.
Where Is Lua Used?
Lua is used in many real-world applications, including:
- Game development
- Game engines and scripting systems
- Automation tools
- Embedded systems
- Configuration and extension systems
Because Lua is lightweight, it is often chosen when performance and flexibility are important.
How Lua Programs Work
A Lua program is a text file containing Lua instructions. These instructions are read and executed line by line from top to bottom.
Lua does not require:
- A main function
- Complex setup
- Strict file structures
This makes Lua beginner-friendly and easy to experiment with.
Your First Lua Program
Traditionally, the first program in any programming language displays a simple message on the screen. In Lua, this is done using the print function.
Example: Hello World Program
print("Hello, Lua!")Explanation
- print is a built-in Lua function
- The text inside quotation marks is called a string
- The program outputs the text to the console
When you run this program, Lua displays:
Hello, Lua!Understanding Functions in Simple Terms
A function is a reusable block of code that performs a specific task.
The `print` function:
- Takes a value as input
- Displays it on the screen
You will learn how to create your own functions in later lessons.
Writing Multiple Lines of Output
You can call the `print` function multiple times.
print("Welcome to Lua")print("This is your first program")print("Programming starts here")Each `print` statement runs in order, producing output line by line.
Common Beginner Mistakes
- Forgetting quotation marks around text
- Misspelling print
- Using incorrect capitalization (Print will not work)
Lua is case-sensitive, meaning uppercase and lowercase letters matter.
Practice Task
Try writing a Lua program that prints:
- Your name
- Your favorite hobby
- A message explaining why you want to learn Lua
Each message should appear on a separate line.
Lesson Summary
In this lesson, you learned:
- What Lua is and why it is used
- How Lua executes code
- How to write your first Lua program
- How to display output using print
You have officially written your first Lua program.
Page 2 — Deep Dive
The Philosophy of an "Extensible Extension Language"
Lua is technically defined as an extensible extension language. This terminology is specific to its design goals at PUC-Rio.
- Extension Language: It is designed to be "embedded" into a host application.
- Extensible: Lua provides minimal built-in features, allowing developers to create their own.
Historical Context and Nomenclature
The name Lua means "Moon" in Portuguese.
Ready to test your knowledge?
Take the quiz to complete this lesson and verify your understanding.
Next Lesson:
Lua Environment Setup and Basic Syntax →