Thursday, June 18, 2020

A Guide to Python Break Statement



Hello guys! Welcome back to another section of my 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 as well resuming execution at the next statement.

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

If you are making use of nested loops, the 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

The 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 studying 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.
Share:

0 comments:

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.