Editor settings

General settings

Save settings

Learn to program with Python

Iterate with while loops #

A while loop in Python is a type of loop that repeats a block of code as long as a certain condition is True. It has the following syntax:

while condition:
    # code to be executed while the condition is true

Or we can use the keyword else as well. For example:

while condition:
    # code to be executed while the condition is true
else:
    # optional code to be executed when the condition is false

Here, condition is the expression that is evaluated before each iteration of the loop. If the condition is True, then the code inside the loop is executed. This process is repeated until the condition becomes False, at which point the program continues executing after the loop. The optional else block is executed after the loop finishes if the condition becomes False.

Here's an example of a while loop that uses the else block:

num = 1
while num <= 5:
    print(num)
    num += 1
else:
    print("Finished counting!")
Output:
1
2
3
4
5
Finished counting!

In this example, the condition num <= 5 is evaluated before each iteration of the loop. As long as num is less than or equal to 5, the loop continues to execute, printing the value of num and incrementing it by 1. When num becomes 6 and the condition becomes false, the program executes the else block and prints "Finished counting!".

Infinite Loop #

It's important to be careful when writing while loops to avoid creating infinite loops, which are loops that never terminate because the condition is always True. Here's an example of an infinite loop:

while True:
    print("Hello")
You can copy the above code to run on an online editor to see the results, but the system will stop working before it uses resources and causes a failure.

This while loop has a condition of True, which is always true, so the loop will continue to execute indefinitely, printing "Hello" over and over again. To stop an infinite loop, you can press Ctrl + C to interrupt the program or modify the code to include a condition that will eventually become false.


Exercise #

  • Print any message 5 times using the while loop and print() function
Output will be displayed here