Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua if Statement

Lua if Statement

Lua Flow Control Lua Flow Control

Lua if statement It consists of a boolean expression as the condition, followed by other statements.

The syntax of the Lua if statement is as follows:

if(boolean expression) then --[ statements executed when the boolean expression is true --] end

When the boolean expression is true, the if code block is executed; when the boolean expression is false, the code immediately after the end of the if statement is executed.

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

The flowchart of the if statement is as follows:

Example

The following example is used to determine whether the value of variable a is less than 20:

Example

--[ define variable --]

a

=

10

;

--[ use the if statement --]

if

(

a

<

20

)

then

--[ print the following information when the if condition is true --]

   

print

(

"a is less than 20"

)

;

end

print

(

"The value of a is:"

,

a

)

;

The result of executing the above code is as follows:



a 小于 20

a 的值为:    10

Lua Flow Control Lua Flow Control