### Unit 1 - Iterative computation and control flow

CBSE Revision Notes

Class-11 Informatics Practices (New Syllabus)

Unit 1: Programming and Computational Thinking (PCT-1)

Iterative computation and control flow

Notion of iterative computation and control flow: Iteration repeats the execution of a sequence of code. Iteration is useful for solving many programming problems. Iteration and conditional execution form the basis for algorithm construction.

while Loop: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. Let’s try to understand why we need iterative statements or loops. Have a look at these 5 statements.

Print(1)

Print(2)

Print(3)

Print(4)

Print(5)

If we have to print 5 numbers we need to write 5 different statement but with the help of loops we can achieve it using 1 print statement have a look at this example

count=1 #counter initialized to 1

while count<=5: #this statement will repeat until condition is true

print(count) #printing the value of count which will start from 1 till 5

count=count+1 #this statement will increment the value of count ist it will be 1

Then it will be incremented to 2 till 5 at 6 loop will end and control will come out of while block. Let’s understand this with the help of a flowchart.

for Loop: It has the ability to iterate over the items of any sequence, such as a list or a string. Difference between for and while loop is in ist we initialize the counter then we check the condition and finally we increment the counter but in for loop all this is done in a single line. Let’s see the syntax

for n in range(begin, end, steps):

print(n)

here n is a counter in range function we specify what is the starting point of the counter where it needs to stop and value of increment/ decrement if we won’t specify steps then by default it’s 1.

Let’s see an example

for n in range(1,10):

print(n)

It will display value from 1 to 9. Let’s understand for loop with the help of flowchart.

Simple interest program:

#user will enter amount, rate of interest and time.

print("Enter amount")

p=eval(input())

print("Enter rate of interest")

r=eval(input())

print("Enter Time")

t=eval(input())

#calculating interest

SI=(p*r*t)/100

print("Interest after ", t," years at ", r,"% is", SI)

** “#” denotes start of comment and it’s excluded when program is executed.

__EMI Calculartor:__

# EMI Calculator program in Python

def emi_calculator(p, r, t):

r = r / (12 * 100) # one month interest

t = t * 12 # one month period

emi = (p * r * pow(1 + r, t)) / (pow(1 + r, t) - 1)

return emi

# driver code

principal = 10000;

rate = 10;

time = 2;

emi = emi_calculator(principal, rate, time);

print("Monthly EMI is= ", emi)

__Standard Deviation Calculartor:__

import math

import sys

def sd_calc(data):

n = len(data)

if n <= 1:

return 0.0

mean, sd = avg_calc(data), 0.0

# calculate stan. dev.

for el in data:

sd += (float(el) - mean)**2

sd = math.sqrt(sd / float(n-1))

return sd

def avg_calc(ls):

n, mean = len(ls), 0.0

if n <= 1:

return ls[0]

# calculate average

for el in ls:

mean = mean + float(el)

mean = mean / float(n)

return mean

data = [4, 2, 5, 8, 6]

print("Sample Data: ",data)

print("Standard Deviation : ",sd_calc(data))

Sample Data: [4, 2, 5, 8, 6]

Standard Deviation : 2.23606797749979

__correlation coefficient Calculartor:__

# Python Program to find correlation coefficient.

import math

# function that returns correlation coefficient.

def correlationCoefficient(X, Y, n) :

sum_X = 0

sum_Y = 0

sum_XY = 0

squareSum_X = 0

squareSum_Y = 0

i = 0

while i < n :

# sum of elements of array X.

sum_X = sum_X + X[i]

# sum of elements of array Y.

sum_Y = sum_Y + Y[i]

# sum of X[i] * Y[i].

sum_XY = sum_XY + X[i] * Y[i]

# sum of square of array elements.

squareSum_X = squareSum_X + X[i] * X[i]

squareSum_Y = squareSum_Y + Y[i] * Y[i]

i = i + 1

# use formula for calculating correlation

# coefficient.

corr = (float)(n * sum_XY - sum_X * sum_Y)/

(float)(math.sqrt((n * squareSum_X -

sum_X * sum_X)* (n * squareSum_Y -

sum_Y * sum_Y)))

return corr

# Driver function

X = [15, 18, 21, 24, 27]

Y = [25, 25, 27, 31, 32]

# Find the size of array.

n = len(X)

# Function call to correlationCoefficient.

print ('{0:.6f}'.format(correlationCoefficient(X, Y, n)))

Output: 0.953463

__GST Calculator:__

# Python3 Program to

# compute GST from original

# and net prices.

def Calculate_GST(org_cost, N_price):

# return value after calculate GST%

return (((N_price - org_cost) * 100) / org_cost);

# Driver program to test above functions

org_cost = 100

N_price = 120

print("GST = ",end='')

print(round(Calculate_GST(org_cost, N_price)),end='')

print("%")

Output: GST is 20%