Thursday, November 10, 2016

continue vs pass in python

    Python has two different statements that can be used in loops one is 'continue' and other is 'pass' statement.Most of the beginners get confused with this two thinking that these two are having the same functionality but actually there is a slight difference in between these two statements.Below is the example for each of the statement so that you can compare and learn.

Pass Statement:
     Below is the example of pass statement
  
for letter in 'Python':
   if letter == 'h':
      pass
      print ('This is pass block')
   print ('Current Letter :', letter)

print ("Good bye pass")
Output for the above pass statement code:
   
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye pass
 
 
Continue Statement:
     Below is the example of continue statement
  
for letter in 'Python':
   if letter == 'h':
      continue
      print ('This is pass block')
   print ('Current Letter :', letter)

print ("Good bye continue")
Output for the above Continue statement code:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Good bye continue

    -If you compare both of the example statements you can see that 'h' is not printed in continue statements whereas it is available in pass statement.