Loops.....

Could anyone explain to me what loops are and what they are used for?

Here are 3 examples of loops. Loops are used for doing something multiples times. The 1st loop prints the contents of a table. The next 2 loops just print the value of z. You can do just about anything in a loop. Just to let you know, Codea runs in a loop. The draw function gets run over and over again.


function setup()
    tab={"a","b","c","d","e","f","g","h"}
    for a,b in pairs(tab) do
        print("pairs loop ",a,b)
    end
    
    for z=1,10 do
        print("for loop ",z)
    end
    
    z=0 
    while z<10 do
        print("while loop ",z)
        z=z+1
    end
end

@dave1707 thanks i will practice a bit more

‘For loops’ just index a table if that’s what you need, depending on what sort of table you have, the index of a table is the bit inside the brackets tbl[“index”] can be a string or a number, for a table indexing numbers I would use a normal for loop for i=1,20 do blah blah end, that iterates in one frame 20 times but if indexing with strings I would use a pairs table which instead of just a number ‘i’ from 1-20 gives you the string of each index as ‘k’ and the object as ‘v’ if you use a for k,v in pairs(tbl) do blah blah end

There are many other ways of using a for loop other than ‘just’ a table such as a math equation that you need to find 20 points on…