We now have a youtube channel. Subscribe!

A Guide to Python Break Statement



Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the Python break statement.

Python break statement is used for terminating the current loop and then resuming execution at the next statement.

The most common use for break is when some external condition is triggered needing a hasty exit from a loop. The break statement can be used in both the while and for loops.

If you are using nested loops, the Python break statement stops the execution of the innermost loop and starts executing the next line of code after the block.


Syntax

The syntax for a break statement in Python is as follows -

break

Flow Chart


Example

Following below is a very simple example -

#!/usr/bin/python

for letter in 'Python':     # First Example
   if letter == 'h':
      break
   print 'Current Letter :', letter
  
var = 10                    # Second Example
while var > 0:              
   print 'Current variable value :', var
   var = var -1
   if var == 5:
      break

print "Welcome to Web design tutorialz!"

Output

Below is the output of the above example -

Current Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Welcome to Web design tutorialz!


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Python continue statement.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain