Skip to main content

Difference between pass, continue and break in python

pass vs continue vs break in python 

In Python, Pass, Continue and break are used to loops.

Though continue and break are similar to that of other traditional programming languages, pass is a unique feature available in python.

break   - Terminates the loop, after its invocation.
continue- Skips the current loop, and continues perform the next consecutive loops.
pass      - Does nothing. No logic to work. No break nor skipping a loop. pass is placed when there is no logic to be written for that condition, within the loop. It is advantageous for the developers for writing the logic in future.

The following example illustrates their difference:

1:  #!usr/bin/env python  
2:  print "\nExample to illustrate CONTINUE \n"  
3:  for j in range(10):  
4:    if j==5:  
5:      continue  
6:    print j,  
7:  print "\nExample to illustrate BREAK \n"  
8:  for p in range(10):  
9:    if p==5:  
10:      break  
11:    print p,  
12:  print "\nExample to illustrate PASS \n"  
13:  for i in range(10):  
14:    if i==5:  
15:      pass  
16:    print i,  

OUTPUT:

>>>

Example to illustrate CONTINUE

0 1 2 3 4 6 7 8 9
Example to illustrate BREAK

0 1 2 3 4
Example to illustrate PASS

0 1 2 3 4 5 6 7 8 9


Inference: Ex1 skipped printing 5 due to the presence of  continue.
                Ex2 stopped printing from 5 due to the presence of break.
                Ex3  printed all the values, with neither skipping a value nor stopping the loop.

Comments

Popular posts from this blog

NSG 2.1 Tcl/OTcl Script Generator

Are you a beginner for ns2 network simulator? Are you afraid of Tcl/oTcl script generation? Then there is a tcl script generator, named NSG 2.1 NSG - Network Simulation Generator NSG 2.1 is a java .jar file.  So, this application can run on all platforms (windows/linux/mac os). It deserves the java installed in you pc, prior to working with NSG 2.1. Java must be installed to run NSG2.1. So, initially, java must be installed. How to install java in Windows/ubuntu/mint/debian linux/ OS X ? Step 1 : Go to Terminal and run  java -version   to check the java version installed in your machine. Step 2 :  For Windows, click here  to download the java installer. Then, it is a typical next-next windows executable installation. For ubuntu/mint/debian linux operating systems, run the following commands in that terminal: sudo apt-get install default-jre sudo apt-get install default-jdk sudo apt-get install openjdk-7-jre sudo apt-get install...

List of Websites to get free e-books

Here follows the list of websites to download e-books for free. 1. www.bookfi.org 2. www.bookzz.org  These two websites offer 4 to 5 free downloads per day. Best advantage is that we need not go for any sign-in process or clicking on an advertisement. 3. http://manybooks.net      In this website, apart from downloading in pdf format, one can download in many other e-book formats. 4. www.ebookee.org 5. www.bookboon.com 6. www.gutenberg.org 7. www.books123.me   8. www.brupt.com This website provides file in word (.doc), powerpoint (.ppt) and pdf file formats. 9. www.pdfoxy.com This website provides books in pdf format, but we should wait for 12-15 seconds and enter a Captcha code. But no need to sign-in. 10. www.docstoc.com This is another decent website of downloading ebooks in .pdf format, with no advertisement and no signing in. 11. www.freecomputerbooks.com This is one of the best websites for downloading technical and profession...

getattrib, Setattrib, hasattrib and delattrib in python

# `getattr(object, name[, default])` Function in Python The `getattr(object, name[, default])` function returns the value of a named attribute of an object, where `name` must be a string. If the object has an attribute with the specified `name`, then the value of that attribute is returned. On the other hand, if the object does not have an attribute with `name`, then the value of `default` is returned, or `AttributeError` is raised if `default` is not provided. ```python >>> t = ('This', 'is', 'a', 'tuple') >>> t.index('is') 1 >>> getattr(t, 'index') <built-in method index of tuple object at 0x10c15e680> >>> getattr(t, 'index')('is') 1 ``` when the attribute is not defined, ```python >>> getattr(t, 'len') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'len...