Controlling Loop Repetitions- Counters and Loop Control
Loop Control Basics: What You're Actually Doing
Every loop needs a way to stop. Without control mechanisms, you'd end up with infinite loops crashing your program or missing the exact iterations you need. Loop control comes down to three things: when to start, how many times to repeat, and when to exit.
Counters are the most common way to manage repetitions. They track how many times a loop has run and let you make decisions based on that count.
Counters in For Loops
For loops in most languages automatically provide a counter through their iteration mechanism. Python gives you range() for this:
for i in range(5): runs exactly 5 times, with i going from 0 to 4.
If you need a different starting point or step size:
range(1, 10)counts from 1 to 9range(0, 20, 2)counts 0, 2, 4, 6... up to 18range(10, 0, -1)counts backwards from 10 to 1
JavaScript uses a different approach. You declare the counter yourself:
for (let i = 0; i < 10; i++) — the i++ part is your counter increment.
Counters in While Loops
While loops give you more explicit control. You manage the counter completely:
```python
count = 0
while count < 5:
print(count)
count += 1
```
This does the same thing as range(5), but now you can manipulate count however you want inside the loop.
Loop Control Statements
The break Statement
break stops the loop immediately. No more iterations happen.
```python
for i in range(100):
if i == 10:
break
print(i)
```
This prints 0 through 9, then stops. Useful when you've found what you're looking for.
The continue Statement
continue skips the rest of the current iteration and moves to the next one.
```python
for i in range(10):
if i % 2 == 0:
continue
print(i) # prints only odd numbers
```
Use this when you want to skip certain cases without exiting the entire loop.
The pass Statement (Python)
pass does absolutely nothing. It exists because Python requires something in a code block.
```python
for i in range(10):
if i == 5:
pass # TODO: handle this case later
else:
print(i)
```
It's a placeholder. That's it.
How To: Building a Controlled Loop Counter
Here's a practical pattern for tracking loop iterations with custom logic:
Step 1: Initialize your counter
max_iterations = 50
counter = 0
Step 2: Set up your while condition
while counter < max_iterations:
Step 3: Add your exit logic
if some_condition_met:
break
counter += 1
Step 4: Handle the case when max iterations are hit
if counter == max_iterations:
print("Max iterations reached without finding result")
Loop Control Comparison
| Control Method | Use When | Stops Current | Stops Loop |
|---|---|---|---|
break |
Found target, error condition | No | Yes |
continue |
Skip invalid data, skip certain cases | Yes | No |
Condition in while |
Count-based stopping | N/A | Yes |
range() bounds |
Fixed iteration count needed | N/A | Yes |
| Flag variable | Complex exit conditions | N/A | Yes |
Common Mistakes
Off-by-one errors — range(10) gives you 0-9, not 1-10. Double-check your boundaries.
Forgetting to increment — In while loops, forgetting counter += 1 creates an infinite loop. Always increment, or use a for loop instead.
Modifying loop variables in for loops — Changing i inside a Python for loop doesn't affect the iteration. The loop control ignores your changes.
Using break/continue too freely — Deeply nested breaks make code hard to follow. If you find yourself breaking from multiple levels, consider restructuring with a flag variable or extracting to a function.
When to Use Which Approach
Use range-based for loops when you know exactly how many iterations you need upfront.
Use while with explicit counter when the iteration count depends on something that happens inside the loop.
Use break when you've found your answer and don't need to keep searching.
Use continue when you want to skip specific items without stopping the entire loop.
That's loop control. Counters track iterations, break and continue let you exit or skip early. Pick the right tool for your specific situation.