0% found this document useful (0 votes)
38 views47 pages

Unit 2 Chapter 5

Uploaded by

Ekansh Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views47 pages

Unit 2 Chapter 5

Uploaded by

Ekansh Arya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Chapter – 5

Conditional and
Iterative
Statements
Statement Flow Controls
Every programming language provide three
types of statements :

1. Sequence (I P O)
2. Selection
3. Iteration
Sequence Statements
• The sequence construct means the statements
are being executed sequentially.
Statement 1

Statement 2

Statement 3
Selection Statements
The selection construct means the execution of
statements depending upon a condition –
test.
If a condition evaluates to True, a course-of-
action (a set of statements) is followed
otherwise another course-of-action (a
different set of statements) is followed.
SELECTION FLOW DIAGRAM
Iteration (Looping) Statements
The iteration constructs mean repetition of a set
of statements depending upon a condition-
test.
Till the time condition is True (or False
depending upon the loop) a set of statements
are repeated again and again. As soon as the
condition is False (or True), the repetition
stops.
The iteration constructs also called looping
constructs.
Iteration Flow Diagram
The if Statement of Python
The if statements are the conditional statements
in Python and these implement selection
constructs (decision constructs).
The simplest if statement tests a condition and if
the condition evaluates to true, it carries out
some instructions and does nothing in case
condition evaluates to false.
Program to check eligibility for voting.

n=int(input("Enter age of a person : "))


if n>=18:
print("Eligible to vote!")
Q.1 WAP to input a number and check that
square of that number is equal to 50 or not.
Q2. WAP to input two numbers and check for
the largest number.
X= int(input(“Enter first number:”))
Y= int(input(“Enter second number:”))
if X>Y:
print(“First no. is largest”)
if Y>X:
print(“Second no. is largest”)
The if – else Statement
The form of if statement tests a condition and if the
condition evaluates to true, it carries out
statements indented below if and in case
condition evaluates to false, it carries out
statements indented below else.
Example :
n = int(input(“Enter age of a person : ”))
if n>=18:
print(“Eligible to vote”)
else:
print(“Not eligible to vote”)
Program to accept three numbers and print the
largest of the three.
x=y=z=0
x=int(input(“Enter first number : ”))
y=int(input(“Enter second number : ”))
z=int(input(“Enter third number : ”))
max=x
if y>max:
max=y
if z>max :
max=z
print(“largest number is ”,max)
Program that takes a number and checks
whether the given number is odd or even.

N = int(input(“Enter any number :” ))


if N%2==0:
print(N, “is EVEN number”)
else:
print(N, “is ODD number”)
The if – elif statement
• The elif is short for else if. It allows us to check for
multiple expressions.
• If the condition for if is False, it checks the condition
of the next elif block and so on.
• If all the conditions are False, the body of else is
executed.
• Only one block among the
several if...elif...else blocks is executed according to
the condition.
• The if block can have only one else block. But it can
have multiple elif blocks.
Program that accepts three numbers and
display largest among them.
x=y=z=0
x=int(input(“Enter first number : ”))
y=int(input(“Enter first number : ”))
z=int(input(“Enter first number : ”))
if x>y and x>z:
print (x, “is largest number”)
elif y>z and y>x:
print (y, “is largest number”)
elif z>x and z>y:
print (z, “is largest number”)
else:
print(“Two or more values are equal”)
Program to input day number and display
corresponding day name.
d = int(input(“Enter day number”))
if d==1:
print(“Sunday”)
elif d==2:
print(“Monday”)
elif d==3:
print(“Tuesday”)
else:
print(“Wrong value entered!!!”)
Program to enter two numbers and operator.
Perform operation as per the operator.
x= int(input(“Enter first number”))
y=int(input(“Enter second number”))
a=input(“Enter any operator + , - , / , * : ”)
if a==‘+’:
print(“Sum of two numbers =“,x+y)
elif a==‘-’:
print(“Difference of two numbers =“,x-y)
elif a==‘/’:
print(“Division of two numbers =“,x/y)
elif a==‘*’:
print(“Multiplication of two numbers =“,x*y)
else:
print(“Wrong value entered”)
The nested if statement
if condition:
if condition:
expression
else:
expression
else:
if condition:
expression
else:
expression
Iteration / Looping Statement
The iteration statements or repetition
statements allow a set of instructions to be
performed repeatedly until a certain condition
is fulfilled.
The iteration statements are also called loops or
looping statements.
Python provides two types of loops:
• for loop
• while loop
The for loop
The for loop of Python is designed to process
the items of any sequence, such as a list or a
string one by one.
Syntax:
for <variable> in <sequence>:
statement to repeat
Example :
for x in [1,4,7]:
print(x) 🡪 7
print(x*x) 🡪 49
range() function
• range() is a built-in function of Python. It is used when a user
needs to perform an action for a specific number of times.
• The range() function is used to generate a sequence of
numbers.
• In simple terms, range() allows user to generate a series of
numbers within a given range.
• range() takes mainly three arguments:
1. start: integer starting from which the sequence of integers is to
be returned
2. stop: integer before which the sequence of integers is to be
returned.
The range of integers end at stop – 1.
3. step: integer value which determines the increment between
each integer in the sequence
Example of range() function
range (10) – 0,1,2,3,4,5,6,7,8,9
range(5,10) – 5,6,7,8,9
range(3,7) – 3,4,5,6
range(5,15,3) – 5,8,11,14
range(9,3,-1) – 9,8,7,6,5,4
range(10,1,-2) – 10,8,6,4,2
Program to print table of 5.

N=5
for a in range(1,11):
print(N, ‘x’ , a , ‘=’ , N*a)

5x1=5
5x2=10
5X3=15
5x10=50
Program to print sum of natural numbers
between 20 to 4.
sum=0
for a in range(4,21):
sum=sum+a
print(“Sum of natural numbers =“, sum)

sum = 0+1=1+2=3+3=6+4=10+5=15+6=21+7=28
Sum of natural numbers = 28
Print natural number from 20 to 1.
for i in range(20,0,-1)
print(i, end=‘ ’)

20 19 18 17 16 ……
Q1. WAP to display even numbers from 2 to 50.
Q2. WAP to display multiplication table of any
entered number.
Q3. WAP to display odd numbers from entered
number to 1.
Q4. WAP to display squares of numbers from 1
to 40 those are divisible by 3.
Q5. WAP to display following series:
0 1 1 2 3 5 8 13 21 (till N terms) fibonacci
N=int(input(“Enter no. of terms”))
A=0
B=1
print(A,B,end=‘ ’)
for I in range(3,N+1):
C=A+B
print(C, end=‘ ’)
A=B
B=C

0 1 1 2 3 5 8 13 21…. N
A B C
The while loop
While Loops is used to execute a block of
statements repeatedly until a given condition
is satisfied. And when the condition becomes
false, the line immediately after the loop in
the program is executed.
Syntax:
while expression:
statement(s)
Example of while loop
count = 0
while count < 3:
count = count + 1
print("Hello World")
print(count)
Output:
Hello World
Hello World
Hello World
3
Anatomy of while loop
A while loop has four elements that have
different purposes:
1. Initialization Expression(s) (Starting)
2. Test Expression
3. The Body of the loop
4. Update Expression
Q. WAP to display even numbers from 1 to 40
using while loop.
Q. WAP to display squares of numbers from 50
to 1.
X=50
while X>=1:
print(X**2)
X=X-1
Jump Statements
The break Statement – the break statement enables
a program to skip over a part of the code.
A break statement terminates the very loop it lies
within.
Example:
for i in range(1,20):
if i == 5:
break
print(i)
print(“Over”)
Q1. WAP to display multiplication table
of any entered number using while loop.
N=int(input(“Enter any positive number:”))
A=1
while A<=10:
print(N, ‘x’ , A, ‘=‘ , N*A)
A=A+1
The continue Statement
The continue statement forces the next iteration
of the loop to take place, skipping any code in
between.
Example:
for i in range(1,20):
if i == 5:
continue
print(i)
Nested Loops
A loop may contain another loop in its body. This
form of a loop is called nested loop.
Example:
for i in range(1,6):
for j in range(1,i):
print(“*”,end=‘ ’)
print()
Q. WAP to display sum of following series:
Sum= 1+ 3+ 5+ 7+ 9+ 11+ 13

A=1
Sum=0
while A<=13:
Sum=Sum+A
A=A+2
print(Sum)
Q. What is the output of following code?

N=int(input(“Enter Number”))
Sum=0
i=1
while i < N:
if i%2==0:
sum = sum + i
i=i+2
print (sum)
For value = 5
For value = 0
for i in range(1,6): 3
for j in range(1,i+1): 1 2 3
print(j,end=‘ ’)
print()

1
12
123
1234
12345
Q. WAP to display following patterns:
1. 1
12
123
1234
12345
2. 12345
1234
123
12
1
for i in range(5,0,-1):
for j in range(1,5):
print(j,end=“ ”)
print()

i=3
J= 12345
1234
123
#####
####
###
##
#
for i in range(5,0,-1):
for j in range(1,i+1):
print(“#”,end=“ ”)
print()
Q. WAP to display factors of any entered
number?
Example: 25 = 1 , 5 , 25
24 = 1 , 2 , 3 , 4 , 6 , 12, 24

N=int(input(“Enter any number : ”))


for i in range(1,N+1):
if N%i==0:
print(i)
print(“ A \n B”) ‘’’This line will print A and B in
separate lines .,mdf.fg .lj vc ,.m.,mnvc.
M.,.,m.b.,nmcv,.m b.m’’’
R = float(input(“Enter Radius”))
print(“1. For calculating Area of Circle”)
print(“2. For calculating Perimeter of Circle”)
N=int(input(“Enter your choice:”))
if N==1:
print(“Area of circle :”,3.14 * R * R)
elif N==2:
print(“Peri of circle :”,2 * 3.14 * R)
else:
print(“Wrong value enter”)
WAP to find average of list of numbers
entered through keyboard.
Enter your no. : 12
Do you want to enter more no.? Y
Enter your no. : 13
Do you want to enter more no.? Y
Enter your no. : 25
Do you want to enter more no.? N
Average : 16.666666
A=SUM=C=0
Y=‘Y’
while Y==‘Y’:
X= int(input(“Enter any no. :”)) 10 12 14 20
C=C+1
SUM=SUM+X
Y = input(“Do you want to enter more no.?
(Y/N)”) N
A= SUM/C
print(“Average =“,A)
for :
if x==10:

You might also like