Chapter 4 Flow of Control IP-updated
Chapter 4 Flow of Control IP-updated
Learning Objective:
Computational Thinking.
Algorithms, Flowcharts, Pseudocodes and Decision Tree.
The Control Flow-Sequence, Selection and Iteration.
If el if in Python.
For Range and While Loop.
Nested loops.
Control Structure refers to the program segment which controls the flow of program
execution. In any programming language, there are 3 control structures: Sequence,
Selection and Iteration.
4.10.1 SEQUENCE
Sequence refers to the execution when statements are executed one after another.
This construct specifies the normal flow of control in a program in which all the
statements are executed as they appear. This represents the default flow of execution.
So far whichever lines of coding you have written represent sequence flow of control.
4.10.2 SELECTION
4.10.3 ITERATION
Iteration refers to repeated execution of a set of statement depending on a condition.
Till the time a particular condition is true, a set of statements are repeated again and
again. As soon as the condition becomes False, the repetition stops. Iterative control
structure is controlled by looping statement.
Often, you need to execute some statements only if some condition holds, or choose
statements to execute depending on several mutually exclusive conditions. The Python
compound statement if, which uses if, elif, and else clauses, lets you conditionally
execute blocks of statements. Here’s the syntax for the if statement:
if expression:
statement(s)
elif expression:
statement(s)
elif expression:
statement(s)
...
else:
statement(s)
The elif and else clauses are optional. Note that unlike some languages, Python does
not have a switch statement, so you must use if, elif, and else for all conditional
processing.
When there are multiple statements in a clause (i.e., the clause controls a block of
statements), the statements are separated using different indents.
If the expression for the if clause evaluates as true, the statements following
the if clause execute, and the entire if statement ends. Otherwise, the expressions for
any elif clauses are evaluated in order. The statements following the first elif clause
whose condition is true, if any, are executed, and the entire if statement ends.
Otherwise, if an else clause exists, the statements following it are executed.
Any number of these statements can be nested inside one another. Indentation is the
only way to figure out the level of nesting. This can get confusing, so must be avoided if
we can.
Solution:
a=int(input("enter a number"))
if a>0:
print(a,"is positive")
else:
print(a,"is negative")
Solution:
if a>0:
print("it is positive")
elif a<0:
print("it is negative")
else:
print("the number is 0")
Percentage Grade
> 90 Outstanding
81 to 90 High Achiever
71 to 80 Good
< 70 Average
Solution:
av=(a+b+c+d)/4
if av>90:
print("Outstanding")
print("Good")
else:
print("Average")
Print(“MAIN MENU”)
print-------------------------------------------------
print(“1. Area of Right angled Triangle “)
2. Area and Perimeter of a Square
3. Area and Perimeter of a Circle
Choice=int(input(“”))
If choice==1:
Area=
Print(area)
Elif choice==2:
Area
Perimeter
Solution:
a=0.5*l*b
print(“the area is “, a)
elif ch==2:
a=s*s
p=4*s
elif ch==3:
a=3.14*r*r
p=2*3.14*r
print(3.14*r*r)
else:
print(“invalid choice”)
Sales Bonus DA
Net=sales+A+DA
Print(net)
Solution:
if a>50000:
b=.4*a
n=a+b+2000
b=.3*a
n=a+b+1500
b=.2*a
n=a+b+1000
b=.15*a
n=a+b+500
else:
b=.05*a
n=a+b+200
Solution:
elif a%4==0:
else:
7. Accept three numbers. Find the sum and the sum of non
duplicate numbers(Eg: sum of 3, 3, 6
=12;
Sum of Non duplicate numbers of 3, 3, 6=6)
Solution:
a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
if a==b:
print(“the sum is “, c)
elif b==c:
print((“the sum is “, a)
elif a==c:
print((“the sum is “, b)
print((“the sum is “, 0)
else:
9. WAPS (Write a Python Script) to find the max,mid and min from
from three numbers.
Solution:
a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
o=a, b, c
o=b, a, c
o=c, a, b
o=c, b, a
else:
o=a,c, b
Solution :
q=input("Enter a character")
print("Capital")
print("Not capital")
elif q>'0':
print("Digit")
else:
print("Special character")
Solution :
import math
print("MAIN MENU")
print("-----------------------------------------")
a=int(input("Enter a"))
b=int(input("Enter b"))
c=int(input("Enter c"))
x=math.pow((a+b),2) + math.pow((a-b),2)
y=math.sqrt(a) + math.sqrt(b)
print(x)
print(y)
print(z)
12. WAP to accept a number and round off to the bigger value
using ceil( ) function and round off to the lower value using
floor( ) function(hint: import math )( not in syllabus)
Solution:
import math
a=float(input("Enter a"))
r1=math.ceil(a)
r2=math.floor(a)
Syntax
range(start, stop, step)
Parameter Description
Let’s understand range with the help of the examples given below.
Example:
Range (0,5):This will generate numbers 0,1,2,3,4.
Range(x,y): This will generate numbers x,x+1…..y-1.
Range (2, 8, 2): This will generate numbers 2, 4, 6.
Range (8, 3,-1): This will generate numbers 8, 7,6,5,4.
IN and NOT IN are membership operators. The ‘in’ operator is used to check if
a value exists in a sequence or not. Evaluates to true if it finds a variable in the
specified sequence and false otherwise.
>>> 3 not in [2, 3, 4]
False
>>> 3 not in [4, 5, 6]
True
for x in range(0,6):
print(x)
output will be
0
1
2
3
4
5
Notice that 6 is not included in the list.
Solution: Output
for a in range(1,5): Computer Science
print("Computer Science") Computer Science
Computer Science
Computer Science
2. WAPS to print first 5 natural numbers.
Solution: Output
for a in range(1,6): 1
2
print(a)
3
4
5
3. WAPS to print numbers between 6 and 10.
Solution: Output
for a in range(6,11): 6
7
print(a)
8
9
10
4. WAPS to print odd numbers between 1 and n.(include
n)
Solution: Output
enter the value of n : 5
n=int(input("enter the value 1
3
of n : "))
5
for i in range(1,n+1,2):
print(i)
Solution: Output
enter the value of n : 5
n=int(input("enter the value 2
4
of n : "))
for i in range(2,n+1,2):
print(i)
Solution: Output
enter the value of n : 1
n=int(input("enter the value 8
6
of n : "))
4
for i in range(8,n,-2): 2
print(i)
Solution: Output
enter the value of n : 4
n=int(input("enter the value 8
7
of n : "))
6
for i in range(8,n+1,-1):
print(i)
Solution: Output
enter the value of n : 5
n=int(input("enter the value 5*1=5
5*2=10
of n\n"))
5*3=15
5*4=20
for i in range(1,11): 5*5=25
5*6=30
print(n," * ",i,"= ",i*n)
5*7=35
5*8=40
5*9=45
5*10=50
Solution: Output
enter the value of n : 5
S=0 enter the value of n : 34
for i in range(1,6): enter the value of n : 23
n=int(input("enter the enter the value of n : 23
value of n : ")) enter the value of n : 67
s=s+n 152
print(s)
11. WAPS to display first n terms of fibonecci series.
(Where every third term is the sum of previous two.)
0 1 1 2 3 5 8 ….n
a b c
a b c
Solution: Output
n=int(input("enter a enter a number10
0
number"))
1
a=0 1
2
b=1
3
print(a) 5
8
print(b)
13
for i in range(3,n+1): 21
34
c=a+b
fibonacci series printed
print(c)
a=b
b=c
else:
print("fibonacci series
printed")
Solution: Output
f=1 enter n : 5
120
n=int(input("enter n : "))
for i in range(1,n+1):
f=f*i
print(f)
14. WAPS find the factors of a number.
Solution: Output
n=int(input("enter a number
: enter a number : 24
1 is factor
"))
2 is factor
for i in range(1,n+1): 3 is factor
4 is factor
if n%i==0:
6 is factor
print(i, “ is factor”) 8 is factor
12 is factor
24 is factor
Solution: Output
count=0 enter a number : 23
n=int(input("enter a number : prime
"))
for i in range(1,n+1):
if n%i==0:
count=count+1
if count==2:
print("prime")
else:
print("non prime")
Solution: Output
a=int(input("enter first enter first number6
enter second number24
number"))
gcd is 6
b=int(input("enter second lcm is 24.0
number"))
if a<b:
lower=a
else:
lower=b
for i in range(1,lower+1):
if a%i==0 and b%i==0:
g=i
l=a*b/g
print("gcd is ",g)
print("lcm is ",l)
Solution: Output
count=0 Enter a number: 6
Perfect
n=int(input("enter a number :
"))
for i in range(1,n):
if n%i==0:
sum=sum+i
if sum==n:
print("perfect")
else:
print("non perfect")
Solution: Output
for a in range(1,11): 1 3 7 15 31 63 127 255 511
1023
mersnum=2**a-1
print(mersnum,end=” “)
print()
Solution: Output
ce=0 Enter number of numbers: 6
Enter a number : 23
co=0 Enter a number : 24
Enter a number : 22
n=int(input("enter the number of numbers")) Enter a number : 35
Enter a number : 25
for i in range(1,n+1): Enter a number : 26
Count of even numbers
a=int(input("enter the number")) is :3
Count of odd numbers is :3
if a%2==0:
ce=ce+1
else:
co=co+1
print("the count of even numbers is ", ce)
se=0
so=0 Output
m=int(input("enter first
enter first number2
number"))
enter second number22
n=int(input("enter second the sum of even
numbers is 132
number"))
the sum of odd numbers is
for i in range(m,n+1): 120
if i%2==0:
se=se+i
else:
so=so+i
print("the sum of even
numbers is ",se)
print("the sum of odd numbers is
",so)
print(i*i,end=’ ‘)
Ans a,b,c=1,4,7
a=a*3
b=b*3
c=c*3
7. for i in range(100,29,-10):
print(i,end=' ')
print(i)
Ans 100 90 80 70 60 50 40 30 30
Ans w = "Raining"
if w == "sunny" :
print("Going Skiing")
else:
print("Weather")
Ans 2
True
17. Give the output
x=30
if x>30:
print("hi")
elif x>20:
print("hello")
print("bye")
Ans Hello
Bye
if x>3:
if x>4:
else:
if(x!=0):
print (“D”)
2. What is the following code doing? What would it print for input as 3?
n=int (input(“Enter an integer:”))
if n<1:
print(“invalid value”)
else:
print(i*i)