While Loops
Published by
sanya sanya
Loops are an essential concept in programming that allow you to execute a set of instructions repeatedly. Python consists 2 types of loops namely,
- For Loop
- While Loop
While loops are useful for executing a block of code repeatedly as long as a certain condition is true. In this blog, we will take a closer look at Python while loops and how to use them effectively. A basic difference between both the loops is until a condition is no longer satisfied, a while loop repeatedly runs a block of code. Contrarily, for loops repeatedly run a block of code a predetermined number of times.
Basic Syntax of While Loop:
The basic syntax of a while loop in Python is as follows:
while condition: statement(s)
Here,
- A while loop assesses the circumstance.
- The while loop's function is run if the condition evaluates to True.
- condition is assessed once more.
- Until the condition is false, this process is repeated.
- The loop terminates when the condition evaluates to False.
FlowChart of While Loop

Example 1: Printing numbers with a while loop
Let's start with a simple example of using a while loop to print numbers from 1 to 5. Here's the code:
# program to display numbers from 1 to 5 # initialize the variable num = 1 n = 5 # while loop from i = 1 to 5 while num <= n: print(num) num = num + 1
In this example, we start with the variable num equal to 1. The condition num <= 5 is True, so the code block inside the loop is executed, which prints the value of num and increments it by 1. This process repeats until the condition becomes False (when num is equal to 6), and the loop exits.
Output:
1
2
3
4
5
Below is the explanation how it worked,
| Variable | Condition: num <= n | Output |
| num=1 n=5 | True | 1 is printed & num=2 |
| num=2 n=5 | True | 2 is printed & num=3 |
| num=3 n=5 | True | 3 is printed & num=4 |
| num=4 n=5 | True | 4 is printed & num=5 |
| num=5 n=5 | True | 5 is printed & num=6 |
| num=6 n=5 | Fasle | Loop is terminated |
Example 2: Calculating factorial with a while loop
Another common use of while loops is to iterate over a range of values to perform a certain computation. Let's look at an example of using a while loop to calculate the factorial of a number. The factorial of a positive integer n is the product of all positive integers from 1 to n. Here's the code:
n = 5 fact = 1 while n > 0: fact *= n n -= 1 print(fact)
In this example, we start with the variable n equal to 5 and the variable fact equal to 1. Inside the loop, we multiply fact by n and decrement n by 1. This process repeats until n is equal to 0, at which point the loop exits and the factorial of 5 is printed.
| Variables | Condition | Action |
| n=5 fact=5 | True | n=4 |
| n=4 fact=20 | True | n=3 |
| n=3 fact=60 | True | n=2 |
| n=2 fact=120 | True | n=1 |
| n=1 fact=120 | True | n=0 |
| n=0 fact=120 | False | Loop is terminated |
Nested While Loop
A nested while loop in Python is a loop within another loop. The outer loop executes first, and each time it runs, the inner loop will execute completely. The inner loop will run to completion for every iteration of the outer loop.
For instance, consider a scenario where a teacher wants to create a multiplication table for the numbers 1 and 2. A nested while loop can be used to generate this table as follow. In this case, the outer loop would iterate through the multiplicand values, while the inner loop would iterate through the multiplier values.
i = 1 while i <= 2: j = 1 while j <= 10: result = i * j print(i, "*", j, "=", result) j += 1 i += 1
In this example, the outer loop iterates through the multiplicand values (1 to 2), while the inner loop iterates through the multiplier values (1 to 10) for each multiplicand value. The program generates a multiplication table for the numbers 1 to 2, giving the teacher a quick and easy way to check their students' multiplication skills.
Output
1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
1 * 7 = 7
1 * 8 = 8
1 * 9 = 9
1 * 10 = 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
Infinite Loops
It's important to be careful when using while loops, as they can lead to infinite loops. An infinite loop is a loop that never exists. This can happen if the condition in the while loop is always true.
For example, the following code creates an infinite loop:
while True: print("Hello, world!")
In this case, the condition is always True, so the loop will continue to execute indefinitely, printing "Hello, world!" over and over again.
To avoid infinite loops, make sure that the condition in the while loop will eventually become false.
Difference between for loop and while loop
-
Iteration over a sequence: A for loop is used to iterate over a sequence of items, such as a list or a tuple, and perform a certain operation on each item. A while loop is used to perform an operation repeatedly as long as a certain condition is true.
-
Number of iterations: With a for loop, the number of iterations is determined by the length of the sequence being iterated over. With a while loop, the number of iterations is determined by the condition being checked.
-
Initialization and increment: A for loop doesn't require any explicit initialization or increment steps as these are handled automatically by the loop structure. In contrast, a while loop requires explicit initialization and increment steps.
-
Range of use: For loops are typically used when you know the exact number of times you need to iterate over a sequence, while while loops are typically used when the number of iterations is not known beforehand or when you need to repeat an operation until a certain condition is met.
-
Complexity of loop body: For loops are generally used for simple operations that need to be performed on each item in a sequence, such as printing out a list of items or calculating a sum of values. While loops are generally used for more complex operations, such as implementing game logic or waiting for an event to occur. This is because the loop body in a while loop can be more complex and involve more conditional logic.
-
Code readability: For loops are generally easier to read and understand than while loops because the loop structure is more explicit. While loops can be more complex and harder to read, especially if the condition being checked is complicated.
Library
WEB DEVELOPMENT
FAANG QUESTIONS

