Next →

What is a While Loop?

A while loop repeats a block of code as long as a condition is true.

Example:

count = 0
while count < 5:    
	print(count)    
	count = count + 1

This loop prints numbers 0 to 4.

Why Use While Loops?

While loops are useful when you don’t know beforehand how many times you need to repeat an action — the loop continues until a condition changes.