Python While Loop

Last Updated : 14 Apr 2026

Python while loop is used to repeat a block of code while a condition is True. It is helpful when we do not know how many times the loop will run.

In this chapter, you will learn about the Python while loop, its working, and how to use it in different situations.

What is Python While Loop?

The while loop executes a block of code again and again as long as a given condition remains True. It stops when the condition becomes False. If the condition never becomes False, the loop keeps running and becomes an infinite loop.

Python While Loops

In a while loop, we can write one or more statements inside the loop using proper indentation.

Syntax of while Loop

Let's see the basic syntax of the while loop in Python:

The while loop checks the given condition before running the code. If the condition is True, the loop executes the code block. After each iteration, the condition is checked again, and the loop continues until the condition becomes False.

Simple while Loop Example

Let us take a look at a simple example of a Python while loop.

Output:

0 Hello
1 Hello
2 Hello
3 Hello
4 Hello

Explanation:

In this example, the variable counter starts from 0. The while loop runs as long as the value of counter is less than 5. After each iteration, the value of counter increases by 1, and the loop stops when the condition becomes False.

Flowchart (Working) of while Loop

The image below shows the working of a while loop.

Python While Loops
  • Step 1: The program starts.
  • Step 2: The while loop checks the given condition.
  • Step 3: If the condition is True, the loop runs the code inside it.
  • Step 4: The loop variable is updated (for example, incrementing a counter).
  • Step 5: The condition is checked again.
  • Step 6: If the condition becomes False, the loop stops and the program continues.

Note: Proper indentation is required in a while loop. Incorrect indentation can cause errors or infinite loops.

More Examples of Python while Loop

In the following examples, we will see how to use the while loop in different situations to solve various problems.

Example 1: Finding Numbers Divisible by 5 or 7

In this example, we are finding numbers between 1 and 50 that are divisible by 5 or 7 using a while loop.

Output:

5 7 10 14 15 20 21 25 28 30 35 40 42 45 49

Example 2: Finding the Sum of Squares

In this example, we are calculating the sum of squares of the first 15 natural numbers using a while loop.

Output:

The sum of squares is 1240

Example 3: Checking Prime Numbers

In this example, we are checking whether numbers in a list are prime using a while loop.

Output:

34 is not a PRIME number
12 is not a PRIME number
54 is not a PRIME number
23 is a PRIME number
75 is not a PRIME number
34 is not a PRIME number
11 is a PRIME number

Example 4: Checking Armstrong Number

In this example, we are checking whether a number is an Armstrong number using a while loop.

Example

Output:

It is an Armstrong number

Example 5: Multiplication Table

In this example, we are creating a multiplication table using a while loop.

Output:

The Multiplication Table of: 21
21 x 1 = 21
21 x 2 = 42
21 x 3 = 63
21 x 4 = 84
21 x 5 = 105
21 x 6 = 126
21 x 7 = 147
21 x 8 = 168
21 x 9 = 189
21 x 10 = 210

Infinite while Loop

An infinite while loop occurs when the condition always remains True that causes the loop to run continuously without stopping. Such loops do not end on their own and require manual interruption.

Example

In the following example, we are creating an infinite loop because the condition always remains True.

Output:

Infinite Loop
Infinite Loop
Infinite Loop
Infinite Loop
......

Explanation:

In this example, the variable age is set to 28. The condition age > 19 is always True, so the loop keeps running and printing the message again and again. Since the condition never becomes False, the loop does not stop unless we manually terminate the program.

Loop Control Statements with while Loop

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

We will now look at various loop control statements used in Python's while loop.

1. Break Statement

The break statement immediately terminates the while loop, regardless of the condition.

Output:

Counter is: 0
Counter is: 1
Counter is: 2
Counter is: 3
Breaking the loop at 4

Explanation:

In the above example, the loop stops as soon as the counter reaches 4, even though the original condition was counter < 8.

2. continue Statement

The continue statement skips the current iteration and moves to the next one without executing the remaining code inside the loop.

Output:

Counter is: 1
Counter is: 2
Counter is: 3
Counter is: 5
Counter is: 6
Counter is: 7
Counter is: 8

Explanation:

In the above example, the loop skips the print statement when the counter value is 4; however, it continues executing afterward.

3. Pass Statement

The pass Statement acts as a null operator or placeholder. It is applied when a statement is required syntactically, which means that the statement will be visible in the code but will be ignored; hence, it returns nothing.

Output:

Counter is: 0
Counter is: 1
Counter is: 2
Counter is: 3
Counter is: 4
Counter is: 5
Counter is: 6
Counter is: 7

Explanation:

In this example, the pass statement ensures that Python does not throw an error while we develop the code. Since this function is empty and returns nothing due to the pass statement, there will be no output when we call this function.

4. Else with a while Loop

The else block executes after the loop finishes normally (without a break statement).

Output:

Counter is: 0
Counter is: 1
Counter is: 2
Counter is: 3
Counter is: 4
Counter is: 5
Counter is: 6
Counter is: 7
Loop completed successfully!

5. Break statement with Else-while Loop

The Break statement with the else-while loop terminates the program at that very instance when it is executed. In this case, the loop terminates immediately, and the else block is skipped.

Execute Now

Output:

Counter is: 0
Counter is: 1
Counter is: 2
Counter is: 3
Counter is: 4

Explanation:

In this example, the else block does not execute as the break statement stops the loop early.


Next TopicPython continue