Control Structure in Python-MCA275
Control Structure in Python-MCA275
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D AP P L I CAT I O N S
Control statements
Control structure is used to control the sequence of
execution of statements written in a program.
Without using Control structure sequence of execution of
statements in a program is line by line and every statement
is executed once .
In python we can divide the control structure into two parts
1. conditional control structure
2. looping/iterative control structure
Conditional control structure
1. if statement
2. if...else statement
3. if...elif...else statement
4. Nested if..else statement
Conditional Statements
if statement
Syntax
if condition:
# body of if statement
Example
Conditional Statements
If…else statement
Syntax
if condition:
# block of code if statement is True
else:
# body of code if statement is False
Example
Conditional Statements
If…elif…else statement
Syntax
if condition1:
# code block 1
elif conditon2:
# code body 2
elif conditon3:
# code body 3
else:
# code
Conditional Statements
If…elif…else statement
Syntax Example
if condition1:
# code block 1
elif conditon2:
# code body 2
elif conditon3:
# code body 3
else:
# code
Conditional Statements
Nested if..else statement
Syntax
Example
if condition1:
# code of if
#inner if statement
if conditon2:
code of inner if Neste
#inner else statement d if…
else: else
code of inner else
Break Statement
Break statement is used to force fully terminate a loop. The use of this statement
can be easily understood with the help of following example. Write a program to
demonstrate the use of break a=1 while a<=10: print(a) a=a+1 if a==5: break output:
1,2,3,4
output: 1,2,3,4
Looping Control Structure
Continue Statement
Continue statement is used to skip the execution of loop statements. The use of
this statement can be easily understood with the help of following example.
Python Continue statement is a loop control statement that forces to execute the
next iteration of the loop while skipping the rest of the code inside the loop for the
current iteration only
Write a program to demonstrate the use of continue
Consider the situation when you need to write a program that prints the number
from
1 to 10, but not 5.
a=1
while a<=10:
if a==5:
a=a+1
continue
print(a)
a=a+1
Looping Control Structure
Pass Statement
Pass statement is used in situations where there are no statements for execution
in a particular section of program but for following syntax rules the inclusion of
section is must. The use of this statement can be easily understood with the help
of following example. s = "rajneesh"
# Pass statement
Write a program to demonstrate
for i in s:
the use of pass if i == 'j':
a=1 print('Pass executed')
while a<=10: pass
if a%5==0:
pass print(i)
else:
print(a) print()
a=a+1
# Continue statement
output: 1,2,3,4,6,7,8,9
for i in s:
if i == 'j':
print('Continue executed')
continue
print(i)
SCHOOL OF ENGINEERING &TECHNOLOGY
D E PAR T M E N T O F C O M P U T E R S C I E N C E AN D E N G I N E E R I N G
THANK
YOU