These statements are used to change execution from its normal sequence.
Python supports three types of loop control statements:
Python Loop Control Statements
Control Statements | Description |
---|---|
Break statement | It 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 statement | It causes the looping to skip the rest part of its body & start re-testing its condition. |
Pass statement | It 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
If you have any doubts,please let me know