You are reading the article How Break Statement Works In Lua With Examples? updated in September 2023 on the website Benhvienthammyvienaau.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 How Break Statement Works In Lua With Examples?
Introduction to Lua BreakBreak 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax 1:
……………… while( EDUCBA < 200 ) do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA+10 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 #1Code:
EDUCBA = 100 print("Welcome to increasing numbers:") while( EDUCBA < 200 ) do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA+10 then break end endOutput:
Example #2Code:
EDUCBA = 200 print("Welcome to decreasing numbers:") do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA-10 if( EDUCBA < 100) then break end endOutput:
Example #3Code:
EDUCBA1 = 100 print("Welcome to increasing numbers:") while( EDUCBA1 < 200 ) do print("Now, Value is:", EDUCBA1) EDUCBA1=EDUCBA1+10 then break end end EDUCBA2 = 200 print("Welcome to decreasing numbers:") do print("Now, Value is:", EDUCBA2) EDUCBA2=EDUCBA2-10 if( EDUCBA2 < 100) then break end end Example #4Code:
EDUCBA=100 print ("Let's start ride:") while 10 do print (EDUCBA) EDUCBA = EDUCBA + 10 end print ("Ride is completed now")Output:
Example #5Code:
EDUCBA=200 while 10 do print (EDUCBA) if EDUCBA < 100 then break end EDUCBA = EDUCBA - 10 end print ("Ride came to halt!!")Output:
Example #6Code:
EDUCBA=100 print ("Let's start ride:") while 10 do print (EDUCBA) EDUCBA = EDUCBA + 10 end print ("Ride is completed now") EDUCBA=200 while 10 do print (EDUCBA) if EDUCBA < 100 then break end EDUCBA = EDUCBA - 10 end print ("Ride came to halt!!") Example #7Code:
EDUCBA = 10 print("Welcome to numbers multiplier:") while( EDUCBA < 50 ) do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA*2 then break end endOutput:
Example #8Code:
EDUCBA = 100 print("Welcome to numbers multiplier & adder :") while( EDUCBA < 500000000 ) do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA*EDUCBA+1 then break end end EDUCBA = 100 print("Welcome to numbers multiplier :") while( EDUCBA < 50000000000 ) do print("Now, Value is:", EDUCBA) EDUCBA=EDUCBA*EDUCBA 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 then break end endOutput:
ConclusionOn 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 ArticlesWe hope that this EDUCBA information on “Lua Break” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading How Break Statement Works In Lua With Examples?
Update the detailed information about How Break Statement Works In Lua With Examples? on the Benhvienthammyvienaau.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!