Class 11 - Presentation # 5 - Iterative Statements - PART 1
Class 11 - Presentation # 5 - Iterative Statements - PART 1
XI
Introduction
Iteration means to do something repeatedly. Iteration is also called loop.
2
XI
Types of Loops
Loops
for while
3
XI
while loop
while is a loop used for repeated execution as long as a specified expression
Syntax : Statements 1
[else:
Statements 2]
4
XI
while loop
While loop with else clause While loop without else clause
false false
Out of loop
5
XI
Loop Control Elements
Every while loop has its elements that control and govern its execution. A while
6
XI
Loop Control Elements
7
XI
while loop
Statements
…..
…..
Update expression
8
XI
Some important points:
In a while loop, the loop control variable should be initialized before the
●
The loop variable must be updated inside the body of the loop in a way
●
that after sometime the test condition becomes false otherwise the loop
will become an endless loop or infinite loop.
● While loop is also called entry-controlled loop as the entry into the
loop is controlled by testing condition.
9
XI
Example: Code to print a message 5 times on
screen Test
Initialisation
count=1
while count<=5: Body of loop
print("Learn Python")
count=count+1
Update expression
10
XI
Example : Code to print first 10 natural
numbers
11
XI
Question 1
Write a program to print all even numbers from 1 to 10 and find their sum too.
12
XI
Question 2
Write a program to print table of number n.
13
XI
Question 3
Write a program input a number and then find the factorial of a number.
14
XI
Programs
1. Print all the natural numbers from m to n.
16
XI
Question 2
Write a program to print all the natural numbers from m to 1.(Reverse Order)
17
XI
Question 3
Write a program to input a number n and then print all the odd numbers from 1 to m
18
XI
Question 4
Write a program to input a number n and then print all its factors.
19
XI
Question 5
Write a program to input a number n and then print the sum of all its factors.
21
XI
Explanation :
I 9 9 230
II 0 90 23
III 3 903 2
IV 2 9032 0
Enter a number:
num = int(input("Enter a number: "))
4224
rev = 0
4224 is a palindrome
t = num
while t > 0:
digit = t % 10
rev=rev*10+digit
t //= 10
if num == rev:
print(num,"is a Palindrome")
else:
XI
print(num,"is not a palindrome") 23
Question 6
Write a program to input a number and then print if the number is an armstrong
number or number
not . is a number that is equal to the sum of cubes of its digits. Eg:(153 =
s a word,
num = number
int(input("Enter a number: ")) Enter a number: 153
sum = 0 153 is an Amrstrong
t = num number
while t > 0:
digit = t % 10
sum += digit ** 3
t //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
24
XI
Question 7
Write a program to print fibonacci series till value n
s a word, number
num = int(input("Enter a number: ")) Enter a number: 10
a=0 0
b=1 1
print(a,end=',') 1
print(b,end=’,’) 2
c=a+b 3
5
while c <=num:
8
print(c,end=',')
a=b
b=c
c=a+b 25
XI
Question 7
Write a program to print fibonacci series upto n
s a word, number
28
XI
Unconditional exit from loop : break
statement
29
XI
Shift to the next iteration of the loop - continue
statement
30
XI
Shift to the next iteration of the loop - continue
statement
31
XI
Shift to the next iteration of the loop - continue
statement
32
XI
Shift to the next iteration of the loop - continue
statement
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print ('Current variable value :', var)
The else part is executed if the condition in the while loop evaluates to False.
s a word, number
34
XI
Nested while loop
Nested loop is a loop inside another loop.
35
XI
Nested while loop
36
XI
Programs
1. Write a program to print the following patterns :
1234 2 2 * *
123 3 3 3 * * *
12 4 4 4 4 * * * *
1 5 5 5 5 5 * * * * *
37
XI
Program to generate factorial of all natural numbers
till n.
38
XI
Programs
1. Program to print all the prime numbers till n.
39
XI
Program to print all the prime numbers till n.
a=int(input("Enter a x=123045
number: ") while x%10:
while a%5!=0 x=//10
if (a%2>0): print(x,sep="--")
print(a,sep="--) else print(a)
a+=1
else: print(a)
43
XI
Solution:
a=int(input("Enter a number: ") Missing closing bracket
x=123045
while x%10:
x=//10 Wrong assignment
operator
print(x,sep="--")
else print(a) a is not defined
45
XI
Solution
a=int(input("Enter a number: ") Missing closing bracket
46
XI
Find the output of the following code:
49
XI
Solution:
50
XI
Give output :
Find the output for :
51
XI
Solution:
Enter value of n : -6
-5
52
XI
Give output :
53
XI
Solution
54
XI
Give output of the following code:
55
XI