UNIT: II Decision Control Statements Lectures: 08 Hrs
Decision Control Statements: Decision control statements,
Selection/conditional branching Statements: if, if-else, nested if,
if-elif-else statements.
Basic loop Structures/Iterative statements: while loop, for loop,
selecting appropriate loop. Nested loops, The break, continue, pass,
else statement used with loops.
Other data types- Tuples, Lists and Dictionary
1
Prof. S. S. Ayare
Decision Control Statements
• Decision making statements in programming languages decides the direction
of flow
of program execution.
• In our daily life we do many things which depends on some kind of conditions
for e.g.
If I study I will pass exams. If he is hungry he will eat etc.
• Sometimes in the program, we need the statement to execute under some
condition like if the value is equal to this, then this will happen and if not
equal to this then this will happen.
• This can be achieved in Python by using the Decision Control statements in
python.
2
Prof. S. S. Ayare
Decision Control Statements
• We have the two types of Control statement in
Python:
• Decision Control Statement:
If, If..else, if…elif….else.
• Flow Control Statement:
for, while break, continue
3
Prof. S. S. Ayare
IF Statement
The If the statement is similar to other languages like in Java, C, C++, etc. It is
used when we have to take some decision like the comparison between
anything or to check the presence and gives us either TRUE or FALSE.
The syntax for IF Statement is as:
if (expression):
# code to be executed under the
condition statement1
statement2
4
Prof. S. S. Ayare
if Statement Example
var1 = 10
var2 = 12
if var2 > var1: #here it return the TRUE value hence the if block is executed
print( var2,'is greater then', var1)
if var1 > var1: #here it return the FALSE value hence the if block is not
executed
print(var1,'is smaller than', var2)
print('statment after the false return in if(var1>var2)')
5
Prof. S. S. Ayare
if…else Statement
The else statement can be used with the if statement. It usually contains the
code which is to be executed at the time when the expression in the if statement
returns the FALSE. There can only be one else in the program with every single if
statement
It is optional to use the else statement with if statement it depends on your
condition.
The syntax for If….Else
Statement is as: if (expression):
#body of if
statement1
statement2
else: #body
of else
statement1
statement2 6
Prof. S. S. Ayare
if…else Statement Example
var1 = 10
var2 = 12
if var2 > var1:
print( var2,'is greater then', var1)
else: #it is not executed because the if expression is ture
print(var2,'is samller then', var1)
if var1 > var2:
print(var1,'is greater than', var2)
else: #it is executed because if expresion is false
print(var1, 'is samller then', var2)
7
Prof. S. S. Ayare
The elif Statement
The elif statement in the Python is used to check the multiple expression for
TRUE and
execute a block of code as soon as one of the conditions returns to TRUE.
elif – is a keyword used in Python in replacement of else if to place another
condition
in the program. This is called chained conditional.
Chained conditions allows than two possibilities and need more than two
branches.
The syntax for elif Statement is as:
if expression: #body to be executed in if
statement1
elif #body to be executed in elif
statement1
else #body to be executed in else
8
Prof. S. S. Ayare
The elif Statement Example
age1 = 15
#check
your age
#age1 = 12
#age1 = 19
if age1 < 12: #executed when the
age1=12 print('my age is ', age1)
elif (age1 >= 12) and (age1 < 18): #executed when the
age1=15 print('my age is ', age1)
else: #executed when the
age1=19 print('my age is ',
age1)
9
Prof. S. S. Ayare
Python Nested if Example
# In this program, we input a
number
# check if the number is positive or
# negative or zero and display
# an appropriate message
# This time we use nested if
num = float(input("Enter a number:
")) if num >= 0:
if num ==
0:
print("Zer
o") else:
print("Positive number")
else:
print("Negative number")
10
Prof. S. S. Ayare
Example: largest among three numbers
a = int(input(“Enter 1st
number:”)) b= int(input(“Enter
2nd number:”)) c=
int(input(“Enter 3rd number:”))
if (a > b) and (a > c):
print("a is
greater") elif (b <
a) and (b < c):
print(“b is
greater") else:
print(“c is greater")
11
Prof. S. S. Ayare
CONTROL STATEMENT (Looping Statement)
Program statement are executed sequentially one after another. In some
situations, a
block of code needs of times.
These are repetitive program codes, the computers have to perform to
complete tasks. The following are the loop structures available in python.
while statement
for loop statement
Nested loop statement
12
Prof. S. S. Ayare
Why we Use Loop
13
Prof. S. S. Ayare
Why we Use
Loop
14
Prof. S. S. Ayare
Why we Use
Loop
15
Prof. S. S. Ayare
For loop statement
The for loop is another repetitive control structure, and is used to execute a set of
instructions repeatedly, until the condition becomes false.
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other
iterable objects. Iterating over a sequence is called traversal.
For Loops
For loops are a pre-test loop
In order to utilize a for loop you need 3 things:
1. Needs to initialize a counter
2. Must test the counter variable
3. It must update the counter variable
Syntax of for Loop:
for val in sequence:
Body of for loop
Here, val is the variable that takes the value of the item inside the sequence on each
iteration 16
Prof. S. S. Ayare
Loop continues until we reach the last item in the sequence. The body of for
17
Prof. S. S. Ayare
The range() function
• Can generate a sequence of numbers using range() function
• range(10) will generate numbers from 0 to 9 (10 numbers)
• Can also define the start, stop and step size as range(start,stop,stepsize)
• Step size defaults to 1 if not provided.
• Does not store all the values in memory, it would be inefficient
• So it remembers the start, stop, step size and generates the next number on
the go
18
Prof. S. S. Ayare
While
Loop
19
Prof. S. S. Ayare
While loop
A while loop statementstatement
in Python programming language repeatedly
executes a
target statement as long as a given condition is true.
Syntax of
while loop:
while
expression:
statement(s)
20
Prof. S. S. Ayare
While loop
statement
21
Prof. S. S. Ayare
Nested
Loop
22
Prof. S. S. Ayare
Nested Loop
Example
23
Prof. S. S. Ayare
24
Prof. S. S. Ayare
25
Prof. S. S. Ayare