Iteration is the repetition of a block of code, and one iteration is one complete run through that block. A program that adds up a list of prices does not contain a separate line for every price. It contains one addition, executed once per price, with each pass starting from the total the previous pass left behind.
The loop and the iteration are two different things, and confusing them is where most miscounts start. The loop is the construct written once in the source code. An iteration is what happens each time the processor runs that construct, and a single loop can therefore produce four iterations, or four million, depending on the values it is fed.
One iteration is one full pass through the loop body
The statements inside a loop are called its body. One iteration begins when control enters the body and ends when the last statement in the body has run and control returns to the top. Everything the body changes stays changed. Variables keep their new values into the next pass, which is what makes each pass build on the one before it.
total = 0
for value in [1, 2, 3, 4]:
total = total + value
print(total) # 10
Four passes run here. The table follows total through each one.
| Pass | Value added | Total after the pass |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
Two facts in that table carry the whole concept. First, the count of iterations equals the count of items, four. Second, the state moves: total enters pass three holding 3 and leaves it holding 6, because pass three starts from where pass two ended.
Is a loop the same thing as an iteration?
No. The loop is written once and sits still in the source file. Iterations happen only when the program runs, and their number is set by the data. Feed the loop above a list of a million prices, and the same few lines produce a million iterations.
At the hardware level, the end of a pass is a jump. The processor’s program counter normally steps to the next instruction. At the end of a pass it is sent back to the loop’s first instruction, and the body runs again from the top, because the CPU’s fetch-execute cycle is the machinery that performs that jump on every pass. Those four instructions, add, inc, cmp and jle, are assembly language, read line by line in the assembly guide.

Count-controlled iteration: the for loop settles the number in advance
A for loop is the form to reach for when the number of passes is known before the loop starts. The same form fits when the passes simply walk a collection from its first item to its last. Programmers call this definite iteration. A loop over a sequence runs as many times as the sequence has items. A 12-track playlist gets 12 passes. A 3-letter word gets 3.
Python’s range states the count directly. range(1, 101) hands the loop the values 1 through 100, so the body runs 100 times, and a reader can verify that count from the header alone, before a single pass executes. The Python documentation defines the for statement as iterating over the items of a sequence in order (Python language reference, the for statement). The count in this form is settled before execution begins.
To see the count your own loop plans, print the loop variable on each pass. The values that appear are the passes themselves. The first value printed opens pass one, and the last value printed is the final pass. The number of lines on screen equals the number of iterations.
Condition-controlled iteration: the while loop checks before every pass
A while loop knows no count in advance. It carries a condition, a yes-or-no test, and it evaluates that test before every pass, including the first one. The body runs while the test passes; the pass that would follow a failed test never starts. Teachers call this indefinite iteration, because the number of passes is settled by the data the loop meets along the way.
The condition resolves to a single true-or-false value on every check. Comparisons such as n > 1 are evaluated by the processor’s arithmetic circuitry, built from the same logic gates that sit under every yes-or-no decision a computer makes. Its one bit of outcome decides whether another pass happens.
Watch the count shrink in a halving loop. Start at 1,000,000 and halve the value, rounding down, until it reaches 1. The value runs 500,000, 250,000, 125,000, then 62,500 and on downward, and the loop arrives at 1 after 19 passes. The pass count follows the base-2 logarithm of the starting value, rounded down, which comes to 19 here. That is why halving loops stay short even on large inputs.
Can a while loop run zero times?
Yes. The test comes first, so a loop whose condition fails at the first check executes its body zero times and the program moves on. The test-first rule has a for-loop twin: range(0) produces no values, and a loop over it performs no iterations at all.
One million iterations, counted as they ran
For this test run, small Python 3.12.3 programs counted their own passes while they worked, and a timer recorded how long each loop took.
The first program adds the numbers from 1 to 1,000,000 in a for loop, incrementing a counter inside the body on every pass. When it finished, the counter read exactly 1,000,000 and the total read 500,000,500,000. The formula n(n + 1)/2, evaluated on its own, returns 500,000,500,000 as well, so the loop and the arithmetic agree to the last digit.
count = 0
total = 0
for i in range(1, 1000001):
total = total + i
count = count + 1
The two routes differ sharply in cost. The formula took about 0.002 milliseconds. The loop took a median of 49 milliseconds across five runs with the counter line removed, rising to 69 milliseconds with the counter left in, so the counting itself added roughly 40 percent to the run. That gap surprised me more than the totals did. An iteration counter feels free when you write it, and at a million passes it carries a real cost.
A while loop version of that job recorded 1,000,001 condition evaluations for its 1,000,000 passes. The extra evaluation is the failing check that ends the loop. A condition is always tested one more time than the body runs. One expectation of mine failed during these runs. I had predicted 20 passes for the halving loop, and the counter returned 19, because 2^20 is 1,048,576, which overshoots 1,000,000. Times also moved between runs, from 97 to 170 milliseconds for the same counted loop on the same machine, while every count came back identical in every run. Treat the times as machine-dependent and the counts as the durable result.
Nested loops: the counts multiply
Place one loop inside another and the inner body runs once for every pass of the outer loop, on every outer pass. A grid scan with 1,000 rows and 1,000 columns is the standard shape. The outer loop takes one row, the inner loop walks all 1,000 columns in that row, and the cell inspection at the center executes 1,000 × 1,000 times.
The run confirms the arithmetic exactly. A nested counter with 1,000 outer passes and 1,000 inner passes recorded 1,000 outer iterations and 1,000,000 inner iterations, in 84 milliseconds. Doubling both loops to 2,000 grows the inner count fourfold, to 4,000,000, because both factors in the multiplication grew.

This multiplication is why compilers work on loops so hard. One optimization, unrolling, copies the body several times and cuts the pass count. It trades a larger program for fewer jumps back to the top. How a compiler works walks through the stages where that trade is made.
When iteration runs too long, and when it stops early
A while loop ends only when its body moves the condition to false. Leave out the update, or update in the wrong direction, and no pass brings the end any closer. The loop runs at full speed until something outside it intervenes. Full speed is fast. A tight counter loop that also read the clock on every pass completed 5,612,983 passes in one second, about 178 nanoseconds per pass, sustained across the whole second the loop was allowed to run. A runaway loop therefore does not idle. It burns a processor core at its maximum rate from the first second.
Some loops are meant to run without end. A web server’s accept loop and a traffic light controller both iterate as their normal state. They stop only when the process is shut down. The opposite tool is break, which ends the loop in the middle of the current pass. A search that finds its target at the third item of a million-item list has spent 3 passes when break fires. The remaining 999,997 passes never happen.
Iteration and recursion: one repetition, two mechanisms
Recursion reaches the same repetition from the other side. A function that must repeat a step calls itself with a smaller input. The repetition ends when the input is small enough to answer directly. Factorial shows the shape. Five factorial is 5 × 4 × 3 × 2 × 1. Computed recursively, it asks for four factorial first. Computed iteratively, a loop multiplies an accumulator by 2, 3, 4 and 5 in turn.
The machinery differs, and the difference has a hard edge. Every recursive call pushes a frame onto the call stack, holding that call’s variables until the call returns, while a loop reuses the same few variables on every pass. Python caps that stack growth at a default recursion limit of 1,000. The test run hit the cap head-on: factorial of 5,000 written recursively stopped with a RecursionError. Written as a loop, the job ran to completion. It produced a result 16,326 digits long.
Given a ceiling like that, I reach for the loop first whenever the repetition is a simple walk. I keep recursion for problems that branch, such as walking a folder tree. Whichever form you choose, run one check before you trust it. State the value the loop starts from, the value or condition that stops it, and the step that moves between them. If any of the three cannot be stated, the loop’s behavior is still a guess, and the first surprising input will find the gap.
Frequently asked questions about iteration
What is an iterator in programming?
An iterator is an object that hands a loop one item at a time, remembering where it stopped between requests. In Python, calling iter() on a list returns such an object, and each call to next() on it yields the following item, until the items run out and it raises StopIteration. A for loop over a list is this process with the bookkeeping hidden.
What does iteration mean in mathematics?
In mathematics, iteration means applying a function to its own output, again and again: start with a value, apply the rule, and feed the result back in as the next input. Approximating a square root works this way, where each pass through the averaging rule returns a value closer to the true root than the pass before it.
Why does my loop run one time too many, or one time too few?
The usual cause is an endpoint error, called an off-by-one error. Python’s range excludes its end value, so range(1, 10) supplies the nine values from 1 to 9, and a programmer who expected ten passes gets nine. The mirror mistake adds a pass: range(0, 10) supplies ten values because it starts at 0. Checking the first and last values the header produces settles which side the error sits on.
How do you stop a loop that never ends?
From outside the program, interrupt it: in a terminal, Ctrl+C raises a KeyboardInterrupt in Python and ends the run. From inside the code, give the loop an exit it can reach, by making the body update the variable the condition tests, or by adding a break tied to a condition that will occur, such as a maximum pass count.
Does the loop variable keep its last value after the loop ends?
In Python, yes: after for i in range(10), the variable i still holds 9, the final value it was given. In C, the equivalent loop, for (int i = 0; i < 10; i++), leaves i holding 10, because the counter itself is the value the condition finally rejected. Code that reads the counter after the loop therefore behaves differently across the two languages.
What is an iteration in Agile project work?
In Agile methods, an iteration is a fixed block of working time, usually one to four weeks, in which a team plans, builds and reviews a small piece of a product; Scrum teams call the same block a sprint. The word keeps its core sense of a repeated cycle, but nothing in it counts executions of code.

Kaleem
Kaleem is a Computer Science graduate (Virtual University of Pakistan) and the founder and editor of Valley AI, where he publishes in-depth, tested guides on computer hardware, artificial intelligence, and how the internet works. Every guide is researched from primary sources, tested hands-on where possible, and updated when the facts change.