Difference between for loop and while loop in Python

In this post, we will understand the difference between the for and while loop in Python. Both are control flow statements used for repetitive execution, but they serve different purposes based on the situation.

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 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 variable in iterable:
    # body of the for loop

Example

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

# input list
input_list = [10, 20, 30, 40, 50]
print("Input list elements:")

# traversing through all elements of the list using for loop
for element in input_list:
    # printing each element of the list
    print(element)

The output of the above code is

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:
    # body of the loop

Example

The following program prints numbers from 1 to 9 using the while 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 by 1
    i += 1

The output of the above code is

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 Without End Condition

In the following example, the loop will run infinite times

# This creates an infinite loop - do not run online
numbers = [1]
for num in numbers:
    print("TutorialsPoint")
    numbers.append(num)

This creates an infinite loop because we keep adding elements to the list while iterating over it.

While Loop Without End Condition

In the following example, the loop will run infinite times

# This creates an infinite loop - do not run online
while True:
    print("TutorialsPoint")

This creates an infinite loop because the condition True never becomes False.

Comparison

Basis of Comparison For Loop While Loop
Keyword Uses for keyword Uses while keyword
Used When number of iterations is known When number of iterations is unknown
Absence of condition Loop runs infinite times Loop runs infinite times
Nature of Initialization Once done, cannot be repeated Can be repeated at every iteration
Functions Uses range() function for numeric iterations No built-in function required
Initialization based on iteration Done at the beginning of the loop Can be done anywhere in the loop body
Generator Support Can iterate over generators directly Cannot iterate directly on generators
Speed Generally faster for known iterations Relatively slower for same tasks

Conclusion

Use for loops when you know the number of iterations or need to iterate over sequences. Use while loops when the number of iterations depends on a condition that may change during execution.

Updated on: 2026-03-26T21:28:32+05:30

53K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements