What is the for-in keyword used for?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JulioYagami

I ever see codes like:

for i in 10:
    # do something
1 Like
:bust_in_silhouette: Reply From: kidscancode

for is used to create a loop that iterates through a range of values. The most common form is a loop to count through numeric values:

for i in range(10):
    print(i)

This would print the numbers from 0 through 9.

The form you quoted above is an abbreviation. If you omit the range() function GDScript will assume it.

Note you can also iterate through arrays, strings, and dictionaries as well.

See here for more details:

1 Like