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

Lua if Nested Statements

Lua if Nested Statements

Lua Flow Control Lua Flow Control


if...else statement

Lua if statements allow nesting, which means you can insert other if or else if statements inside an if or else if statement.

The syntax of Lua if nested statements is as follows:

if( boolean expression 1) then --[ execute this block when boolean expression 1 is true --] if(boolean expression 2) then --[ execute this block when boolean expression 2 is true --] end end

You can nest in the same way else if...else if statement.

Example

The following example is used to judge the values of variables a and b:

Example

--[ define variable --]

a

=

100

;

b

=

200

;

--[ check condition --]

if

(

a

==

100

)

then

   

--[ execute the following if condition check when the if condition is true --]

   

if

(

b

==

200

)

   

then

     

--[ execute this block when the if condition is true --]

     

print

(

"The value of a is 100, the value of b is 200"

)

;

   

end

end

print

(

"The value of a is :"

,

a

)

;

print

(

"The value of b is :"

,

b

)

;

The result of executing the above code is as follows:



a 的值为 100 b 的值为 200

a 的值为 :    100

b 的值为 :    200

Lua Flow Control Lua Flow Control