Lua if statement


Release date:2023-09-29 Update date:2023-10-13 Editor:admin View counts:287

Label:

Lua if statement

Lua if statement consists of a Boolean expression as a conditional judgment, followed by other statements.

The syntax format of the Lua if statement is as follows:

if(Booleans)
then
   --[Statement executed when Boolean expression is true --]
end

When the Boolean expression is true at that time if block of code in is executed, when the Boolean expression is false , follow closelyat the if statement end , the subsequent code will be executed.

Lua believes that false and nil are false, while true and non nil are true. It should be noted that 0 in Lua is true.

The if statement flow chart is as follows:

Image0

Example

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

Example

--[Define variables --]
a = 10;

--[ Using the if statement --]
if( a < 20 )
then
   --[ Print the following information when the if condition is true --]
   print("A less than 20" );
end
print("The value of a is:", a);

The execution result of the above code is as follows:

A less than 20
The value of a is: 10

Powered by TorCMS (https://github.com/bukun/TorCMS).