Understanding while Loops: Looping Until a Condition Changes
A for loop is a definite loop that processes each item in a known collection exactly once.
Start with a Known Collection
When a program must process every item in a collection, a for loop provides a definite pattern: it processes each item in the known collection exactly once. The collection supplies the items, and the iteration variable refers to the current item during each pass through the loop.
red
blue
greenTrace Each Iteration
The loop above has three iterations because the collection has three items. On the first iteration, color holds red. On the second, it holds blue. On the third, it holds green. The iteration variable changes automatically as the loop moves from one collection item to the next.
What do you think happens?
What will this loop print, and in what order? numbers = [2, 4, 6] for number in numbers: print(number)
Reveal answer
Answer: 2, then 4, then 6
The loop processes each item in the known collection once. The iteration variable number refers to 2 during the first iteration, 4 during the second, and 6 during the third.
Read the Loop Syntax
A for loop requires the keywords for and in. The name between them is the iteration variable. The collection appears after in. A colon follows the for statement, and proper indentation identifies the statements that belong to the loop.
When reading a for loop, identify the iteration variable first, then identify the collection after in. Next, inspect the indented statements to determine what happens for each current item.
Follow the Iteration Variable
The iteration variable is not a separate counter that you update manually in this pattern. It holds the current collection item and changes automatically with each iteration. To trace a loop, write down the value it receives at each step and then apply the indented loop body to that value.
Tracing Item Names
Predict the output of this loop: animals = ["cat", "dog"] for animal in animals: print("Animal:", animal)
First iteration: The iteration variable animal holds cat. The loop body prints Animal: cat.
Second iteration: The iteration variable animal changes automatically and holds dog. The loop body prints Animal: dog.
End of collection: Both known collection items have been processed once, so this loop has completed its definite sequence.
Animal: cat Animal: dog
Choose For or While
| Loop type | Best fit | Iteration count |
|---|---|---|
| for | Processing each item in a known collection | Known from the collection |
| while | Continuing when the number of iterations is unknown | Not known in advance |
Use a for loop when the program has a known collection to process and the loop should visit each item exactly once. Use a while loop when the number of iterations is unknown. The supplied concept distinguishes these loops by when the iteration count is known, rather than by the name of the iteration variable.
Mistakes Beginners Make
Leaving out the for or in keyword.
The required for-loop syntax includes both for and in.
Fix:
for color in colors: print(color)Leaving out the colon after the for statement.
The syntax requires a colon after the for statement.
Fix:
for color in colors: print(color)Failing to indent the loop body.
Proper indentation is part of the required loop syntax and identifies the statements belonging to the loop.
Fix:
for color in colors: print(color)Assuming the iteration variable keeps one value for every pass.
The iteration variable holds the current item and changes automatically with each iteration.
Fix:
Trace the current value separately for each collection item.
Practice the Trace
Trace this loop before reading the output. Record the value of item during each iteration, then write the resulting output. items = ["pen", "book", "lamp"] for item in items: print("Found", item)
Hints
- The collection contains three items.
- The iteration variable item receives one current collection item at a time.
- Apply the indented print statement once for each item.
Found pen
Found book
Found lamp- To solve a tracing problem, count the known collection items, follow the iteration variable from the first item to the last, and apply the loop body once for each current value.
Key Takeaways
- A for loop is a definite loop that processes every item in a known collection exactly once.
- The iteration variable holds the current item and changes automatically with each iteration.
- A valid for statement uses for and in, ends with a colon, and has a properly indented loop body.
- Trace a for loop by recording the iteration variable's value at each step and applying the loop body.
- Use for when the collection and iteration count are known; while is better when the iteration count is unknown.