Code 1:
iteration = 0
count = 0
while iteration < 5:
for letter in "hello, world":
count += 1
print "Iteration " + str(iteration) + "; count is: " + str(count)
iteration += 1
Code 2:
iteration = 0
while iteration < 5:
count = 0
for letter in "hello, world":
count += 1
print "Iteration " + str(iteration) + "; count is: " + str(count)
iteration += 1
Code 3:
iteration = 0
while iteration < 5:
count = 0
for letter in "hello, world":
count += 1
if iteration % 2 == 0:
break
print "Iteration " + str(iteration) + "; count is: " + str(count)
iteration += 1
For Code 1, you're continuing to add on to the count. So during the first iteration, the count becomes 12 (the length of "hello, world" is 12), and then during the second iteration, you never reset the count to 0 so count is continuously added on until it reaches 24, as it adds on the length of "hello, world" again (12 + 12 = 24).
For Code 2, count is reset to 0 each time. This means that the count will always equal 12, as the length of "hello, world" is 12.
For Code 3, you break every time the iteration is an even number. That means for iterations 0, 2, and 4, iteration returns a value of 1 as 1 is added to iteration at the beginning of the for loop. But during odd iterations, the count is 12, since the program does not break out of the for loop and adds the length of "hello, world", which is 12.