Google News
logo
Python Program to The while loop
In the following example of Python program that demonstrates how to use the `while` loop :
Program :
# Using the while loop
count = 0

while count < 5:
    print("Count is", count)
    count += 1

print("Done.")
Output :
Count is 0
Count is 1
Count is 2
Count is 3
Count is 4
Done.
In this program, we use the `while` loop to repeat a block of code as long as a condition is true. The variable `count` is initialized to `0`. Then, we use the `while` loop to repeat the block of code as long as `count` is less than `5`.

Inside the loop, we print the current value of `count` using the `print()` function and increment the value of `count` by `1` using the `+=` operator.

When the value of `count` reaches `5`, the loop exits, and the program continues to execute the `print()` statement outside the loop, which prints the message "Done."

You can use the `while` loop to repeat a block of code as many times as needed, based on a condition.