Your first Lua
What Lua is, how to print a line, how comments work, and a first program you can run and change.
A small language, widely used
Lua started as a small scripting language for other programs to embed, and that job shaped it: it is tiny, fast, and easy to add to an application. You meet it in games, in embedded devices, and in tools that let players or admins write a little logic of their own.
Because it was built to be embedded, Lua carries almost no baggage. There are eight kinds of value, one general container called a table, and only a few keywords. That makes it quick to learn and quick to read, which is why this course can start at a single line.
Printing a line
print writes to the output. In this sandbox every call is captured and shown exactly, one line per call, so what you print is what you see.
Lua does not need a semicolon at the end of a line. A newline ends the statement, and the language trusts you to mean it.
Comments and blank lines
A comment starts with two dashes and runs to the end of the line. Use it to say why a line is there; the code already says what it does.
Blank lines are ignored. Group the lines that belong together with a space between the groups, and the shape of the program becomes readable at a glance.
Text with quotes and escapes
A string can be written with single or double quotes, and the two are equivalent. Inside it, a backslash starts an escape: \n is a new line, \t a tab, and \" a quote that would otherwise end the string. The backslash is not printed; it marks the character that follows. A missing closing quote makes the error appear at the end of the file, because the string swallowed the rest.
For text with many quotes or several lines, Lua has long strings: [[ ... ]] keeps everything between the brackets literally, newlines included. A level can be added with equals signs, [=[ ... ]=], when the text itself contains ]]. That form is how you paste a block of prose or a snippet of another language without escaping each line.
Comments that explain, not repeat
The comment in the first example says that two dashes start a comment. That is a comment about the language, which is useful once. A comment that says -- add one to i above i = i + 1 repeats what the code already says and will be wrong the moment the line changes.
Write the reason instead: why the count starts at zero, why this branch is allowed to skip the check, where the value came from. A reader can recover the what from the code; the why exists only in your head, and the comment is where you put it. Long comments use --[[ ... ]], which can span lines. When you change the code, change the comment in the same edit; a stale comment is worse than none.
How a line becomes output
Lua does not run your text directly. It reads the whole file first and turns it into instructions for a small virtual machine. That is why a missing bracket at the bottom of a file can stop the very first line from running: nothing executes until the whole chunk has been read.
print is a function, not a keyword. The brackets are a call, and the values between them are the arguments. print("Hello") passes one string; print(2 + 3) passes the number five, because the sum is worked out before the call.
A file is one chunk. Statements run in order, one after another, and the output pane shows a line for each print call. When you change one line, run the chunk again and compare the whole output rather than guessing which lines moved.
Hello, and a few sums
-- Two dashes start a comment.
print("Hello, Lua!")
print(2 + 3)
print(10 - 4)
print(6 * 7)
print(10 / 4)Three mistakes bite beginners. A missing closing bracket or quote makes Lua complain near the end of the file rather than on the line you meant. A # does not start a comment in Lua — it is the length operator, so a line beginning with it is a syntax error. And quotes turn an expression into text: print("2 + 3") shows the five characters, not the number five.
Try these next
- Print how many minutes there are in a day, using an expression rather than the answer typed out.
- Add a comment above one line and a blank line between two groups; confirm the output is unchanged.
- Remove a closing bracket, run it, and read where Lua says the problem is. Put it back.
Run it, then make it yours
Press Run and read the output line by line. Then change the greeting, add a line that prints a subtraction of your own, and run it again.
A receipt, with the sums shown
Run it, then change the quantity and the unit price. Watch which lines are text and which are numbers computed at run time.
When a line surprises you, print it on its own and run again. Printing a value the moment you are unsure of it is the fastest way to learn what the language actually did.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does print do?
