Introduction to Lua for loop
In Lua script for loop is one of the features and it is one of the conditional loop structure for initializing or declaring the values to the variables which depends upon both user or customized and predefined variables, check the conditions by using some conditional operators, increment or decrement the variables with satisfied the boolean conditions if the loop is satisfied their conditions it will execute the other statements that is if a boolean condition is true else if the condition is false it will terminate the loop so that the for loop is the repetition loop control structure which depends upon the user condition mentioned in the loop statement.
Syntax:
In Lua script, it has some basic functionality for each keyword, variables, and method for both user or custom items. Here for loop is one of the conditional loop structures and it is used for iterating the values with different time intervals. Based on the initialization the values will be validated and increment.
for initialise variable with value, maximum/ minimum conditional value, increment/ decrement the values
do
----some lua script code logics depends upon the requirement----
End
The above codes are the basic syntax for utilizing the for loop to iterate the values. We also use the do keyword for start or performing the loop operations once evaluated the conditions the loop will end by using the end keyword.
Flowchart
The Lua script has separate workflows for every conditional statement and method. We have a flowchart to know the workflow of the Lua script-based application.
<image>
The above diagram is the flowchart and workflow for the for loop structure.
How for loop works in Lua?
Generally for loop is one of the features and conditional statements for executing the application. Due to the declaration and initialization of datas, it can be populated and structured the application in more sophisticated. Whenever we initialize the variable it must be the first and most priority thing for any loops in the script for controlling the variables. Then next step we can check and evaluate the variables with some conditions like maximum or minimum values to execute the statements with further steps. Once completed in the second step it goes to the third and final step of the for loop that is we can increment/decrement the variables. Based on the user requirement we can increase or decrease the variable values this statement is only for updating the loop variables with control structures. If the above steps are evaluating successfully, then the boolean condition is assigned as true, or else it goes to the false statement, and also it terminates the loop automatically in the script. So that for loop is the most and repetition control structure for the user data’s in more efficiently and processed or operate the loop with several numbers of times which depend upon the user requirement.
Example #1
function fun(j)
print("Welcome To My Domain Its a First Example")
return j*12/2
end
vars = {1,2,3,4,5}
for k,l in ipairs(vars) do print(l) end
for i=0,fun(1) do print(i)
print("Thanks users your first example over")
end
Output:
The above example we used for loops in different formats. Here we can use one function and we are passing the parameters as func(j). By using this variable we can perform some mathematical operations like j*12/2 with the help of this formula we can calculate user input values with the help of for loop the values are repeating and iterating the input values and printed it on the output console. We used the recursion concept for calculating the output with the mentioned user inputs.
Example #2
a="123 456 789 12354 643 -6313 4652 2156435 62134 1275 127536 21756 217564 21753 2175 1275 2156 21756 1275 7215"
b=loadstring("return {"..a:gsub("%s+",",").."}")()
for i,j in ipairs(b) do print(i,j) end
str = "Welcome To My Domain Its a second example"
for k in string.gmatch(str, "%a+") do
print(k)
end
if(str == 'Welcome') then
print("Your input is validated")
else
print("Thanks for giving the inputs to the screen and it will not get the exact match \n so Please provide your inputs it will helpful for validating the code")
end
Output:
In the second example, we used a variable with string type values that is the integer inputs that are entered in the string using “” double-quotes. With the help of the loadstring() method, the input values are segregated and splitted it. While we use for loop to iterate and match the exact inputs to the number formats. Here string.gmatch() method will evaluate and match the exact user inputs to the other type like string or integer. We can validate the given input strings in if loop also it is one of another condition loop statements for validating the user datas. After iterating and matching the user input string to integer type we can validate the values in the if statement.
Example #3
vars=string.format
vars1 = {"one two three four five"}
vars1[0]="zero"
vars1[1] = "one"
vars1[2] = "two"
print("\nWelcome To My Domain its third example")
local t=0
for t= 1, #vars1 do
print(vars("t=%s, u=%s", tostring(t), tostring(vars1[t])))
end
print("\n Thanks for giving the inputs to the screen and will get the exact match outputs")
for t=2, table.maxn(vars1) do
print(vars("t=%s, u=%s", tostring(t), tostring(vars1[t])))
end
Output:
In the final example, we used for loops to iterate the values and also by using tostring() method for to map the exact number to string type values. Each value will have stored as separate rows and columns in the table structure. Here table keywords will help to create and store the data’s in the table format.
Conclusion
In conclusion, part Lua script is one of the system-level programming languages and these scripts are validating with client-side. Like that for loop is one of the basic conditional statement and using these loop n number of values will be iterating and printed it. The Lua script supports all types of browsers and operating systems.
Recommended Articles
This is a guide to Lua for loop. Here we discuss How for loop works in Lua and Flowchart along with the examples and outputs. You may also have a look at the following articles to learn more –
41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses