How to Use the if... else statement in Lua

Source: Internet
Author: User

How to Use the if... else statement in Lua

This article mainly introduces how to use the if... else statement in Lua. It is the basic knowledge of Lua beginners. For more information, see

The if statement can be followed by an optional else statement. if the Boolean expression is false, the statement is executed.

Syntax

The syntax of the if... else statement in Lua programming language is:

The Code is as follows:

If (boolean_expression)

Then

-- [Statement (s) will execute if the boolean expression is true --]

Else

-- [Statement (s) will execute if the boolean expression is false --]

End

If the Boolean expression is true, the if code block is executed, otherwise the else code block is executed.

The Lua programming language assumes that any combination of Boolean true and non-zero values is true and whether it is a Boolean false or zero value is false. However, it should be noted that the Lua zero value is considered true.

For example:

The Code is as follows:

-- [Local variable definition --]

A = 100;

-- [Check the boolean condition --]

If (a <20)

Then

-- [If condition is true then print the following --]

Print ("a is less than 20 ")

Else

-- [If condition is false then print the following --]

Print ("a is not less than 20 ")

End

Print ("value of a is:",)

When the above Code is created and run, it will produce the following results.

The Code is as follows:

A is not less than 20

Value of a is: 100

If... else statement

The if statement can be followed by an optional else if... else statement, which is very useful to test a single if... else if statement with various conditions.

When using the if, else if, else statements, remember to use:

If there can be zero or one else, but it must be before elseif.

After if, there can be zero to many else if before else.

Once an else if succeeds, other elseif will not be tested.

Syntax

If... else the syntax of the else statement in Lua programming language is:

The Code is as follows:

If (boolean_expression 1)

Then

-- [Executes when the boolean expression 1 is true --]

Else if (boolean_expression 2)

-- [Executes when the boolean expression 2 is true --]

Else if (boolean_expression 3)

-- [Executes when the boolean expression 3 is true --]

Else

-- [Executes when the none of the above condition is true --]

End

For example:

The Code is as follows:

-- [Local variable definition --]

A = 100

-- [Check the boolean condition --]

If (a = 10)

Then

-- [If condition is true then print the following --]

Print ("Value of a is 10 ")

Elseif (a = 20)

Then

-- [If else if condition is true --]

Print ("Value of a is 20 ")

Elseif (a = 30)

Then

-- [If else if condition is true --]

Print ("Value of a is 30 ")

Else

-- [If none of the conditions is true --]

Print ("None of the values is matching ")

End

Print ("Exact value of a is:",)

When the above Code is created and run, it will produce the following results.

The Code is as follows:

None of the values is matching

Exact value of a is: 100

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.