07. Hello, World
The traditional first program in any language prints
Hello, world! and exits. It is tiny, but writing it proves
the language is installed, the editor is set up, and the terminal can
run files. This chapter writes it, breaks it to see an error, then fixes
it.
The whole program
Open exercises/07/01-hello.lua in VS Code. The file
contains one line:
print("Hello, world!")In the VS Code terminal, run it:
lua exercises/07/01-hello.lua
The terminal prints:
Hello, world!
That is the whole program. The rest of this chapter explains each part of that line.
What print does
print is a function — a named piece of
code that does a job when you ask for it. The job of print
is to write whatever you give it to the terminal, followed by a new
line.
The parentheses ( and ) after the name are
how you ask the function to run. Anything between them is what you give
it.
print("Hello, world!")
-- ^^^^^^^^^^^^^^^
-- this is what we are giving to printIn Lua, the things you give a function are called
arguments. The program above gives print
one: the string "Hello, world!".
Change the text inside the quotes to your own name. Save and run it again. The terminal should now print your name.
Strings
The text "Hello, world!" is a string —
a sequence of characters. In Lua, strings are written between
double quotes " or single
quotes '. Both work:
print("Hello, world!")
print('Hello, world!')The quotes are not part of the string. They mark where the string starts and where it ends. When the string prints, only what is between the quotes appears.
Printing more than one thing
print can take more than one argument. Separate them
with commas:
print("Hello,", "world!")The terminal prints Hello,, a tab, then
world!, all on one line. print puts a tab
between each argument and a new line at the end.
For a different separator, build one string yourself with
.. (two dots), which sticks strings together:
print("Hello, " .. "world!")Now the output is Hello, world! with one space between
the words, because the space is inside the first string.
Make print show three things on one
line: the word lua, the number 5, and the word
is_fast. Run it. Notice the number appears even though it
has no quotes around it.
Comments
A comment is text Lua ignores when it runs the
program. Comments are for the humans reading the code. A single-line
comment starts with --:
-- This line is a comment. Lua does not run it.
print("Hello, world!") -- A comment can also sit at the end of a line.A comment that spans many lines starts with --[[ and
ends with ]]:
--[[
Anything in here is ignored.
Useful for long explanations
or for temporarily turning off
a block of code.
]]
print("Still runs.")Use comments to explain why the code does something, not what each line does. Good code already says what.
Making and reading errors
Now break the program on purpose. In 01-hello.lua,
remove the last quote so the line reads:
print("Hello, world!)Save and run it. Lua refuses to run the file and prints an error like this:
lua: exercises/07/01-hello.lua:1: unfinished string near
'"Hello, world!)'
stack traceback:
[C]: in ?
Read errors in this order:
- The file name and line number
(
01-hello.lua:1). This is where the problem is. Look here first. - The short message
(
unfinished string near ...). This is what went wrong. "Unfinished string" means Lua saw the opening quote and started a string but never found the closing one. - The snippet after
nearshows the code that confused Lua.
Errors are not punishments. They tell you exactly which line to look at and hint at what is wrong.
Fix the missing quote so the program runs again. Then try another
mistake: change print to prnt and run it. Read
the new error. What does the wording suggest? (No need to write down
an answer. Just notice.)
A note on whitespace and case
Lua does not care how many spaces or blank lines are in your file. All three of these run the same:
print("Hello, world!") print( "Hello, world!" )
print("Hello, world!")
Lua does care about capital letters.
print is a different word from Print and
PRINT. The built-in name is lower case.
Homework
The homework files are in exercises/07/homework/. Open
each starter in VS Code, read the comment at the top, and finish the
file so it runs and produces the expected output.
Problem 1 — Greet yourself
Open exercises/07/homework/01-greet-yourself.lua. The
file is empty except for a comment. Write three print calls
so that running the file shows, on three separate lines:
- a greeting (your choice of words),
- your first name,
- the name of your favourite game.
Problem 2 — Print a quote
Open exercises/07/homework/02-print-quote.lua. Print a
famous quote on the first line and the author's name on the second line
in this format:
"The best way to predict the future is to invent it."
— Alan Kay
(The dash before the author can be a regular hyphen -.
Exact spacing does not matter, as long as the quote is on one line and
the author is on the next.)
Problem 3 — Fix the error
Open exercises/07/homework/03-fix-the-error.lua. The
file does not run. Read the error Lua prints, fix the file, and run it
until you see:
All good now.
There is more than one mistake in the file. Find them all.
Challenge — Three-line story
Open exercises/07/homework/04-three-line-story.lua.
Write a three-line micro-story where:
- the first line sets the scene,
- the second line introduces a problem,
- the third line resolves the problem.
Each line should be printed by its own print call. Add a
--[[ ]] multi-line comment at the top explaining, in plain
English, what your story is about.
Stuck or finished? Open the homework solutions page for a walkthrough.