Home Python How to Break “While” Loop in Python?

How to Break “While” Loop in Python?

0
How to Break “While” Loop in Python?

Loop is a programming structure that implements iteration. A loop is used when you want to repeat a specific block of code many times until you met the given conditions. Similarly, a while loop executes its statement many times as long as the given conditions give ‘true’.

The Python while loop starts with a ‘while’ which is followed by the conditional expression. If the expression is true according to the conditions, the statement will execute it then the expression is again checked and it will execute it until the condition is true.

And if the expression is false then the loop terminates and moves back to the first statement beyond the body of the loop.

How to Break “While” Loop in Python?

while EXPRESSION:

Statement(S)

In this example, n is initially 5 and the expression in the while statement header on line 2 is n > 0, which is true, so the loop body executes. Inside the loop body on line 3, n is decremented by 1 to 4 and then printed.

When the body of the loop ends, program execution returns to the top of the loop at line 2, and the expression is evaluated again. It is still true, so the body executes again, and 3 is printed. This continues until n goes to 0.

When the expression is tested and it is false, the loop terminates. The execution would resume at the first statement following the loop body.


n = 5

while n > 0:

n -= 1

print(n)

Output:

4

3

2

1

0

Break and Continue Statement

The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body.

The Python continue statement immediately terminates the current loop iteration. Execution jumps to the top of the loop, and the controlling expression is re-evaluated to determine whether the loop will execute again or terminate.


n = 5

while n > 0:

n -= 1

if n == 2:

break

print(n)

print('Loop ended.')

output:

4

3

Loop ended

Break “While” Loop in Python

In break.py where n becomes 2, the break statement is executed. The loop is terminated completely as the program execution jumps to the print() statement.

In the continue.py there is a continue statement in place of the break like:


n = 5

while n > 0:

n -= 1

if n == 2:

continue

print(n)

print('Loop ended.')

output:

4

3

1

0

Loop ended

Here, when n is 2, the continue statement causes termination of iteration. Thus, 2 do not get printed. Execution returns to the top of the loop and the condition is re-evaluated, and it is still true. The loop resumes and causes termination when n becomes 0.

The Else Clause

The else clause at the end of a while loop is only present in Python. It is written under the while statement as,

whileexpr:

statement(s)

else:

additional_statement(s)

When we do not use the else clause, additional_statement(s) will be executed after the while loop terminates. When additional_statement(s) are placed in an else clause, they will be executed only if the loop terminates by exhaustion which means the loop iterates until the controlling condition becomes false. For example,


n = 5

while n > 0:

n -= 1

print(n)

else:

print('Loop done.')

Output:

4

3

2

1

0

Loop has done.

In this case, the loop repeated until the condition was exhausted: n became 0, so n > 0 became false. Because the loop lived out its natural life, so to speak, the else clause was executed.


n = 5

while n > 0:

n -= 1

print(n)

if n == 2:

break

else:

print('Loop done.')

Output:

4

3

2

This loop is terminated prematurely with a break, so the else clause isn’t executed.

Read more: What is Fix “Invalid Character Identifier” in Python Programming?

LEAVE A REPLY

Please enter your comment!
Please enter your name here