Loops
As the meaning of the word loop already describes everything inside will be executed again and again.
Two different types of for - Loop are existing. Now will talk about the numerical one and we will come to the other type when we know a little bit more about tables.
How does the loop look like?
for variable = beginning, finish, step do
chunck
end
We do have:
- a variable
- a beginning
- a finish
- and a step to increment the variable
The for - Loop starts counting from the "beginning" until "finish" by increasing the "variable" according the size of "step". After executing the chunck, the value of "variable" and "finish" will be compared. If they are not equal the loop will be started again.
Example:
beginning = 5; finish = 11; step = 0.5; -- pay attention: the decimal point is a point
The chunck shall print the variable so we can see the result.
for i = 5, 11, 0.5 do
print(i)
end
Let's have a look on following picture

The first result we get is 5. So, "step" will not be added for the first time.
Then at every cycle our variable called i will be increased by 0.5. If variable gets the size of 11 the loop ends.
To be sure that the loop has really been finished I put the statement print("Ende") after the loop.
This kind of loop can also be executed backwards. We want to count down from 11 to 5. But what to do with our step? If we increase 11 by 0.5 we get 11.5. This seems to be the wrong way. We have to decrease by 0.5. How do we do this?
we take the "-"
This sign is to negate values.
It changes plus to minus and vice versa.
c = - a
if a == 2, than c == -2
We are going to use the above mentioned loop again but now with a negativ step. That means that we are increasing 11 by -0.5.
You think, that this expalantion is too complicated? Believe me, I do have reasons for this. It seems to be complicated now, but therefore it will be much easier to understand later on.
for i = 11, 5, -0.5 do
print(i)
end
Now: each cycle
- 11 plus -0.5 = 10.5 --> print --> 10.5
- 10.5 plus -0.5 = 10 --> print --> 10
- ...
- ...
- ...
- 5.5 plus -0.5 = 5 --> print --> 5
- finish of the loop
For this time without any picture. Try it by yourself.
What we have not known until now: our variable called i is a local one in our loop. That means she is not existing outside.
Let's try to change print("Ende") into print(i) as this statement is located outside the loop.
That's all about for-Loops.
while condition do
chunck
end
That means: since a condition is not true execute the chunck. Vice versa: if the condition isn't true anymore (false) than stop the loop.
But step by step:
- 1. it will be checked if the condition is true
- 2. if condition is true than execute the chunck
- 2.1 on "end" it will be checked if the condition is true or not
- 2.2 if the condition is still true the chunck will be executed again (back to 2.)
- 2.3 if the condition is not true (false) than stop the loop
For an example we are going to change our for - Loop into a while - Loop.
while i <= 11 do
print (i)
end
However, if the programm reaches the loop we will get a message, that "i" is not existing.
So we create an according variable
i = 5 -- because this is our "beginning"
before we get another message we insert a line which increases "i".
i = i + 0.5
However, where do we have to integrate this line to do the same as in our "for - Loop"?
It has to be integrated after the print(i) as we want to get the result on the screen for the value 5 as well.
Therefore:
i = 5
while i <= 11 do
print (i)
i = i + 0.5
end
We are going to try this in our editor and we are glad about the result.
But how does this work in detail?
- Before starting with the loop the variable i gets the value of 5.
- By starting the loop it will be checked if 5 is smaller or equal to 11.
- The result will be printed --> 5
- And then? No, it isn't a paradox as you might think. For this we should know how a mathematic operation works. This operation is an addition.
- At first the computer reads the value of i (5 for the first time) and puts it in the memory. Then he adds the value 0.5 to the value of i.
- The result will be stored in the same memory of the variable i. But now, the variable has a value of 5.5.
- Reaching the "end" it will be checked if 5.5 is smaller or equal to 11 and it is!
Try another kind of while-loop by yourself. For example a count down.
Gosh! Another kind of loop? Yes, but you will not have any problems with it.
repeat chunck
until condition becomes true or false
repeat
Chunck
until condition
Basically a repeat-loop doesn't differ from a while-loop.
The only difference is a small one and perhaps you have already been aware of it. The condition will be placed at the end.
In opposite to the while-loop, the repeat-loop will be executed at least for one time. And the condition looks a little bit different to do the same as in a while-loop.
Let's have a look on the code and try those loops by yourself as well.
i = 5
repeat
print (i)
i = i + 0.5
until i > 11
The loop will be executed until a condition becomes true. ( in our case, if "i" is bigger than 11)
A while-loop will be executed as long as a condition becomes true.
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006
Copyright © Robert Schmitz 2006