Our lives resemble a loop. Waking up in the morning, performing daily rituals, heading to our respective workplaces, having lunch, returning home for dinner and rest, engaging in some entertainment if time permits, and sleeping at night. This is how our daily routine goes. In essence, we do almost the same tasks every day.

In programming language, this repetitive task is called a loop or looping. Loops are an essential element in any programming language.

There are two types of loops in Python:

  • while and
  • for

However, a loop has three main parts:

  1. Initialization
  2. Condition
  3. Increments/Decrements

Initialization means initializing some value before starting a loop’s condition, condition imposes the main criterion of the loop, and increments/decrements help to change the loop’s value to break the condition.

while loop

The literal meaning of while is ‘when.’ That means a while loop will continue as long as a specific condition is true. Let’s look at an example:

# This program is an example of while loop in python
  
i=0
 
while(i<10):
    print(i)
    i+=1

Result:

0                                                                                                                                                                                  
1                                                                                                                                                                                  
2                                                                                                                                                                                  
3                                                                                                                                                                                  
4                                                                                                                                                                                  
5                                                                                                                                                                                  
6                                                                                                                                                                                  
7                                                                                                                                                                                  
8                                                                                                                                                                                  
9

Here, the loop is initialized with i = 0, a condition is set with while i < 10, and i += 1 helps to break the condition. Initially, i is set to 0, and with each iteration, i is incremented by 1 through i += 1. Eventually, when i becomes 10, the condition becomes false, and the loop breaks.

for loop

In Python, the for loop is used to iterate over a sequence, meaning it repeats over a sequence such as a list, tuple, dictionary, etc. Let’s look at an example:

# This program is an example of for loop in python
 
for i in range(10):
    print(i)

Result:

0                                                                                                                                                                                  
1                                                                                                                                                                                  
2                                                                                                                                                                                  
3                                                                                                                                                                                  
4                                                                                                                                                                                  
5                                                                                                                                                                                  
6                                                                                                                                                                                  
7                                                                                                                                                                                  
8                                                                                                                                                                                  
9 

The result is the same as the while loop, but the approach is different. In a while loop, the loop runs based on a condition, while in a for loop, it runs over a sequence. Here, range(10) signifies numbers from 0 to 9.

Now, let’s see how to sum the numbers from 1 to 10 using both loops.

First, using the while loop:

# This program is an example to sum 1 to 10 using while loop in python
 
sum=0
i=0
while(i<11):
    sum+=i
    i+=1
print(sum)

Result:

55

Now, using the for loop:

# This program is an example to sum 1 to 10 using for loop in python
 
sum=0
for i in range(11):
    sum+=i
print(sum)

Result:

55

From the above examples, we have a clearer understanding of the difference between for and while loops and how they work.

0 0 votes
Article Rating
0
Would love your thoughts, please comment.x
()
x