Difference between for loop and while loop in Python

In this post, we will understand the difference between the ‘for’ and the ‘while’ loop.

For Loop

A for loop is a control flow statement that executes code for a predefined number of iterations. The keyword used in this control flow statement is “for”. When the number of iterations is already known, the for loop is used.

The for loop is divided into two parts −

Header − This part specifies the iteration of the loop. In the header part, the loop variable is also declared, which tells the body which iteration is being executed.

Body − The body part contains the statement executed per iteration.

• The initialization, condition checking, and iteration statements are written at the beginning of the loop.

• It is used only when the number of iterations is known beforehand.

• If the condition is not mentioned in the 'for' loop, then the loop iterates the infinite number of times.

• The initialization is done only once, and it is never repeated.

• The iteration statement is written at the beginning.

• Hence, it executes once all statements in the loop have been executed.

Syntax

for(initialization; condition; iteration){
//body of the 'for' loop
}


Example

The following program prints all the list elements using the for loop −

# input list
inputList = [10, 20, 30, 40, 50]
print("Input list elements:")
# traversing through all elements of the list using for loop
for element in inputList:
# printing each element of the list
print(element)


Output

On executing, the above program will generate the following output −

Input list elements:
10
20
30
40
50


While Loop

A loop that runs a single statement or a set of statements for a given true condition. This loop is represented by the keyword "while." When the number of iterations is unknown, a "while" loop is used. The statement is repeated until the boolean value is false. Because the condition is tested at the beginning of a while loop, it is also known as the pre-test loop.

• The initialization and condition checking are done at the beginning of the loop.

• It is used only when the number of iterations isn’t known.

• If the condition is not mentioned in the 'while' loop, it results in a compilation error.

• If the initialization is done when the condition is being checked, then initialization occurs every time the loop is iterated through.

• The iteration statement can be written within any point inside the loop.

Syntax

while ( condition) {
statements;
//body of the loop
}


Example

The following program prints all the list elements using the for loop −

# Initializing a dummy variable with 1
i = 1
# Iterate until the given condition is true
while i < 10:
# printing the current value of the above variable
print(i)
# incrementing the value of i value by 1
i += 1


Output

On executing, the above program will generate the following output −

1
2
3
4
5
6
7
8
9


When Should You Use For and While Loop?

The for loop is used when we know the number of iterations, that is, how many times a statement must be executed. That is why, when we initialize the for loop, we must define the ending point.

A while loop is used when the number of iterations is unknown. It is used when we need to end the loop on a condition other than the number of repetitions. It is not necessary to know the condition ahead of time in this case. That is why we can use a boolean expression in the loop's initialization.

In the Absence of Condition

If no condition is specified in the for and while loop, the loop will iterate infinitely.

In the absence of a condition, the following is the difference between a for loop and a while loop −

For Loop − In the following example, the loop will run infinite times.

Example

l = [1]
for m in l:
print("TutorialsPoint")
l.append(m)


Output

On executing, the above program will generate the following output −

TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
.
.
.
.
runs infinite times


We started with a list and initialized it with a single random value. Then, using the for loop and in operator, we traversed through the elements of the list. Inside the loop, it will print some random text, and then we added another element to the list, so the for loop gets executed again because of the new element. In this way the loop gets executed infinite times.

While loop − In the following example, the loop will run infinite times.

Example

while True:
print("TutorialsPoint")


Output

On executing, the above program will generate the following output −

TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
TutorialsPoint
.
.
.
.
runs infinite times


Differentiation Table

Basis of Comparison For Loop While Loop
Keyword Uses for keyword Uses while keyword
Used For loop is used when the number of iterations is already known. While loop is used when the number of iterations is already Unknown.
absence of condition The loop runs infinite times in the absence of condition Returns the compile time error in the absence of condition
Nature of Initialization Once done, it cannot be repeated In the while loop, it can be repeated at every iteration.
Functions To iterate, the range or xrange function is used. There is no such function in the while loop.
Initialization based on iteration To be done at the beginning of the loop. In the while loop, it is possible to do this anywhere in the loop body.
Generator Support Python's for loop can iterate over generators. While loops cannot be directly iterated on Generators.
Speed The for loop is faster than the while loop. While loop is relatively slower as compared to for loop.

Conclusion

In this article, we learned about the differences between the for and while loops, as well as how the while and for loops work through examples.