Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is a for loop and what can it do?

Asked by 9 years ago

Can someone just explain to me in as much detail as possible what a for loop is and how it works and what stuff can it do?

2 answers

Log in to vote
2
Answered by
l0cky2013 135
9 years ago

There are two types of loops, a numeric for loop, and a generic for loop. They are both mainly used for iterating through tables of different kinds. Although the numeric for loop is also commonly used for countdowns.

Generic For

local tab={"Hello"}
for index, value in pairs(tab) do
print(index, value)
end

Will print "1 Hello."

Numeric For

local tab={"Hello"}
for index=1, #tab do
print(index, tab[index])
end

Will print "1 Hello" as well.

Countdown

local t=30 --time to countdown from
for index=t, 1, -1 do
print(index)
wait(1)
end
1
for the countdown one couldnt you just do for t,1,-1 do print(t) ?? NinjoOnline 1146 — 9y
Ad
Log in to vote
1
Answered by
Thetacah 712 Moderation Voter
9 years ago

For loops can be used for many things, like irritating through tables or making a loop go 10 times.

--This loop goes through a table and prints its index and value.

myTable = {"Thetacah", "Josh", "John"};
for i, v in pairs(myTable) do
wait(1)
print(i..v)
end

--This loop will print one through ten since it runs 10 times and i is the value of the ammount of times the loop runs.

for i = 1, 10 do
wait(1)
print(i)
end

Answer this question