for loop

A loop that runs its body once for each item in a list or other collection, setting a loop variable to the current item.

In more detail

You name the loop variable and what to loop over, and the body runs once per item, such as for item in items in Python or for f in *.md in a shell script. Many languages also have a counting form that repeats a fixed number of times, such as range in Python or for ((i = 1; i <= 3; i++)) in Bash.

Examples

for file in notes/*.md; do echo "$file"; done
for word in ["red", "green"]:
    print(word)

Related terms

Taught in

References

Certification courses