Updated April 20, 2023
Introduction of Lua If
The world is full of ifs and but a so why it wouldn’t be present in the programming world. Lua also has an if statement for programming the conditions. If statement in Lua is just like other programming languages including C, C++, Python. If a statement is very important while programming as it leverages the option of creating a condition where the coder can derive two outputs for the true and false results respectively. In this article, if the statement is explained in detail with examples. The syntax of if is explained in the article and the whole process of if the statement is explained through a flowchart.
Syntax:
if( Rahul< 50 )
then
print("Rahul age is less than 50" )
end
If Statement Flowchart with Working
The flowchart drawn below describes the process of an if statement. In the flowchart, we can see that the first thing in an if the program is the condition. In the condition part, one has to write the if statement. The if statement can contain logical and arithmetic operators. Here, once the program runs, it checks the first block for decision making. Now, if the if the statement is true then the program will complete the if operation and will output the result specified for the true condition. In case the if the statement isn’t true then the program will skip the if operation and will directly move out of the if block and will move ahead to the other blocks of codes present after it.
Usage of If Statement in Lua with Examples
Following are the examples are given below:
Example #1
Code:
Age = 5;
if( Age< 50 )
then
print("My age is less than 50" )
end
print("My age is :", Age)
Output:
Code:
Age = 105;
if( Age< 50 )
then
print("My age is less than 50" )
end
print("My age is :", Age)
Output:
Example #2
Code:
Rahul = 105;
if( Rahul< 50 )
then
print("Rahul age is less than 50" )
end
print("Rahul age is :", Rahul)
Ankush = 15;
if( Ankush< 50 )
then
print("Ankush age is less than 50" )
end
print("Ankush age is :", Ankush)
if (Rahul > Ankush)
then
print("Rahul is elder than Ankush" )
end
Output:
Example #3
Code:
Age = 150
print("Actually I am: ", Age, "years old" )
if( Age == 60 )
then
print("Voila!, your age is 60" )
if( Age == 5 )
then
print("Voila!, your age is 5" )
if( Age == 0 )
then
print("Voila!, you are not born :P" )
end
end
end
print("Told you man! my age is: ", Age )
Output:
Code:
Age = 0
print("Actually I am: ", Age, "years old" )
if( Age == 60 )
then
print("Voila!, your age is 60" )
end
if( Age == 5 )
then
print("Voila!, your age is 5" )
end
if( Age == 0 )
then
print("Voila!, you are not born :P" )
end
print("Told you man! my age is: ", Age )
Output:
Example #4
Code:
RahulAge = 150
print("Actually Rahul is: ", RahulAge, "years old" )
if( RahulAge == 60 )
then
print("Voila !, Rahul age is 60" )
end
if( RahulAge == 5 )
then
print("Voila !, Rahul age is 5" )
end
if( RahulAge == 0 )
then
print("Voila !, Rahul is not born :P" )
end
print("Told you man! Rahul age is: ", RahulAge )
AnkushAge = 0
print("Actually Ankush is: ", AnkushAge, "years old" )
if( AnkushAge == 60 )
then
print("Voila !, Ankush age is 60" )
end
if( AnkushAge == 5 )
then
print("Voila !, Ankush age is 5" )
end
if( AnkushAge == 0 )
then
print("Voila !, Ankush not born :P" )
end
print("Told you man! Ankush's age is: ", AnkushAge )
Output:
Example #5
Code:
Age = 20;
if( Age< 100 )
then
Agenew = 20*5
print("My new age is :", Agenew )
end
print("My earlier age was :", Age)
Output:
Example #6
Code:
Age = 20;
if( Age< 100 )
then
Agenew = 20*5
print("My new age is :", Agenew )
end
if( Age == 20 )
then
Agenew = 20-5
print("Age of mine, 5 years ago was :", Agenew )
end
print("My earlier age was :", Age)
CashAge = 8;
LeftAge = 8;
print("Cash is the sweetest angel")
if( CashAge< 100 )
then
Agenew = 20*5
print("Wanted my cash to live :", Agenew, "years")
end
if( LeftAge == 8 )
then
LeftAge1 = 20 - 12
print("Cash left me when he was", LeftAge1, "years old" )
end
print("Cash today age of cash would be :", CashAge, "years")
Output:
Recommended Articles
We hope that this EDUCBA information on “Lua If” was beneficial to you. You can view EDUCBA’s recommended articles for more information.