Updated April 10, 2023
Introduction to Lua else if
Whenever there is a necessity to test several conditions using single if statement, then we are going to make use of else if statement following the if statement ending with else statement in Lua programming language and usage of else if statement is optional and of all the else if statements, success of the first else if statements eliminates the need to test the other else if statements and the if.. else if.. else statement ends with the keyword end in Lua programming language and there can be any number of else if statements in a single if statement ending with else and end keyword.
Syntax to define else if statement in Lua:
if(boolean_expression 1)
then
block of code to be executed when Boolean_expression 1 is evaluated to True
elseif(boolean_expression 2)
block of code to be executed when Boolean_expression 2 is evaluated to True
elseif(boolean_expression 3)
block of code to be executed when Boolean_expression 3 is evaluated to True
.
.
.
else
block of code to be executed when none of the above conditions is evaluated to True
end
Where,
boolean_expression 1, boolean_expression 2, boolean_expression 3 are the conditions which are evaluated to True or False.
Flowchart
Flowchart to demonstrate the else if statement in Lua is as follows:
Working of else if Statement in Lua
- Whenever there is a necessity to test several conditions using single if statement, then we are going to make use of else if statement following the if statement ending with else statement in Lua programming language.
- Of all the else if statements, success of the first else if statements eliminates the need to test the other else if statements.
- The usage of else if statement is optional is Lua programming language.
- The if.. else if.. else statement ends with the keyword end in Lua programming language.
- There can be any number of else if statements in a single if statement ending with else and end keyword.
Examples of Lua else if
Given below are the examples of Lua else if:
Example #1
Lua program to demonstrate else if statement to check if a number is negative, positive or zero and display the appropriate message as the output on the screen.
Code:
--[storing a numerical value in a variable called value--]
value = 100
--[writing if condition statement to check the condition if the given value is greater than 0--]
if( value > 0 )
then
--[ the below statement is displayed as the output on the screen if the condition evaluates to True --]
print("The given value is positive" )
--[writing else if condition statement to check the next condition if the given value is lesser than 0--]
elseif(a < 0)
then
print("The given value is negative" )
else
--[ the below statement is displayed as the output on the screen if all the above conditions evaluates to False --]
print("The given value is zero")
end
Output:
In the above program, we are storing a numerical value in a variable called value. Then we are writing if condition statement to check the condition if the given value is greater than 0. Then a statement stating the given value is greater than zero is displayed as the output on the screen if the condition evaluates to True. Then we are writing else if condition statement to check the next condition if the given value is lesser than 0. Then a statement stating the given value is lesser than zero is displayed as the output on the screen if the condition evaluates to True. Then a statement stating the given value is equal to zero is displayed as the output on the screen if the above two conditions evaluates to False.
Example #2
Lua program to demonstrate else if statement to check if a given string matches the string values and display the appropriate message as the output on the screen.
Code:
--[storing a string value in a variable called value--]
value = "EDUCBA"
--[writing if condition statement to check the condition if the given value is same as the string--]
if( value == "EDUCBA" )
then
--[ the below statement is displayed as the output on the screen if the condition evaluates to True --]
print("The given value is matching" )
--[writing else if condition statement to check the next condition if the given value does not match the above string--]
elseif(value == "Learning")
then
print("The given value is not matching" )
else
--[ the below statement is displayed as the output on the screen if all the above conditions evaluates to False --]
print("The given value does not match anything")
end
Output:
In the above program, we are storing a string value in a variable called value. Then we are writing if condition statement to check the condition if the given value matches a string. Then a statement stating the given value is matching is displayed as the output on the screen if the condition evaluates to True. Then we are writing else if condition statement to check the next condition if the given value does not match the above string. Then a statement stating the given value is not matching is displayed as the output on the screen if the condition evaluates to True. Then a statement stating the given value does not match anything is displayed as the output on the screen if the above two conditions evaluates to False.
Recommended Articles
We hope that this EDUCBA information on “Lua else if” was beneficial to you. You can view EDUCBA’s recommended articles for more information.