Loop Control Statements (JNNC Technologies)



These statements are used to change execution from its normal sequence.
Python supports three types of loop control statements:
Python Loop Control Statements
Control StatementsDescription
Break statementIt is used to exit a while loop or a for loop. It terminates the looping & transfers execution to the statement next to the loop.
Continue statementIt causes the looping to skip the rest part of its body & start re-testing its condition.
Pass statementIt is used in Python to when a statement is required syntactically and the programmer does not want to execute any code block or command.

Break statement

Syntax:
break
Example:
#!/usr/bin/python

count = 0

while count <= 100: print (count) count += 1 if count >= 3 :
      break
Output:
0
1
2

Continue statement

Syntax:
continue
Example:
#!/usr/bin/python

for x in range(10):
   #check whether x is even
   if x % 2 == 0:
      continue
   print x
Output:
1
3
5
7
9

Pass Statement

Syntax:
pass
Example:
#!/usr/bin/python

for letter in 'TutorialsCloud': 
   if letter == 'C':
      pass
      print 'Pass block'
   print 'Current letter is:', letter
Output:

Current letter is : T
Current letter is : u
Current letter is : t
Current letter is : o
Current letter is : r
Current letter is : i
Current letter is : a
Current letter is : l
Current letter is : s
Pass block
Current letter is : C
Current letter is : l
Current letter is : o
Current letter is : u
Current letter is : d

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();