Control statement in python
Control statement is usually used in looping and an if-else statement which alter the normal flow of the program. However, when control leaves the program all the objects which are created I completely destroyed out of the scope.
Majorly there are three types of control statement in Python:
- Break Statement
It basically terminates the current loop structure and continues the execution at the next level statement, the break statement of python is similar to the break statement has been earned in C language.
Majorly the break statement is used in the program which is made by while loop and for loop respectively.
Example:
for letter in 'Apple':
if letter == 'l':
break
print 'Current Letter :', letter
var1 = 10
while var1 > 0:
print 'Current variable value :',var1
var1 = var1 -1
if var1 == 5:
break
print "happy codding !"
2. Continue statement
If we want to skip some part of the program then the continue statement is used mostly.
Continue statement basically returns the control to the initial point of the while loop and it rejects the remaining states in the ongoing iteration of the loop, then it moves the control to the top of the loop. It is used in a while and for a loop.
Example:
for letter in 'hero':
if letter == 'h':
continue
print 'Current Letter :', letter
var = 15
while var > 0:
var = var -1
if var == 8:
continue
print 'Current variable value is :', var
print "Happy codding !"
3. Pass statement
If we don’t want to use any command to execute and want the statement to be syntactically correct then we use the pass statement. Pass statement is basically a null operation and when executed doesn’t perform anything.
Example:
for letter in 'Pyramid ':
if letter == 'a':
pass
print 'This is pass block where one letter missed '
print 'Current Letter :', letter
print "happy codding !"
