<< Click to Display Table of Contents >> Navigation: Multi-Protocol MasterOPC Server > Lua 5.1 Reference Manual > The Language and syntax > Statements > Cycles while, repeat and for |
Lua provides 3 types of cycles: while, repeat and for.
The "while" cycle
The "while" cycle is a cycle with a precondition, that is, a condition of cycle execution is set in the declaration beginning. The cycle is executed while the condition is true.
while expression do
--cycle code
end
Example
a=1
while a<10 do --condition of execution: execute while a is less than 10
a=a+1 --add 1
end
The "repeat" cycle
The "repeat" cycle is a cycle with a condition at the end, that is, contents of that cycle is executed 1 time at least. The cycle is executed until the condition becomes true (that is, while the condition is false).
repeat
--cycle code
until expression
Example
a=1
repeat
a=a+1 --add 1
until a>=10 --condition to go out of the cycle - a is greater or equal to 10
The "repeat–until" cycle ends in a condition that follows until, therefore that condition can operate with local variables described within the cycle.
The "for" cycle
The statement for can be typed in numerical or extended form.
In the numerical form, for executes a code block until the loop variable, which is changed in an arithmetic progression, reaches a threshold predefined.
for var,limit,step do
--cycle code
end
Here
•var - a variable incremented (a loop variable)
•limit - a threshold; the cycle is completed if var reaches the threshold
•step - a step of changing var; if not set, 1 is used
Example
k=0
for i=0,10,1 do --increase i from 0 up to 10, step equals 1
k=k+i; --add i to k
end
The extended form of for is developed, using functions of iterators. An iterator is called at each iteration in order to get a new value of a loop variable. The cycle is terminated if the iterator returns nil. As such an iterator, the pairs function is used. In C++ and C#, an analog of such a cycle is the cycle foreach. Using this type of cycles, it is convenient to look over values in arrays.
The extended statement for has the following syntax:
for namelist in explist do
--cycle code
end
As an example, let's summarize table elements, using both numerical and extended for.
Example of numerical for:
mytable={10,20,30,40} --initialization of table elements with values
sum=0 --variable to store a sum
for i=1,4,1 do --look over elements from 1 (numeration of table indexes starts with 1 in Lua) up to 4, step equals 1
sum=sum+mytable[i] --summarize
end
Example of extended for:
mytable={10,20,30,40} --initialization of table elements with values
sum=0 --variable to store a sum
for i,v in pairs(mytable) do --look over elements, i - value index, v - value
sum=sum+v --summarize
end
A logical expression in cycles (as well as in control structures) can return any value The values false and nil are considered as false. All other values (including 0 and an empty string!) are considered as true.
In order to terminate a cycle of any type, the break statement is used. Below is an example of termination of the for cycle:
mytable={10,20,30,40} --initialization of table elements with values
sum=0 --variable to store a sum
for i,v in pairs(mytable) do --look over elements. i - value index, v - value
sum=sum+v --summarize
if sum>80 then
break --terminate forcedly if the sum exceeds 80
end
end
Being typed within some cycle, the break statement terminates only that cycle; execution of external cycles continues.