Unit 1 - Iterative computation and control flow

 CBSE Revision Notes

Class-11 Computer Science (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.

Prime number test:

#in this program we'll check if the number is prime or not.
#prime number means if number is divisible by 1 and itself like 5 it's divisible by 1 and 5 only

print("Enter number")
num=eval(input())
flag=False

#loop will start from 2 as every number is divisible by 1 and will go till number-1
#if no number 1 less than the number will divide it then it's prime

for i in range(2, num-1):

#here we are checking if number is divisible by any number

if num%i==0:
print(num," is not prime")

#if number is divisible by any number then we'll set flag as true

flag=True

break

#if flag is true then it means number is not prime and if it's false then it's prime and we'll print the msg

if flag==False:
print(num," is prime")

Factorial:

#In this program we'll check the factorial of a number
#factorial of 4 is 4*3*2*1=24, factorial of 5 is 5*4*3*2*1=120

print("Enter Number")
num=eval(input())

#fact will hold the final value

fact=1

#loop will start from the number and number will be decremented after multiplication is performed

for i in range(num,1,-1):
fact=fact*i

print("Factorial of ", num," is ",fact)

#initially value of fact is 1 lets say num=4 i will have value of 4
#in ist iteration fact=1*4 fact will have value of 4
#in 2nd iteration value of i will be decremented and it will have value of 3 fact has value 4
#fact=4*3 fact will have value 12 then value of i will be 2 in next iteration
#fact=12*2 fact=24 i=1
#fact=24*1 fact=24