Lua Logo
Loops

As the meaning of the word loop already describes everything inside will be executed again and again.

nach obennumerical for - Loop


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:

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 = 5110.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 = 115, -0.5 do
         print(i)
    end
    
Now: each cycle


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.


nach obenwhile - 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:

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?

Later on the variable i will not be smaller or euqal to 11 and then the loop ends.

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.

nach obenrepeat - Loop
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