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"; donefor word in ["red", "green"]:
print(word)Related terms
- while loop — A loop that keeps repeating its block as long as a condition stays true, checking the condition before each pass.
- until loop — A loop that runs its body again and again until a test command succeeds, the reverse of while.
- break and continue — Builtins that change a loop's flow: break leaves the loop at once and continue skips to the next turn.
- shell array — A variable that holds a numbered list of separate values, written in bash as names=(one "two words" three).
- loop variable — The name in a for heading that takes each item of the sequence in turn, as item in for item in items.
- range() — A built-in that produces a sequence of whole numbers on demand, such as range(5) for 0, 1, 2, 3, 4.
- iterable — Any object you can loop over with for, such as a list, string, tuple, dictionary, set, range, file or generator.
- accumulation pattern — A loop pattern that builds a result step by step, starting from an initial value and combining each item into it, such as adding up a list.
Taught in
- Arrays, And Why Dollar At Is Not Dollar Star — Bash Scripting & Command Line Automation
- Loops: for, while, until, break, continue — Bash Scripting & Command Line Automation
- Arguments: Dollar One, Dollar At, shift — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation
- What A Program Really Is — Hands-on Python: Complete Video Course & Book
- How Python Runs Your Code — Hands-on Python: Complete Video Course & Book
- A Sample Program, Explained — Hands-on Python: Complete Video Course & Book
- Lists, Range And The For Loop — Hands-on Python: Complete Video Course & Book
- Nesting Control Flow Statements — Hands-on Python: Complete Video Course & Book
- Iterators And Generators — Hands-on Python: Complete Video Course & Book
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Forty Minutes, Thirty Items: How PCEP Is Run — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Interpreter, Compiler, And What Counts As Syntax — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Branching With If, Elif And Else — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- While, For, Range And The Loop Else — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Nested Loops, Traced By Hand — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Control Flow Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Lists: Slices, Methods And Aliases — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Tuples, And The Comma That Makes Them — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Dictionaries: Keys, Values And Items — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Collections Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Modules Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comparing, Slicing And Never Changing — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- List Comprehensions, Filtered And Nested — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Lambdas, map() And filter() — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Mock, Review And Exam Day — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep