← Back to Lua Course | Chapter 1: Introduction | Lesson 7 of 7

Statements and chunks

A Lua file is a chunk, a list of statements that need no semicolons or special ending.

In this page:

  1. Statements and chunks

Statements and chunks

A chunk is any sequence of statements and is the unit Lua compiles and runs, either a file or a string. Statements are separated by whitespace, and semicolons are optional. The main chunk is itself a function that receives the command-line arguments as varargs.

Note: Wrap the code in do ... end when you want a block with its own local variables.

Example: Statements and chunks

lua
local a = 1; local b = 2
print(a + b)

do
  local hidden = "inside block"
  print(hidden)
end

print(select("#", ...))
print(load("return 6 * 7")())

-- Output:
-- 3
-- inside block
-- 0
-- 42
Common Mistakes
  1. Putting a semicolon at the end of every line out of habit and thinking it is required
  2. Starting a line with a parenthesis and accidentally continuing the previous statement
  3. Forgetting end after a block
Chapter Summary
  • A chunk is a sequence of statements
  • Semicolons are optional
  • The main chunk receives arguments as ...
  • do ... end creates an explicit block
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.