Home | | Problem Solving and Python Programming | Iteration/Control Statements - Python

Chapter: Problem Solving and Python Programming : Control Flow, Functions

Iteration/Control Statements - Python

state · while · for · break · continue · pass

ITERATION/CONTROL STATEMENTS:

 

·                 state

·                 while

·                 for

·                 break

·                 continue

·                 pass

 

State:

Transition from one process to another process under specified condition with in a time is called state.

 

While loop:

 

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

 

Syntax:


Flowchart

 

Examples:

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

 

Sum of n numbers:

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

 

Factorial of a numbers:

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

 

Sum of digits of a number:

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

 

Reverse the given number:

n=eval(input("enter a number"))

sum=0

while(n>0):

a=n%10

sum=sum*10+a

n=n//10

print(sum)

 

output

enter a number

123

321

 

Armstrong number or not

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

 

Palindrome or not

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

 

For loop:

 

for in range:

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


Flowchart:


 

For in sequence

 

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.



Sequence can be a list, strings or tuples


 

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

 

print nos divisible by 5 not by 10

n=eval(input("enter a"))

for i in range(1,n,1):

if(i%5==0 and i%10!=0):

print(i)

 

output

enter a:30

5

15

25

 

Fibonacci series

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

 

 

find factors of a number

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

 

 

check the no is prime or not

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.")

 

 

output

enter a no:7

The num is a prime number.

 

check a number is perfect number or not

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")

 

Output

enter a number:6

the number is perfect number

 

Program to print first n prime numbers

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

 

Output

enter  no  of  prime  numbers  to  be

displayed:5

2

3

5

7

11

 

Program to print prime numbers in range

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

 

Loop Control Structures

 

BREAK

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.

 

Syntax:

Break


 

Flowchart


 

example

for i in "welcome":

if(i=="c"):

break

print(i)

 

Output

w

e

l

 

CONTINUE

 

It terminates the current iteration and transfer the control to the next iteration in the loop.

 

Syntax: 

Continue


 

Flowchart


 

 

Example:

for i in "welcome":

if(i=="c"):

continue

print(i)

 

Output

w

e

l

o

m

e

 

PASS

 

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.

 

Syntax:

pass

break

 

Example

for i in “welcome”:

if (i == “c”):

pass

print(i)

 

Output

w

e

l

c

o

m

e

 

Difference between break and continue


 

else statement in loops:

else in for loop:

 

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.

 

example

for i in range(1,6):

print(i)

else:

print("the number greater than 6")

 

output

1       

2       

3       

4       

5        the number greater than 6

 

else in while loop:

 

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.

 

Program

i=1

while(i<=5):

print(i)

i=i+1

else:

print("the number greater than 5")

 

output

1

2

3

4

5

the number greater than 5

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Control Flow, Functions : Iteration/Control Statements - Python |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.