How do for do loops work?

Hi, I have been trying to learn to script about 3 - 4 weeks I’ve been trying to learn for do loops but I kinda get confused I always think they are a while loops I, hope you could explain it to my small brain.

They are iterators. They loop through lines of code a specified number of times.

3 Likes

Think of it like a loop that plays as much as you want. For instance I want this loop to run 5 times. Or 6 times. There is a number that specifies the number of uses.

3 Likes

They are nicely explained here Documentation - Roblox Creator Hub.

2 Likes

I used this quote below on another topic where someone asked what a for [...] do loop is and how it works.
It is not as throughout explained as the devhub article sent by @vwize, but it’s also pretty understandable (I hope).

2 Likes

For loops are basically loops that run a certain amount of time.

for i = 0, 10, 2 do
    print(i)
end

The first number, 0 in this case is the starting point, from what value to start looping, the second is the goal value, the value that we wanna achieve, in this case 10, and the third number is the increment, basically "by how much we add to i each time we loop`, so each step, each iteration, we go up by 2.

In conclusion, what this loop is saying is: go from 0 to 10, by adding 2 each time.
Which technically means that this loops will loop 6 times (not 5, because at first the loop makes an iteration without adding any value, it just stays at 0), we go from 0 to 2, from 2 to 4… from 8 to 10.

The i variable, which can be named anything else it doesn’t have to be i, resembles the value that shows where we’re at each time we loop.

for i = 0, 10, 2 do
    print(i)
end

This would print as follow:

0
2
4
6
8
10

7 Likes

for loops are much more simpler than you may think.

Let’s say we have to repeat some code a certain amount of times, say 5. We can easily do this by doing:

for i = 1, 5 do
    -- run your code
end

As you can see, there are two numbers sent: the first number is the number you want to start with (1) and the second number is the number you want to end with (5).

Additionally, there is a third number you can input, which would essentially increment from the first number all the way until you reach a number greater or equal to the second number.

for i = 1, 5, 3 do
    print(i)
end

Output:
(first loop) 1
(second loop) 1+3 -> 4
--(now the loop stops as if you do 4+3, it equals greater than the second number, 5.)

Now, for loops can also go “through” a table, whether it is an array or a dictionary.

If we have an array, we can iterate through it using the number for loop like previously or ipairs.

Here is an example:

local array = {2,2,2,2,2,22,2,"value", false, "epic", 3.4}
local NumberOfItems = #array -- gets the total number of items in the array

for i = 1, NumberOfItems do -- we start at one, and end at the total amount of elements
   print(array[i])
end

or 

for i, v in ipairs(array) do
   print(i, v) -- the current iteration position and the value
end

Let’s say you wanted to get the items below (aka the children of) a certain object, let’s say the workspace, you can easily do this by doing:

local ChildrenOfWorkspace = game:GetService("Workspace"):GetChildren()

for i, v in ipairs(ChildrenOfWorkspace) do
   print(i, v)
end

This logic also works with dictionaries (t = {a = 1, b = 2, c = 3, ...}), but we use pairs instead of ipairs.

If you need any clarification or anyone have anything to correct please lmk. :smile:

3 Likes

Numeric for loops

I believe the above replies explain this well. A numeric for loop is for doing something a set amount of times. But I do not feel generic for loops are explained at all/well enough.

Generic for loops

A generic for loop usually this format

for variable_list in iter, invariant_state, init do

end

iter is meant to be a function that will be called with arguments invariant_state and init until it returns nil. The variable list is a list of variables that get the values of the returns of the function

Here is a quick example of ipairs, but with a twist: it goes backwards

local function iter(a, i)
    i = i - 1

    local v = a[i]

    if v ~= nil then
        return i, v
    end
    return nil
end
local t = { "a", "b", "c" }

for i, v in iter, t, #t + 1 do
    print(i, v)
end

-- c 3
-- b 2
-- a 1

t is the invariant, and init is where it starts here. Now functions like pairs and ipairs are known as iterator factories because they return the iterator functions, as well as arguments for it. They are generally clearer to
read.

Let’s wrap the custom reverse ipairs function:

local function reverse(a)
    return iter, a, #a + 1
end

for i, v in reverse(t) do
    print(i, v)
end

Same exact result. Only that the wrapper makes it clearer.

2 Likes

next is equivalent to pairs, not ipairs. However, there’s no benefit to manually using next anymore.

3 Likes