HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Conditions (if)

Conditions (if)

if-elseif-else, then/end, and ~= (not equal).

Simple enough for a 5-year-old to understand

if / elseif / else

Example code
1
local score = 75
2
if score >= 85 then
3
print("Excellent")
4
elseif score >= 50 then
5
print("Passed")
75 falls in here.
Output / Input75 falls in here.
6
else
7
print("Failed")
8
end
End of block.
ConditionEnd of block.
Output / InputCondition
💡 Hint: Not equal is with ~= (NOT !=). Blocks open with then and close with end.

About this topic

Conditions in Lua are written with if, elseif, and else, and each block is closed with end. There are no curly braces; keywords define the blocks. elseif is written as a single word; if written separately, each becomes a separate if and requires its own end.

Lua's truthiness rule is very simple and must be known precisely: only false and nil are falsy. Everything else is truthy — even zero, empty strings, and empty tables. This differs from many languages and leads to silent bugs if not paid attention to.

Logical operators are written with the words and, or, and not. They have an interesting feature: they return one of the operands rather than a boolean value. This behavior is frequently used as a shortcut for providing default values.

What you will learn

  • the if, elseif, and else structure
  • Lua's truthiness rules
  • Logical operators
  • The inequality operator
  • The default value pattern

Who is this for?

  • Learners of control flow
  • Those expecting zero to be considered falsy
  • Those coming from C-like languages

When to use?

State checks in game scripts constantly require conditions; knowing the truthiness rule prevents wrong branchings.

Using the logical operator pattern is the shortest way to assign a default in case a value hasn't been provided.

Common mistakes

Wrong

local sayi = 0
if sayi then
  print("not zero")
end

Zero is truthy in Lua; only false and nil are falsy. Therefore, the block runs and prints the wrong message. Those coming from C or Python expect zero to be falsy. You need to write your intent with an explicit comparison.

Correct

local sayi = 0
if sayi ~= 0 then
  print("not zero")
end

Wrong

local a = 5
if a != 3 then
  print("different")
end

In Lua, the inequality operator is written with a tilde and an equals sign; the format with an exclamation mark gives a syntax error. Similarly, logical operators are words, not symbols. This is the difference those coming from the C family encounter on day one.

Correct

local a = 5
if a ~= 3 then
  print("different")
end

Wrong

local x = 10
if x > 5 then
  print("greater")
else if x > 2 then
  print("medium")
end

When else and if are written separately, a new block opens and each requires its own end; this code gives an "'end' expected" error. When the single-word elseif is used, no extra end is needed and the structure remains readable.

Correct

local x = 10
if x > 5 then
  print("greater")
elseif x > 2 then
  print("medium")
end

Frequently asked questions

Is there a switch in Lua?

No. The alternative is to write an elseif chain or set up a table mapping keys to functions. The second method is both faster and more readable in multi-branch situations and is common in Lua code.

How does the default value pattern work?

The or operator returns the right-hand value if the value on its left is false or nil. It is used to assign a default if a parameter has not been provided. The thing to watch out for is: if the value is false or zero, the default also kicks in, which can produce unexpected results.

Is there a ternary operator?

Not directly, but the same effect is achieved by using and and or together. If the condition is true, the first value is taken; if false, the second value. This pattern is common but yields unexpected results in cases where the first value can be falsy.

Summary

Only false and nil are falsy; zero is truthy.

The not equals operator is written with a tilde.

Logical operators are written as words.

elseif must be a single word.

Blocks are closed with end.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code