Could someone explain the concept of
[lua]for i = 1, 2 do[/lua]
I have no idea what this means.
Thanks! [import]uid: 50842 topic_id: 26842 reply_id: 326842[/import]
Could someone explain the concept of
[lua]for i = 1, 2 do[/lua]
I have no idea what this means.
Thanks! [import]uid: 50842 topic_id: 26842 reply_id: 326842[/import]
it means
set the variable i to be the value 1
do something
come back to this line and add 1 to i
do something
repeat until i is > 2 then don’t do the ‘something’, just go past the matching ‘end’ statement
A fuller version is
for i = 1 , 2, 1 do
which explicitly states that each time around things change by 1
because it could say
for i = 1, 2, 0.5 do
and you would get 3 passes through the code
[import]uid: 108660 topic_id: 26842 reply_id: 108950[/import]
Here is an easy example
[lua]–Just created a variable to store the value of i
local iCount = 0
–This will create a loop, the program will run everything with in i and i’s end 50 times in this example, starting with 1.
for i = 1, 50 do
–Each pass through the loop iCount will have 1 added to it for a maximum of 50
iCount = iCount + 1
print (iCount)
end[/lua]
That is a for loop at its simplest, it will run 50 times and each time it will add one to the iCount variable and after it runs 50 times its done.
Here is another example
[lua]–Create a Table variable
local iCount = {}
–Start the for loop, this time we will start the loop at 5 and end at 50
for i = 5, 50 do
–This will add a position in the iCount table. It will use what ever number i is on within the loop so if i is on the 32 loop then i = 32 and the position and value will be 32
iCount[i] = i
–once i == 50 we will print every value in the table which will be 5 - 50
if i == 50 then
–it does not have to be i that u use u can use any letter, like q
for q = 5, #iCount do
print (iCount[q])
end
end
end[/lua]
In the above example we are doing a loop from 5 - 50 that means that i will start on number 5 and increase until its 50. at each loop it will add a value to the table iCount at the current position in the loop that i is in.
it is a very powerful tool i use it alot to add a lot of screen elements at once or compare/store values. [import]uid: 126161 topic_id: 26842 reply_id: 108978[/import]