Updated April 11, 2023
Introduction to Lua Break
Break is one of the important statements while studying loops. In Lua, break statement enables us to come out of the inner block of a code. Break statement is used to end the loop. The break statement breaks the for, while, or repeat loop which includes the break statement. After the break loop, the code runs after the break statement and the broken loop. This article will help the readers in understanding the concept of break and how it is used in loops. Multiple examples are explained in the article below. The examples perform different functions and uses break statement in it.
Break statement in Lua, syntax:
Syntax 1:
………………
while( EDUCBA < 200 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA+10
if( EDUCBA > 200)
then
break
end
………………
Syntax 2:
…………
while 10 do
print (EDUCBA)
if EDUCBA < 100 then break end
EDUCBA = EDUCBA - 10
end
……………
How to work break statement in Lua?
Working of break statement in Lua explained with examples:
Lua is a multi-paradigm, high level and lightweight programming language. It is designed basically for embedded use in apps. It is cross-platform as the interpreter of the compiled code is in ANSI code. Lua has C API for embedding it into apps. Basically, Lua has meta-features that are flexible and simple enough to be extended whenever needed, as it doesn’t have features for a specific programming paradigm which makes the basic language lighter.
In the examples described below, a loop which prints numbers getting increased, decreased, multiplied or their combination after every iteration and the moment the value doesn’t follow the condition applied, the break statement does its work and the compiler jumps out of the loop.
Example #1
Code:
EDUCBA = 100
print("Welcome to increasing numbers:")
while( EDUCBA < 200 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA+10
if( EDUCBA > 200)
then
break
end
end
Output:
Example #2
Code:
EDUCBA = 200
print("Welcome to decreasing numbers:")
while( EDUCBA > 100 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA-10
if( EDUCBA < 100)
then
break
end
end
Output:
Example #3
Code:
EDUCBA1 = 100
print("Welcome to increasing numbers:")
while( EDUCBA1 < 200 )
do
print("Now, Value is:", EDUCBA1)
EDUCBA1=EDUCBA1+10
if( EDUCBA1 > 200)
then
break
end
end
EDUCBA2 = 200
print("Welcome to decreasing numbers:")
while( EDUCBA2 > 100 )
do
print("Now, Value is:", EDUCBA2)
EDUCBA2=EDUCBA2-10
if( EDUCBA2 < 100)
then
break
end
end
Output:
Example #4
Code:
EDUCBA=100
print ("Let's start ride:")
while 10 do
print (EDUCBA)
if EDUCBA > 200 then break end
EDUCBA = EDUCBA + 10
end
print ("Ride is completed now")
Output:
Example #5
Code:
EDUCBA=200
print ("Reverse Ride starts now:---")
while 10 do
print (EDUCBA)
if EDUCBA < 100 then break end
EDUCBA = EDUCBA - 10
end
print ("Ride came to halt!!")
Output:
Example #6
Code:
EDUCBA=100
print ("Let's start ride:")
while 10 do
print (EDUCBA)
if EDUCBA > 200 then break end
EDUCBA = EDUCBA + 10
end
print ("Ride is completed now")
EDUCBA=200
print ("Reverse Ride starts now:---")
while 10 do
print (EDUCBA)
if EDUCBA < 100 then break end
EDUCBA = EDUCBA - 10
end
print ("Ride came to halt!!")
Output:
Example #7
Code:
EDUCBA = 10
print("Welcome to numbers multiplier:")
while( EDUCBA < 50 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA*2
if( EDUCBA > 40)
then
break
end
end
Output:
Example #8
Code:
EDUCBA = 100
print("Welcome to numbers multiplier & adder :")
while( EDUCBA < 500000000 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA*EDUCBA+1
if( EDUCBA > 400000000)
then
break
end
end
EDUCBA = 100
print("Welcome to numbers multiplier :")
while( EDUCBA < 50000000000 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA*EDUCBA
if( EDUCBA > 40000000000)
then
break
end
end
EDUCBA = 100
print("Welcome to numbers multiplier and subtractor :")
while( EDUCBA < 50000000000 )
do
print("Now, Value is:", EDUCBA)
EDUCBA=EDUCBA*EDUCBA-1
if( EDUCBA > 4000000000000)
then
break
end
end
Output:
Conclusion
On the basis of the above article, we understood Lua and its concept of break statement. The break statement is an important part of a loop and the examples explained in the article could help in understanding its importance and implementation in a program.
Recommended Articles
We hope that this EDUCBA information on “Lua Break” was beneficial to you. You can view EDUCBA’s recommended articles for more information.