0% found this document useful (0 votes)
160 views

Python Control Statments PDF

The document discusses different types of conditional and control statements in Python including if, if-else, elif, nested if-else, while loops, for loops, and using else with loops. It provides syntax and examples of using each statement to make decisions or repeat blocks of code a given number of times based on conditions.

Uploaded by

Anirudh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
160 views

Python Control Statments PDF

The document discusses different types of conditional and control statements in Python including if, if-else, elif, nested if-else, while loops, for loops, and using else with loops. It provides syntax and examples of using each statement to make decisions or repeat blocks of code a given number of times based on conditions.

Uploaded by

Anirudh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CONDITIONAL STATEMENTS

and
CONTROL STATEMENTS

Prepared by
V.Kumararaja, AP/IT
S.Thanga Prasath, AP/CSE
Er.Perumal Manimekalai Colleg of Engineering ,
Hosur
1
CONDITIONAL STATEMENTS (Decision Making)
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
2
The if statement
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)

3
Example program
i=int(input(“Enter the number:”))
If (i<=10):
print(“ condition is true”)

OUTPUT
Enter the number: 9
Condition is true

4
If … else statement
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

5
Write a program to check if a number is Odd or Even
num = int(input(“Enter the number:”))
if (num % 2)== 0:
print (“Given number is Even”)
else:
print(“ Given number is Odd”)

OUTPUT
Enter the number: 9
Given number is Odd

6
elif Statements
 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.
SYNTAX
if expression:
Body of if
elif expression:
Body of elif
else:
Body of else
7
Figure – elif condition Flowchart

8
Example: largest among three numbers

a = int(input(“Enter 1st number:”))


b= int(input(“Enter 2nd number:”))
c= int(input(“Enter 3rd number:”))
OUTPUT
if (a > b) and (a > c):
Enter 1st number:10
print("a is greater")
Enter 2nd number:25
elif (b < a) and (b < c): Enter 3rd number:15
print(“b is greater") B is greater
else:
print(“c is greater")

9
Nested if … else Statements

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.

10
Syntax
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
.
11
• Example program
n = int(input(“Enter number:”))
If (n<=15):
if (n == 10): OUTPUT
print(‘play cricket’) Enter number : 10

else: Play cricket


print(‘play kabadi’)
Else:
print(‘Don’t play game’)

12
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 staement
13
While loop statement
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)

14
Write a program to find sum of number
num = int(input("Enter a number: "))
sum = 0
while(num > 0): OUTPUT

sum = sum+num Enter a number: 10


num = num-1 The sum is 55
print("The sum is",sum)

15
Using else statement with while loops
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
counter = 0
while counter < 3: OUTPUT
Inside loop
print("Inside loop") Inside loop
counter = counter + 1 Inside loop
else: Outside loop
print(“Outside loop")
16
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 val in sequence:
Syntax Body of for loop

17
For loop flow chart
Addition of number using for 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

18
for Loop and for Loop with else
EX-01: OUTPUT
genre = ['pop', 'rock', 'jazz'] I like pop
for i in range(len(genre)): I like rock
print("I like", genre[i]) I like jazz

EX-02:
genre = ['pop', 'rock', 'jazz'] OUTPUT
for i in range(len(genre)): I like pop
print("I like", genre[i]) I like rock
else: I like jazz
print("No items left.") No items left.

You might also like