Python Breaking a While Loop With Continue

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:


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:

Python while

See online demo and code

This is how the while loop is used:


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 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 that will exit the loop:

while break

See online demo and code

This is how the break statement is used:


An example of using continue statement in while loop

If you need to exit just the current iteration of the loop rather 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. See the demo online:

while continue

See online demo and code

The code:

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 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.

while else

See online demo and code

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

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 above example, the else clause will not execute.

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

while break else

See online demo and code

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

This div height required for enabling the sticky sidebar

handtgired1947.blogspot.com

Source: https://www.jquery-az.com/python-while-loop-5-examples-with-break-continue-and-else-clause/

0 Response to "Python Breaking a While Loop With Continue"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel