Repetition with For Loops

2-20-2021

In programming, many scenarios will arise where you may not want to perform a task only once. In these scenarios, manually writing out the same code over and over is time consuming and not an efficient use of time. This where loops come in, which can be used to repeat things.


Programming Without Loops


If you have been learning how to code and have not learned how to utilize loops, you may have encountered a scenario similar to this. You want to do something in your code multiple times, but just know you shouldn't be repeating the same code over and over. For example, you may want to demonstrate the growth of a number as it is multiplied by itself.


You could write:

num=4
print(num)
num*=num
print(num)
num*=num
print(num)
num*=num
print(num)
num*=num
print(num)
num*=num
print(num)

While this code definitely does work, it is time consuming, and would require a lot of work just to modify how many times you want to multiply and print the value of num.


Adding in Loops


There are multiple kinds of loops in programming, but here we will be talking specifically about the for loop. The for loop is a useful loop when you know how many times you need something repeated, because you can directly tell it how many times to repeat. For example, in Python we can right this as:


for i in range(4):
    print(i)

=>0
=>1
=>2
=>3

In this example, we are using a for loop to repeat four times. What you want repeated is then place on the line below the "for i in range(4):" and indented. You mut put a colon at the end of the first line, and the indent the following line, similar to an if statement. So in this case, the program will print the value of i four times.


But what is i? When you write a for loop in Python, you must declare some variable after the word "for". This doesn't have to be i, but i is commonly used.


for apple in range(4):
    print(apple)

for x in range(4):
    print(x)

for i in range(4):
    print(i)

It is important to know that all three of these do the same exact thing. The name you choose for the variable can be almost anythinf you want, just like a normal variable. When using range() with your for loop, the variable you create starts at a number and is incremented. In the above examples, it starts at 0, and is incremented up to, but not including 4.


for i in range(2,5):
    print(i)


=>2
=>3
=>4

In this example, i begans at 2, and is incremented up to but not including 5.


You can also modify how much is incremented each loop with a third number.


for i in range(2,10,2):
  print(i)


=>2
=>4
=>6
=>8

Our Original Example


Now here is how we can write our original example with a for loop:


num=2
for i in range(10):
    num*=num
    print(num)

And we can easily change the number of times num is multiplied and printed, simply by changing the number in range().


Conclusion


Loops are a very useful tool in programming for repetition. Try to write a program which implements a for loop along with the other concepts you have learned so far.