UNIT 2 PPT
UNIT 2 PPT
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.
2
Prof. S. S. Ayare
Decision Control Statements
• We have the two types of Control statement in
Python:
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
if var1 > var1: #here it return the FALSE value hence the if block is not
executed
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.
#body of if
statement1
statement2
else: #body
of else
statement1
statement2 6
Prof. S. S. Ayare
if…else Statement Example
var1 = 10
var2 = 12
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.
statement1
statement1
#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)
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
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
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