CodeRigor

LESSON

Write and run your first Lua program

Write a program that prints lines of text, follow the order its statements run in, add comments and read a first error message.

A program is a list of instructions for a computer, written as text. Lua is a programming language: a set of rules for writing those instructions so that a computer can follow them. To run a program is to ask the computer to carry the instructions out.

Lua is a small language. It is used inside many games and other applications, and its rules fit in your head quickly, which makes it a good first language.

You do not need to install anything for these lessons. Every example with an Edit & run button runs right here on the page, and you can change it and run it again. Nothing you type can break anything.

One instruction, one line on the screen

Here is a complete Lua program. Run it.

Lua
print("Hello, world!")

It shows Hello, world! on the screen. The text a program shows is called its output.

Three things in that one line have names you will meet in every lesson:

  • print is a function: a named action that Lua already knows how to do. This one shows text on the screen.
  • The round brackets after the name call the function, which means "do that action now".
  • "Hello, world!" is a string: a piece of text. The quotation marks tell Lua where the text starts and stops. They are not part of the text, so they are not shown.

The value you put between the brackets is called an argument. It is what the function works on: here, the text to show.

From text you write to text on the screen
start / 3
You write the program as text. Lua reads it and carries out one statement at a time. print puts a line on the screen.
From text you write to text on the screen
You write the program as text. Lua reads it and carries out one statement at a time. print puts a line on the screen.

Statements run from top to bottom

A statement is one complete instruction. Write one statement per line. A program with three statements does three things, always in the order they are written: first line first.

Lua
print("Fill the kettle")
print("Boil the water")
print("Make the tea")

This prints three lines, in the same order as the statements:

Text
Fill the kettle
Boil the water
Make the tea

Step through the diagram to watch each line run. Then swap two lines in the program above and run it again: the output changes order too, because Lua never rearranges your statements.

One statement at a time, top to bottom
start / 3 ⇄ code
Lua finishes each statement before it starts the next one. Every print starts a new line on the screen.
One statement at a time, top to bottom
Lua finishes each statement before it starts the next one. Every print starts a new line on the screen.

print() with nothing between the brackets prints an empty line. That is useful for spacing output out.

Comments are notes for people

A comment starts with two hyphens, --. Lua ignores everything from the -- to the end of that line. Comments are for the person reading the program, including you next week.

Lua
-- Show a two-line welcome.
print("Welcome!")
print()            -- an empty line
print("Come in.")

This prints Welcome!, then an empty line, then Come in.. The comments print nothing.

Two hyphens inside the quotation marks of a string are ordinary text, not a comment: print("Tea -- hot") prints Tea -- hot.

Exactness matters

Lua follows your text exactly, so small differences change the result:

  • Capital letters count. print works; Print is a different name that Lua does not know.
  • Everything between the quotation marks is printed exactly, including spaces and punctuation. "Hello,world!" and "Hello, world!" are different strings.
  • Each string needs a quotation mark at both ends, and each call needs both brackets.

Read your first error message

Everyone who programs sees error messages every day. An error message is not a telling-off. It is Lua saying which line it could not follow, and why. Learning to read one is a skill in its own right.

This program has a mistake: the closing quotation mark is missing.

Lua
print("Hello, world!)

Lua refuses to run it and reports:

Text
solution.lua:1: unfinished string near '"Hello, world!)'

Read it from left to right, one piece at a time:

  1. solution.lua:1: is the file and the line number. On this site your program is saved as solution.lua, and the 1 tells you which line to look at.
  2. The rest names the problem. An "unfinished string" is a string that was started but never ended.
  3. After near, Lua quotes the piece of your text where it got stuck.

Mistakes like this one break Lua's writing rules, so Lua cannot even start: nothing in the program runs, not even correct lines above the mistake.

A second very common mistake is a misspelt name:

Lua
prnt("Hello, world!")

This time the text follows the writing rules, so Lua starts running it and stops at the line it cannot carry out:

Text
/usr/bin/lua: solution.lua:1: attempt to call a nil value (global 'prnt')
stack traceback:
	solution.lua:1: in main chunk
	[C]: in ?

Only the first line matters for now. It starts with /usr/bin/lua:, which is the full name of the program that runs your code; after that come the file and line number, as before. nil is Lua's word for "nothing". Lua looked for something named prnt, found nothing, and you cannot call nothing. So "attempt to call a nil value" nearly always means a misspelt function name, and the brackets at the end of the message tell you which name: prnt.

The lines under stack traceback: say where in the program Lua was when it stopped. In a short program they repeat the line number you already have, so you can ignore them.

When you see an error: find the line number after solution.lua:, read the words after it, go to that line, and compare it with a working example.

Running Lua on your own computer

This is optional; the lessons do not need it. If you install Lua from lua.org, you can save a program in a text file named hello.lua, open a terminal (a window where you type commands) in the same folder and type lua hello.lua. The output appears in the terminal, the same as it does on this page.

What you can do now

You can write a program of several print statements, predict the order of its output, add comments, and find the line an error message is pointing at. In the exercises you will predict what small programs print, repair two broken ones and then write a three-line program of your own.

Next: name values with variables, so that a program can remember and calculate things.

Watch & read

Free lectures, talks and readings that cover this skill from another angle. All resources →

Starts at the 'Output & Comments' chapter (3:03), about twelve minutes: printing text and writing comments. Stop at 'Data Types & Variables' (14:46), which belongs to the next lesson. The opening three minutes, 'What is Lua?', are optional background.