Hello World
It's time to write your first Lua program! Following the
time-honored tradition
of programming, we'll start with "Hello, World!"—a simple program that displays
text on the screen.
This lesson will introduce you to the print() function, comments,
and the basic
structure of a Lua program.
Your First Lua Program
Let's start with the simplest possible Lua program. Click "Run" to execute it:
Click Run to execute your code
Congratulations! You just ran your first Lua program! 🎉
print()is a built-in function that outputs text to the console- Text inside quotes (
"...") is called a string - Each statement in Lua is executed line by line, from top to bottom
The print() Function
The print() function is one of the most useful functions in Lua.
It can print multiple values separated by commas:
Click Run to execute your code
Notice how print() automatically adds spaces between values and a
newline at the end!
| Feature | Description | Example |
|---|---|---|
| Single value | Print one item | print("Hello") |
| Multiple values | Print several items | print("A", "B", "C") |
| Numbers | Print numeric values | print(42, 3.14) |
| Mixed types | Combine strings and numbers | print("Age:", 25) |
print()
with no arguments. This is useful for adding spacing in your output!
Comments in Lua
Comments are notes in your code that Lua ignores. They're essential for explaining what your code does, both for others and for your future self!
Single-Line Comments
Use two dashes (--) for single-line comments:
Click Run to execute your code
Multi-Line Comments
For longer comments spanning multiple lines, use --[[ to start and
]] to end:
--[[
This is a multi-line comment.
It can span multiple lines.
Very useful for longer explanations!
]]
print("Comments are helpful!")
Good:
-- Calculate tax for international ordersBad:
-- Multiply price by 0.15
String Basics
Strings are sequences of characters enclosed in quotes. Lua accepts both single and double quotes:
Click Run to execute your code
\) for special
characters:
\n- New line\t- Tab\"- Double quote\'- Single quote\\- Backslash
Common Mistakes
1. Forgetting quotes around strings
Wrong:
print(Hello) -- Error: 'Hello' is not defined
Correct:
print("Hello") -- Strings need quotes!
2. Mismatched quotes
Wrong:
print("Hello') -- Quotes don't match!
Correct:
print("Hello") -- Both double quotes
print('Hello') -- Both single quotes
3. Forgetting parentheses
Wrong:
print "Hello" -- This actually works in Lua!
-- But it's better to use parentheses
Correct:
print("Hello") -- Always use parentheses for clarity
print "Hello"), but this is considered bad
practice.
Always use parentheses for consistency!
Exercise: Your Turn!
Task: Modify the code below to print information about yourself.
Requirements:
- Print your name
- Print your favorite programming language (Lua, of course! 😊)
- Print your age or year you started learning programming
- Add comments explaining each line
Click Run to execute your code
Show Solution
-- Print my name
print("My name is Alice")
-- Print my favorite language
print("I love Lua!")
-- Print when I started programming
print("I started learning in 2024")
-- Print a fun fact
print("Fun fact: Lua means 'moon' in Portuguese! 🌙")
Summary
- print(): Outputs text and values to the console
- Strings: Text enclosed in quotes (
"..."or'...') - Comments: Use
--for single-line,--[[ ]]for multi-line - Multiple values: Separate with commas in
print() - Escape sequences: Use
\n,\t, etc. for special characters
What's Next?
Great job! You've written your first Lua programs and learned the basics of output and comments. In the next lesson, we'll dive into Variables & Types—how to store and work with different kinds of data in Lua. This is where things get really interesting! 🚀
Enjoying these tutorials?