CompiledFrog

Post #25: The GDScript For Loop

Today, we look at the second kind of loop in Godot, the for loop!

Here is some code:

Post25-Photo1_2026-06-17_compiledfrog

Output:

burger
eggplant
donut
0
1
2

In the example above, we show two example ways you can use a for loop. The first is by iterating through an array. This could be an array of various different types; above we iterate through an array of strings. The second is by iterating through numbers by using range(), which in this case, apparently doesn't allocate an array, which is a good thing.

The various types of loops allow us to pick the correct one for our use case. We could use a while loop to iterate through an array, but then we would need a variable to keep the count as we progress through. If we know the array size, a for loop might be better. However, if we need to read through a file, a while loop would be better, because we can loop through until we reach the end of the file, and we don't know when that may be.

These are just small choices you make at a scripting level; all of these choices build into whatever game you decide to make! (Or program you decide to make, if it's not a game.)

What type of loop do you prefer?