Loop Control Statements in python
Continue Statement :
It returns the control to the beginning of the while loop.. The continue statement
rejects all the remaining statements in the current iteration of the loop and
moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Syntax
The syntax for a continue statement in Python is as follows −
continue
Flow Diagram :
Example:
1.
for letter in 'Apeksha':
if letter == 'h':
continue
print 'Current Letter :', letter
Output:
Current Letter : A
Current Letter : p
Current Letter : e
Current Letter : k
Current Letter : s
Current Letter : a
2.
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
Output:
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Break Statement :
The most common use for break is when some external condition is triggered
requiring a hasty exit from a loop. The break statement can be used in both
while and for loops.
If you are using nested loops, the break statement stops the execution of the
innermost loop and start executing the next line of code after the block.
Syntax
The syntax for a break statement in Python is as follows −
break
Flow Diagram :
Example:
1.
for letter in 'Apeksha':
if letter == 'h':
break
print 'Current Letter :', letter
Output:
Current Letter : A
Current Letter : p
Current Letter : e
Current Letter : k
Current Letter : s
2.
var = 10
while var > 0:
print 'Current variable value :', var
var = var -1
if var == 5:
break
Output:
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Pass Statement :
It is used when a statement is required syntactically but you do not want any
command or code to execute.
The pass statement is a null operation; nothing happens when it executes. The
pass is also useful in places where your code will eventually go, but has not
been written yet.
Syntax
pass
Example:
1.
for letter in 'Apeksha':
if letter == 'h':
pass
print 'Current Letter :', letter
Output:
Current Letter : A
Current Letter : p
Current Letter : e
Current Letter : k
Current Letter : s
Current Letter : h
Current Letter : a
2.
var = 10
while var > 0:
var = var -1
if var == 5:
pass
print 'Current variable value :', var
Output:
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0