top of page

Python Control Flow: while Loop

Updated: Aug 6, 2025

The while loop is an excellent feature of Python, allowing you to perform repetitive tasks until a condition remains true.


In the last post, we learned and saw examples of the for loop. In this post, we will learn how to perform repetitive work in Python using the while statement.


Continuous loop
Continuous Loop

The while keyword is used to do repetitive things in Python. while checks a condition, and if it’s true, does the said thing. If the condition becomes false, then the loop terminates. Let us see its syntax!


#1: Python while Statement


The syntax is:

while condition:
	do something

The while keyword is followed by the condition to be checked each time, then a colon (:). The code to be repeated must be indented.


Let us see an example!

i = 0
while i < 2:
	print("Hello")
	i += 1

In the above example, we are initialising i as a control variable whose initial value is 0.

Python checks the condition i < 2, which is true in this case as 0 < 2; thus, it executes the indented lines of code—that is, it prints "Hello" and increments the value of i by 1. The new value of i is 1.


Python again checks the condition i < 2, which is true as well since 1 < 2, and again prints "Hello" and increments i by 1 to 2.


It again checks the condition i < 2, which is now false, as 2 < 2 is not correct. Thus, the condition fails, and the indented code (print as well as increment) does not execute, and Python comes out of the while loop.


Care: The condition must be carefully crafted, because if the condition never becomes false, the loop repeats endlessly and becomes an infinite loop.

#2: Python while True Statement


while True is used to create an infinite loop in Python. The loop continues as long as the condition True is true—which it always is. We can break out of this loop using the break keyword or continue it using the continue keyword.


Syntax:

while True:
	do something
	if condition:
		continue
	else:
		break

Example: Taking a positive number from the user


We will continuously check whether the user inputs a positive number by using an if statement and the condition number < 0. As long as the user gives a negative number, the condition is true, and the loop continues. The loop breaks if the user finally gives a positive number, say 2, which fails the condition number < 0 as 2 < 0 is incorrect. Python then comes out of the while True loop.


# Validating user input
while True:  # This True condition
	number = int(input("Enter a positive number: "))
	if number < 0:
		continue
	else:
		break

#3: break and continue Statements


The break keyword is used to immediately break an ongoing loop and come out of it. The continue statement is used to skip the rest of the current loop iteration and continue with the next one.


Both depend on a condition to be checked; as long as the condition remains true, we can use break and continue.


Syntax:

if condition:
	break
else:
	continue

or

if condition:
	continue
else:
	break

Example: 4×4 Pyramid using break and continue

rows = 4
i = 0

# Infinite loop
while True: 
	i += 1

	# This condition will never be True
	if i == 0: 
		continue
	
	# Prints spaces and stars
	print(" " * (rows - i) + "*" * i)

	# Stop
	if i >= rows:
		break

How it works:

  1. We start an infinite loop with while True.

  2. We increment i each time before printing.

  3. The continue statement is shown as an example—it could be used to skip unwanted iterations. Here, it’s set so it never actually skips the visible pyramid rows, but it demonstrates placement.

  4. After printing, we check if i >= rows. If so, break ends the loop.



We just learned about while and while True statements in Python for repeating tasks. We learned about break and continue. Such statements are quite useful in taking input from the user continuously, like if we make a chatbot or program that continuously asks questions from the user. If the user answers, the cycle repeats, and the program asks another question. We can make use of a while True loop for this example. This makes it interactive.


In the next post, we will learn about exceptions, or try statements, for checking errors.

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Most Read

AI Nine O Subscription

Subscribe to AI Nine O for 90-second learning.

Your question...

Venus will reply here...

VenusMoon Logo

Promoting equitable education for all through the fruitful use of AI.

© 2024-25 by VenusMoon Education | Udyam Registration Number: UDYAM-MP-10-0030480

bottom of page