Updated April 5, 2023
Introduction to Lua while Loop
Lua is an embeddable scripting language built on top of C programming and is known for being efficient and light weight along with object-oriented and procedural features. This language can be used from complex systems to mobile applications as well. Lua while loop is the same as any while loop in a programming language where a set to executional steps will be performed until the given condition satisfies to a true value.
Syntax:
The syntax for Lua while loop is as simple as writing a loop with a while clause.
while(conditionalCheck)
do
Executional Statements
end
- conditionalCheck: Any value whose result is boolean returns either a True or a False Boolean value.
- Executional Statements: These are a set of statements which we want to execute in case the while loop return True. It can be a simple statement or multiple statements.
Flowchart
Given below is the flowchart mentioned:
How while Loop Works in Lua?
As every while loops work in programming language similarly while loop functions in Lua programming. Lua Programming will first check for the while condition, and if condition evaluation turns out to be true, then Lua will execute the body inside the do and repeat the same process again. On the other hand, if the evaluation of the while condition turns out to be false, Lua will end the loop and exit from there without executing the body inside the do loop. Another interesting part to keep a note about while loop in Lua is that say for example, at the first set of execution-only the evaluation of condition inside while loop turns out to be false then none of the steps inside do will get executed and there would be no execution in this case.
Below are some of the important steps of Lua Do while loop as per the flow diagram:
- The conditional check that needs to be evaluated: The conditional code within these round brackets is used to evaluate conditions that need to be applied by the developer inside the while clause. If this condition of evaluation gets satisfied, then the executional statement inside the do clause gets executed. A typical example may be to check if the variable x is less than 10.
- Executional statements: Here, we add those codes that need to be performed once the condition gets satisfied from the while clause and the execution are inside the do loop is initiated. A typical example may be to print the value of the variable over which the loop is running.
- Increment of the value: Here, a simple value is incremented to modify the conditional check value once the loop is repeated. The value of the variable which is incremented is the variable using which the loop is executing. If this line is not written, then your do while loop might end up in invite looping, and the application might crash.
Here we will be going over the normal variable increment example and also over a factorial program where decremental value is set as conditional check inside while loop and do is executed based on that value.
Examples of Lua while Loop
Given below are the examples of Lua while Loop:
Example #1
Write a program to loop a variable from 1 to 10.
Code:
local test = 1
while test < 10 do
print("Recurring Value : ",test)
test = test + 1
end
Here we have initialised a local variable with the name as test and whose initial value is set to 1. The next line is using a while loop with a condition as test < 10. Which means every time in the loop, Lua will check for the current value of the test to be less than 10. If the value of a test is less than 10, then the 2 statements inside do will get executed. First, we are printing the value of the test. Next statement, we are incrementing the test value by 1. If the value of a test is greater than 10, then Lua program execution directly jumps to the end statement without executing the 2 lines which are inside the do loop.
At the end of Lua programming, we mention the end tag to exit out of the program.
Output:
Example #2
Write a program to print factorial of a 15 using while Loop.
Code:
local i = 15
local factorial = 1;
while (i >= 1)
do
factorial = factorial * i;
i = i - 1;
print("The factorial of the number entered by the user is:", factorial)
end
Here we have initialised a local variable with the name as I and factorial and both of these initial value is set to 1. The next line is using a while loop with a condition as (i >= 1). Which means every time in the loop, Lua will check for the current value of the test to be greater than equal to 10. If the value of i is greater than equal to 1, then the 2 statements inside do will get executed. So we are multiplying factorial with a value i.
Next statement, we are decrementing the i value by 1. If the value of the test is less than equal to 1, then Lua program execution directly jumps to the end statement without executing the 2 lines which are inside the do loop and Print the factorial value.
At the end of Lua programming, we mention the end tag to exit out of the program.
Output:
Conclusion
As Loops are essential building blocks of every programming language, they are an important part of Lua programming language, which is built on top of the C programming language. There are lots of use cases of While loop where a developer needs to restrict the use case based on certain conditions, and once the condition is not satisfied, stop the execution and exit out of the program. There are a lot more other loops which can be used with the Lua programming language.
Recommended Articles
We hope that this EDUCBA information on “Lua while Loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.