Lua Decision Making- Lua if...else statement - Lua (programming language)

What is the use of Lua if...else statement?

if statement will be followed by an optional else statement, which is executed when the Boolean expression is false.

Syntax

Syntax of an if...else statement in Lua programming language is

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

Lua programming language assumes any combination of Boolean true and non-nil values as true, and if it is either Boolean false or nil, then it is assumed as false value. It is to be noted that in Lua, zero will be considered as true.

Flow Diagram

if_else_statement

Example

When you build and run the above code, it produces the following result.

The if...else if...else Statement

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

While using if , else if , else statements, there are a few points to keep in mind

  • An if can have zero or one else's and it must come after any else if's.
  • An if can have zero to many else if's and they must come before the else.
  • Once an else if succeeds, none of the remaining else if's or else's will be tested.

Syntax

Syntax of an if...else if...else statement in Lua programming language is

Example

When you build and run the above code, it produces the following result.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Lua (programming language) Topics