Skip to content
BENIBIX Learn

For Loop

Development🧩 Programming ConceptsControl Flow

Also known as for

A loop that runs once for each item in a list, then stops by itself.

Think of it like

Think of it like a teacher reading the names in class. Each name is called once, in order.

Example

A class app runs a for loop over 28 names. It sends each pupil the same reminder about the school trip.

Why it matters

Most loops go over a list of things. A for loop does that and can never forget to stop.

Definition

A for loop runs its lines once for each item in a list or a range. Each time round, a variable holds the next item. When the items run out, the loop ends by itself. Python writes it as for name in names. Other languages, like C and JavaScript, often count with a number instead.

Example code

python
for name in ["Emma", "Jack"]:
    print("Hi", name)
# Hi Emma
# Hi Jack
Two names, so the loop runs twice and then stops.

Common mistake

Changing the list while looping over it. Items get skipped or seen twice.

Key takeaways

  1. 1
    Once per item. Each item gets exactly one pass.
  2. 2
    Ends by itself. No items left means no more passes.
  3. 3
    A name for the item. A variable holds the current one.

Compare

Learn it properly