Conditional Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1

CONDITIONAL STATEMENTS
and
CONTROL STATEMENTS
CONDITIONAL STATEMENTS
2
The basic decision statements in computer
is selection structure.
The decision is described to computer as a
conditional statement that can be answered
True or False.
Python language provide the following
conditional (Decision making) statements.
 if statement
 if...else statement
 if...elif...else staement
 Nested if..else statement
The if statement
3
The if statement is a decision making
statement. It is used to control the flow of
execution of the statements and also used
to test logically whether the condition is true
or false.
Syntax
if test
expression:
statement(s
)
Example program 4
i=int(input(“Enter the
number:”)) If (i<=10):
print(“ condition is
true”)

OUTPUT
Enter the
number: 9
Condition is true
If … else statement
5
The if…else statement is called alternative
execution, in which there are two possibilities
and the condition determines wich one gets
executed.
Syntax

if test expression:
Body of if
else:
Body of else
Write a program to check if a
number is Odd or Even 6
if (num % 2)== 0:
print (“Given number is
Even”) else:
print(“ Given number is
Odd”)
num = int(input(“Enter the
OUTPUT
number:”))
Enter the number: 9
Given number is
Odd
Elif-Statements
7
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
SYNT and need more than two
branches. if expression:
AX Body of if
elif
expressio
n:
Body of
elif else:
Figure – elif condition
Flowchart 8
Example: largest among three
numbers 9
a = int(input(“Enter 1st
number:”)) b= int(input(“Enter
2nd number:”)) c=
int(input(“Enter 3rd number:”)) OUTPUT
Enter 1st
if (a > b) and (a > c):
number:10 Enter
print("a is
2nd number:25
greater") elif (b < a) Enter 3rd
and (b < c): number:15 B is
greater
print(“b is greater")
else:
print(“c is greater")
Nested if … Statements 10

We can write an entire if… else statement in


another if… else statement called nesting, and
the statement is called nested if.

 In a nested if construct, you can have an if


… elif
… else construct inside an if … elif.. Else
construct.
11
• Example program 12
n = int(input(“Enter
number:”)
) ifIf (n
(n<=15):
== 10): OUTPUT
print(‘play Enter number :
10
cricket’) else: Play
print(‘play kabadi’) cricket

Else:
print(‘Don’t play
game’)
CONTROL STATEMENT (Looping
Statement) 13
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 staement
While loop statement 14
A while loop statement 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
)
Using else statement with while loops
15
 Python supports t have an else statement
associated with a loop statement.
 If the else statement is used with a while loop,
the else statement is executed when the
condition false.
Program to illustrate the else in while loop
while counter
counter = 0 < 3: OUTPUT
Inside loop
print("Inside loop")
Inside loop
counter = counter + Inside loop
1 Outside
loop
else:
print(“Outside
For loop statement
16
 The for loop another repetitive
is 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 val in sequence:


. Body of for
Syntax loop
For loop flow
chart Addition of number using for17
loop numbers = [6, 5, 3, 8, 4,
2, 5, 4]
sum1 = 0
for val in
numbers: sum1
= sum1+val
print("The sum is", sum1)
OUTPUT
The sum is
37
for Loop and for Loop with
else
EX-01: OUTPU
genre = ['pop', 'rock', T
'jazz'] for i in I like
range(len(genre)): pop I
print("I like", genre[i]) like rock
EX-02: I like
genre = ['pop', 'rock', OUTPUT
jazz
'jazz'] for i in I like
range(len(genre)): pop I
print("I like", like rock
genre[i]) I like
else: jazz

You might also like