ITERATION/CONTROL
STATEMENTS:
·
state
·
while
·
for
·
break
·
continue
·
pass
Transition from
one process to another process under specified condition with in a time is
called state.
·
While loop
statement in Python is used to repeatedly executes set of statement as long as
a given condition is true.
· In while loop, test expression is checked first. The body of the loop is entered only if the test_expression is True. After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False.
·
In
Python, the body of the while loop is determined through indentation.
·
The
statements inside the while starts with indentation and the first unindented
line marks the end.
1.
program to find
sum of n numbers:
2.
program to find
factorial of a number
3.
program to find
sum of digits of a number:
4.
Program to
Reverse the given number:
5.
Program to find
number is Armstrong number or not
6.
Program to check
the number is palindrome or not
n=eval(input("enter
n"))
i=1
sum=0
while(i<=n):
sum=sum+i
i=i+1
print(sum)
output
enter n
10
55
n=eval(input("enter
n"))
i=1
fact=1
while(i<=n):
fact=fact*i
i=i+1
print(fact)
output
enter n
5
120
n=eval(input("enter
a number"))
sum=0
while(n>0):
a=n%10
sum=sum+a
n=n//10
print(sum)
output
enter a number
123
6
n=eval(input("enter
a number"))
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
print(sum)
enter a number
123
321
n=eval(input("enter
a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum+a*a*a
n=n//10
if(sum==org):
print("The
given number is Armstrong number")
else:
print("The
given number is not
Armstrong
number")
output
enter a
number153
The given number
is Armstrong number
n=eval(input("enter
a number"))
org=n
sum=0
while(n>0):
a=n%10
sum=sum*10+a
n=n//10
if(sum==org):
print("The
given no is palindrome")
else:
print("The
given no is not palindrome")
output
enter a
number121
The given no is
palindrome
v
We can generate a sequence of numbers using range() function.
range(10) will generate numbers from 0 to 9 (10 numbers).
v
In range function have to define the start, stop and step size as range(start,stop,step size).
step size defaults to 1 if not provided.
Syntax
The for loop in Python is used to iterate over a sequence (list, tuple, string). Iterating over a sequence is called traversal. Loop
continues until we reach the last element in the sequence.
The body of for loop is separated from the rest of the code
using indentation.
Examples:
1.
print
nos divisible by 5 not by 10:
2.
Program
to print fibonacci series.
3.
Program
to find factors of a given number
4.
check
the given number is perfect number or not
5.
check
the no is prime or not
6.
Print
first n prime numbers
7.
Program
to print prime numbers in range
n=eval(input("enter
a"))
for i in
range(1,n,1):
if(i%5==0
and i%10!=0):
print(i)
enter
a:30
5
15
25
a=0
b=1
n=eval(input("Enter
the number of terms: "))
print("Fibonacci
Series: ")
print(a,b)
for i in
range(1,n,1):
c=a+b
print(c)
a=b
b=c
output
Enter
the number of terms: 6
Fibonacci
Series:
0 1
1
2
3
5
8
n=eval(input("enter
a number:"))
for i in
range(1,n+1,1):
if(n%i==0):
print(i)
Output
enter a
number:10
1
2
5
10
n=eval(input("enter
a number"))
for i in
range(2,n):
if(n%i==0):
print("The
num is not a prime")
break
else:
print("The
num is a prime number.")
enter a
no:7
The num
is a prime number.
n=eval(input("enter
a number:"))
sum=0
for i in
range(1,n,1):
if(n%i==0):
sum=sum+i
if(sum==n):
print("the
number is perfect number")
else:
print("the
number is not perfect number")
enter a
number:6
the
number is perfect number
number=int(input("enter no
of prime
numbers
to be displayed:"))
count=1
n=2
while(count<=number):
for i in
range(2,n):
if(n%i==0):
break
else:
print(n)
count=count+1
n=n+1
enter no
of prime numbers
to be
displayed:5
2
3
5
7
11
lower=eval(input("enter
a lower range"))
upper=eval(input("enter
a upper range"))
for n in
range(lower,upper + 1):
if n
> 1:
for i in
range(2,n):
if (n %
i) == 0:
break
else:
print(n)
output:
enter a
lower range50
enter a
upper range100
53
59
61
67
71
73
79
83
89
97
v Break statements can alter the
flow of a loop.
v It terminates the current
v loop and executes the remaining statement
outside the loop.
v If the loop has else statement, that will also
gets terminated and come out of the loop completely.
Break
for i in
"welcome":
if(i=="c"):
break
print(i)
w
e
l
It terminates
the current iteration and transfer the control to the next iteration in the
loop.
Continue
for i in
"welcome":
if(i=="c"):
continue
print(i)
w
e
l
o
m
e
v It is used when a statement is required
syntactically but you don’t want any code to execute.
v It is a null statement, nothing happens when
it is executed.
pass
break
for i in
“welcome”:
if (i == “c”):
pass
print(i)
w
e
l
c
o
m
e
v If else statement is used in for loop, the
else statement is executed when the loop has reached the limit.
v The statements inside for loop and statements
inside else will also execute.
for i in
range(1,6):
print(i)
else:
print("the
number greater than 6")
1
2
3
4
5 the number greater than 6
v If else statement is used within while loop ,
the else part will be executed when the condition become false.
v The statements inside for loop and statements
inside else will also execute.
i=1
while(i<=5):
print(i)
i=i+1
else:
print("the
number greater than 5")
1
2
3
4
5
the number
greater than 5
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.