How to exit from a loop in Python

In this tutorial, we will learn how to exit from a loop in Python with three different statements. We can easily terminate a loop in Python using these below statements

  • break
  • continue
  • pass

Terminate or exit from a loop in Python

A loop is a sequence of instructions that iterates based on specified boundaries. Loops are used when a set of instructions have to be repeated based on a condition. Loops are terminated when the conditions are not met.

But there are other ways to terminate a loop known as loop control statements. Let’s look at them in detail in this tutorial.

Using Break Statement

When break statement is encountered in the loop, the iteration of the current loop is terminated and next instructions are executed. In other words, when break is encountered the loop is terminated immediately.

Syntax:

break

Example of break statement:

for letter in 'CodeSpeedy':     
   if letter == 'S':
      break
   print ('Current Letter :', letter)
print('Loop terminated with the letter :',letter)

Output:




Current Letter : C
Current Letter : o
Current Letter : d
Current Letter : e
Loop terminated with the letter : S

In the above code, the alphabets are printed until an ‘S’ is encountered. After ‘S’ is encountered the loop is broke completely and the next statement after the for loop is executed which is “print(‘Loop terminated with the letter :’,letter)”.
When a for loop is terminated by break, the loop control target keeps the current value.
For if-else condition, break statement terminates the nearest enclosing loop by skipping the optional else clause(if it has).

Using Continue Statement

When continue statement is encountered, current iteration of the code is skipped inside the loop. Here, unlike break, the loop does not terminate but continues with the next iteration.

Syntax:

continue

Example of continue Statement:

for letter in 'CodeSpeedy': 
    if letter == 'e': 
        continue 
    print ('Current Letter :', letter)
print('Loop terminated with the letter :',letter)

Output:

Current Letter : C
Current Letter : o
Current Letter : d
Current Letter : S
Current Letter : p
Current Letter : d
Current Letter : y
Loop terminated with the letter : y

Let’s consider the previous example with a small change i.e. break is replaced with continue.  The for loop skips ‘e’ every time it’s encountered but does not terminate the loop. Hence, all the letters are printed except for ‘e’.

Using Pass Statement

In Python Programming, pass is a null statement. Unlike comment, interpreter does not ignore pass. Hence, pass statement can be used to write empty loops or can be used when a statement is required syntactically but you do not want any command or code to execute.

Syntax:

pass

Example of pass statement:

for letter in 'CodeSpeedy': 
    if letter == 'e': 
        pass
        print('This is pass block')
    print ('Current Letter :', letter)
print('Loop terminated with the letter :',letter)

Output:

Current Letter : C
Current Letter : o
Current Letter : d
Current Letter : e
This is pass block
Current Letter : S
Current Letter : e
This is pass block
Current Letter : e
This is pass block
Current Letter : p
Current Letter : d
Current Letter : y
Loop terminated with the letter : y

Here, we considered the above example with a small change i.e. continue is replaced with pass and a print statement.
The preceding code does not execute any statement or code if the value of letter is ‘e’. The pass statement is helpful when a block of code is created but it’s no longer required.

Conclusion

The break, continue and pass statements in Python will allow one to use for and while loops more efficiently.
Python also supports to have an else statement associated with loop statements.
In the above-mentioned examples, for loop is used. With the while loop also it works the same.

Also learn:


One response to “How to exit from a loop in Python”

  1. Balaramkrish says:

    Great page thanks for helping me out with this I don’t know what I would have done

Leave a Reply

Your email address will not be published. Required fields are marked *