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

Lua if...else Statements

Lua if...else Statements

Lua Flow Control Lua Flow Control


if...else statement

The Lua if statement can be used together with the else statement; when the if conditional expression is false, the else code block is executed.

The syntax of the Lua if...else statement is as follows:



if(布尔表达式)

then

   --[ Execute this block when the boolean expression is true --]

else

   --[ Execute this block when the boolean expression is false --]

end

When the boolean expression is true, the if code block is executed; when the boolean expression is false, the else code block 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 the value of variable a:

Example

--[ define variable --]

a

=

100

;

--[ check condition --]

if

(

a

<

20

)

then

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

   

print

(

"a is less than 20"

)

else

   

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

   

print

(

"a is greater than 20"

)

end

print

(

"The value of a is :"

,

a

)

The result of executing the above code is as follows:



a 大于 20

a 的值为 :    100


if...elseif...else statement

The Lua if statement can be used together with the elseif...else statement; when the if conditional expression is false, the elseif...else code block is executed, which is used to detect multiple conditions.

The syntax of the Lua if...elseif...else statement is as follows:



if( 布尔表达式 1)

then

   --[ Execute this block when boolean expression 1 is true --]



elseif( 布尔表达式 2)

then

   --[ Execute this block when boolean expression 2 is true --]



elseif( 布尔表达式 3)

then

   --[ Execute this block when boolean expression 3 is true --]

else 

   --[ If none of the above boolean expressions are true, then execute this block of statements ]

end

Example

The following example determines the value of variable a:

Example

--[ define variable --]

a

=

100

--[ check boolean condition --]

if

(

a

==

10

)

then

   

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

   

print

(

"The value of a is 10"

)

elseif

(

a

==

20

)

then

 

   

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

   

print

(

"The value of a is 20"

)

elseif

(

a

==

30

)

then

   

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

   

print

(

"The value of a is 30"

)

else

   

--[ print the following information when none of the above conditions is true --]

   

print

(

"No value matching a"

)

end

print

(

"The actual value of a is: "

,

a

)

The result of executing the above code is as follows:



没有匹配 a 的值

a 的真实值为:     100

Lua Flow Control Lua Flow Control