Lua Logo
Chunks / Blocks

To move on with LUA we need to learn more about the already mentioned blocks or chunks.
As also mentioned earlier everything in LUA will be done through chunks. Like a russian Matryoshka.

Matrjoschka
Everybody has seen it before, I guess.

Each is pluged into another.
However, there we have a special difference to LUA. At LUA we are able to put a lot of chunks into one chunk. And these amount of chunks can accommodate also a lot of another chunks and so on.



From my point of view, the biggest puppet is the project/script. Inside the script we find different and a lot of assignments and chunks.

Don't be afraid of the picture below. We will be engaged in chunks for a long time and will be able to understand it.

BlockBild
Chunks do have an envelope or wrapper e.g. started with function() and finished by the key word end. In between these two lines something will happen. We do not have to know right now what will happen. This is not important at this moment. However, we are going to learn all possible envelopes.

It takes place like this:

The biggest chunk is the project/script.

The interpreter reads line after line.
Statements and chunks will be executed directly.
If he has to know a detail of a statement whose definition follows later on, an error occurs. Because, he doesn't know this at this moment.
Functions will not be executed. They will be saved in the meantime and will be executed when they are called.

Every chunk which is shown on the right side in the picture can include other chunks.
This sounds more complicated than it is. However, following is really important:
All chunks or blocks have to be started with an according key word and have to be finished

(with one exception) with the word end.

We already mentioned that functions are chunks. Let us have a look on the other possibilities.



nach obenConditional structure

    1.  if condition then
            block
        end
    
    2.  if condition then
            block1
        else
            block2
        end
    
    3.  if condition1 then
            block1
        elseif condition2 then
            block2
        elseif condition3 then
            block3
        else
            block4
        end

nach obenFunctions

    1.  function Name()
            block
        end
    
    2.  Name = function()
            block
        end

nach obenLoops
    
    1.  for variable = beginning, end, step do
            block
        end
    
    2.  for variable, name  in table do
            block
        end
    
    3.  while condition do
            block
        end
    
    4.  repeat
            block
        until condition
    

There we have the chunck, not ending with the word end. It is called repeat. We will use it at another chapter.


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