YouTip LogoYouTip

If Statement In Lua

Lua if Statement |

-- Learning not just technology, but dreams!

Lua Tutorial

Lua Tutorial Lua Environment Setup Lua Editor Lua Basic Syntax Lua Data Types Lua Variables Lua Loops Lua Flow Control Lua Functions Lua Operators Lua Strings Lua Arrays Lua Iterators Lua table Lua Modules and Packages Lua Metatable Lua Coroutine Lua File I/O Lua Error Handling Lua Debug Lua Garbage Collection Lua Object-Oriented Lua Database Access Lua5.3 Reference Manual

Lua Loops

Lua Functions

Deep Dive

  • Web Service
  • Scripting Language
  • Programming
  • Programming Language
  • Computer Science
  • Development Tools
  • Software
  • Script
  • Web Design & Development
  • Web Service

Lua if Statement

Lua Flow Control Lua Flow Control

Lua if statement consists of a boolean expression followed by other statements.

Lua if statement syntax is as follows:

if( boolean_expression )
then
   --[ statement(s) to execute if boolean expression is true --]
end

If the boolean expression evaluates to true, the code block inside the if statement will be executed. If it evaluates to false, the code immediately following the if statement's end will be executed.

Lua considers false and nil as false, and true and non-nil as true. Note that in Lua, 0 is considered true.

The flowchart for the if statement is as follows:

Lua if Statement

Example

The following example checks if the value of variable a is less than 20:

Example

--
a = 10;

--
if( a < 20)
then
   --
   print("a is less than 20");
end
print("a's value is:", a);

The result of executing the above code is as follows:

a is less than 20
a's value is:	10

Lua Flow Control Lua Flow Control

← Function Call Operator OverloaIncrement Decrement Operators β†’