Unit 2 Chapter 5
Unit 2 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=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