This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
![]() |
Programming in Lua | ![]() |
| Part I. The Language Chapter 4. Statements |
An if statement tests its condition and executes its then-part or its else-part accordingly. The else-part is optional.
if a<0 then a = 0 end
if a<b then return a else return b end
if line > MAXLINES then
showpage()
line = 0
end
When you write nested ifs,
you can use elseif.
It is similar to an else followed by an if,
but it avoids the need for multiple ends:
if op == "+" then
r = a + b
elseif op == "-" then
r = a - b
elseif op == "*" then
r = a*b
elseif op == "/" then
r = a/b
else
error("invalid operation")
end
| Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved. | ![]() |