I am trying to find the formula to count every iteration of this double for
loop (in python for instance):
for i in range(5):
for j in range(5):
count = MYSTERIOUS_FORMULA
print count
Here the final value of count should be 25.
I tried count=(i+1)*j
but it produce 0,1,2,3,4,0,2,4
etc.
The Double-for-loop (a.k.a. Nested loops).
# Set count to 0 before loop starts
count = 0
for i in range(5):
for j in range(5):
# solved mysterious formula (short hand 'count = count + 1')
count += 1
# displaying count after loop
print(count)
Expanding on the formula count = count + 1
, this sets count
to be equal
itself + 1
:
count = count + 1