Python While loop: 5 examples with break, continue, and else clause

The while loop in Python

In the for loop chapter, we learned how to use the for loop with examples. I also explained, the for loop is used when you know the number of iterations.

The while loop is used to iterate through the given code for an infinite number. You may also use for loop in that scenario, however, the while loop is designed for this.

The while loop will keep on executing the given block of code until the given condition is true. As the condition becomes false, the execution moves outside of the while loop or Python also allows using the else statement as the condition becomes false.

The else statement executes once only (see an example in the later part).

Structure of Python while loop

Following is the general structure of using the while loop:

while condition:

Write statements here to execute

 

An example of using while loop

In this simple example of using the Python while loop, a variable is declared and assigned a value. After that, a while loop is used where a condition is set to keep on executing the while loop until the value of the variable is 10. In each iteration, the variable’s value is displayed by using the print function.

This is how the while loop is used:

x = 1



while x <= 10:



print (x)



x = x+1

Output:

Python while

An example of using break statement in while loop

The break statement is used to exit the current loop (while and for loops). The scope of the break statement is only the current loop.

See the following example, where a variable x is initiated with the value of 10. The value in each iteration is incremented by 10. An if statement is used where it checks the value = 30. As it is true, the break statement will execute, which will exit the loop.

This is how the break statement is used:

x = 10



while x <= 100:

if x == 30:

break

print (x)

x = x + 10

Output:

while break

An example of using continue statement in while loop

If you need to exit just the current iteration of the loop rather than exiting the loop entirely, you may use the continue statement of Python.

To demonstrate the continue statement, an if statement is used to check the value of a variable x = 30. As it is true, the continue statement will execute that will omit the current loop. So the value of 30 will not be displayed.

The code:

x = 10

while x <= 100:

x = x + 10

if x == 30:

continue

print ("The value of variable is :", x)

Output:

while continue

You can see, the value 30 is not displayed as running the while loop.

Using else clause in while loop example

In Python, you may use the else clause with the while and for loops. Just like in if statement, the else statement in the while loop executes as the condition becomes false.

See the following example, where a message is displayed as the condition becomes false in a while loop.

This is how you may use the else statement in while loop:

x = 5



while x <= 20:



print (x)



x = x+5

else:

print ("While loop condition became false!")

Output:

while else

Note that, the else clause will not execute if you exit the loop by using the break statement. See the following example.

A demo of using else with break statement in while loop

If you exit the loop by using a break statement, as shown in the above example, the else clause will not execute.

See this example, where I just extended the above example with a break statement and added an else clause. See the output:

x = 10



while x <= 100:

if x == 30:

break

print (x)

x = x + 10

else:

print ("The condition became false!")

Output:

while break else

You can see, it only displayed 20,30 and the loop exited as the break statement executed.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!