How to use the IF ... else statement in Lua

Source: Internet
Author: User
Tags lua

This article mainly introduces the usage of the IF ... else statement in Lua, which is the basic knowledge in Lua's introductory learning, and can be consulted by a friend.

An If statement can be followed by an optional else statement, when the Boolean expression is executed with a false statement.

Grammar

The syntax for the IF ... else statement in the LUA programming language is:

The code is as follows:

if (boolean_expression)

Then

--[statement (s) would execute if the Boolean expression is true--]

Else

--[statement (s) 'll execute if the Boolean expression is false--]

End

If the Boolean expression evaluates to True, then the if code block will be executed, otherwise else code blocks will be executed.

The LUA programming language assumes a value of false if any combination of Boolean true and non-0 values is true and whether it is Boolean false or 0. It should be noted, however, that the LUA 0 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:", a)

When the above code is built and run, it produces the following results.

The code is as follows:

A is not less than 20

Value of a is:100

If...else If...else Statement

An If statement can be followed by an optional else if ... else statement, which is useful to test various conditions for a single if...else if statement.

When using if, else if, the else statement has a few points to remember to use:

If there can be 0 or one else, it must be preceded by a elseif.

There can be 0 to many else if after else if.

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

Grammar

The syntax for If...else if...else...else statements in the 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:", a)

When the above code is built and run, it produces the following results.

The code is as follows:

None of the values is matching

Exact value of a is:100

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.