Why are Loops Better than Repetitive Code?
Compare the two programs
below:
Example
1 Using a For Loop:
n=int(input('Enter
the number to end the count: '))
count=0
for i in range(1,n):
count = count + 1
print('Long line
of text',i)
Example
2 Using Repetitive Code:
print('
Long line of text 2')
print('
Long line of text 3')
print('
Long line of text 4')
print('
Long line of text 5')
print('
Long line of text 6')
print('
Long line of text 7')
print('
Long line of text 8')
print('
Long line of text 9')
print('
Long line of text 10')
What
if the loop was set to go to 100,000 iterations or more? It would be vary
tedious to type all the needed print statements, not to mention the likelihood that
the human would skip count a few times within 100,000 lines of code. The
program output would be more reliable with the print function found in the loop
in Example 1.