Flow of Control in Python Class 11 Notes Important Points

Share with others

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes
Flow of Control in Python Class 11 Notes

Till now we are writing those programs in Python where Python executes one statement after another from beginning to the end of the program. for example

#Program to add three numbers

n1 = int(input("Enter first number"))
n2 = int(input("Enter secod number"))
n3 = int(input("Enter third number"))
s = n1 + n2 + n3
print("Sum of three numbers is  : ", s)

#In above program all the statements are executed in sequence

The order of execution of the statements in a program is known as flow of control. The flow of control can be
implemented using control structures. Python supports two types of control structures—selection and repetition

SELECTION

When we use Google map or any other direction services, to reach from one place to another, we notice that sometimes it shows more than one path like the least crowded path, shortest distance path, etc. We decide the path as per our priority. A decision involves selecting from one of the two or more possible options.

In programming, this concept of decision making or selection is implemented with the help of if..else statement.

The syntax of if statement is:

if condition:
       statement(s)

Example 1 : Write a program to check whether a person’s age is greater than 18.

age = int(input("Enter your age "))
if age >= 18:
      print("Age is greater than Eighteen")

The syntax for if..else statement is as follows.

Flow of Control - if else
Flow of Control – if else
if condition:
       statement(s)
else:
       statement(s)

Example 2 : Accept age from the user and display a message “Hello” if the age is greater than 18 otherwise display message “Bye”

age = int(input("Enter your age ")) 
if age >= 18:       
      print("Hello")
else:
      print("Bye")

Example 3 : Write a program to find the positive difference between two numbers accepting from user.

num1 = int(input("Enter First number"))
num2 = int(input("Enter Second number"))
if num1 > num2:
     print("Difference is : ", num1 - num2)
else:
     print("Difference is : ", num2 - num1)

Example 4 : Write a program check whether a number is even or odd.

num1 = int(input("Enter any number"))
if num1 % 2 == 0:
     print("Number is even")
else:
     print("Number is odd")

The syntax for a selection structure using elif is as shown below:

if condition:
    statement(s)
elif condition:
    statement(s)
elif condition:
    statement(s)
else:
    statement(s)

Example 5 : Write a program to accept a number from the user (between 1 and 7) and display the name of the day like display “Monday” for 1, “Tuesday” for 2 ………………….. “Sunday” for 7

num1 = int(input("Enter any number"))
if num1 == 1:
    print("Monday")
elif num1 == 2:
    print("Tuesday")
elif num1 == 3:
    print("Wednesday")
elif num1 == 4:
    print("Thursday")
elif num1 == 5:
    print("Friday")
elif num1 == 6:
    print("Saturday")
elif num1 == 7:
    print("Sunday")
else:
    print("Enter number between 1 and 7")

Example 6 : Write a program to check whether a number is positive, negative, or zero.

num1 = int(input("Enter any number: "))
if num1 < 0:
     print("Number is negative")
elif num1 > 0:
     print("Number is positive")
else:
     print("Number is zero")

Example 7: Write a program to accept three numbers from the user and display the largest number

num1 = int(input("Enter First number: "))
num2 = int(input("Enter Second number: "))
num3 = int(input("Enter Third number: "))
if num1 > num2 and num1 > num3:
     print("Largest number is : ", num1)
elif num2 > num1 and num2 > num3:
     print("Largest number is : ", num2)
else:
     print("Largest number is : ", num3)

Click for more questions on if-else


REPETITION

Whenever we need to execute same statement again and again in a program, it comes in the category of repetition. This kind of repetition is also called iteration. Repetition of a set of statements in a program
is made possible using loop. for example Look at the program given below:

print("A")
print("A")
print("A")
print("A")
print("A")
print("A")
print("A")
print("A")

The above program is printing “A” eight times, but what if we have to print 100 times or 200 times then it becomes difficult and not even smart work to type the same statement 100 or 200 times. so here loop plays an important role. There are two looping constructs in Python – for and while.

The ‘For’ Loop

The for statement is used to iterate over a range of values or a sequence. While using for loop, it is known in advance the number of times the loop will execute.

Syntax of the For Loop

for [control variable] in [range/sequence]:
       [statements inside body of the loop]
Flow of Control - for loop
Flow of Control – for loop

Example 8: Write a program to accept a word from the user and print all the characters of the word.

st = input("Enter any word : ")
for i in st:
    print(i)

OUTPUT

Enter any word : INDIA
I
N
D
I
A

range( ) function :

It is a built-in function in Python. Syntax of range() function is:

range([start], stop[, step])

It is used to create a list containing a sequence of integers from the given start value upto stop value
(excluding stop value), with a difference of the given step value. for example

list(range(1, 10, 1)) will make a sequence [1, 2, 3, 4, 5, 6, 7, 8, 9] #stop value is excluded

list(range(1, 10, 2)) will make a sequence [1, 3, 5, 7, 9] #here step value 2

list(range(3, 10, 2)) will make a sequence [1, 3, 5, 7, 9] #here start value is 3

list(range(8)) will make a sequence [0, 1, 2, 3, 4, 5, 6, 7] #by default start value is 0 and step value is 1

list(range(10, 5, -1)) will make a sequence [10, 9, 8, 7, 6] #here step is negative

Example 9 : Write a program to print “A” five times

for i in range(5):
     print("A")

OUTPUT:

A
A
A
A
A

Example 10: Write a program to print first seven natural numbers

for i in range(1, 8):
     print(i)

OUTPUT:

1
2
3
4
5
6
7

Example 11 : Write a program to print all the numbers given in the list.

L = [23, 45, 43, 89, 20]

L = [23, 45, 43, 89, 20]
for i in L:
     print(i)

OUTPUT:

23
45
43
89
20

Example 12 : Write a program to print table of 7.

for i in range(7, 77, 7): #in place of 77, we can also write any number less than 77 and more than 70
      print(i)

OR

for i in range(1, 11):
     print(7*i)

OUTPUT:

7
14
21
28
35
42
49
56
63
70

Click for more programs of ‘for’ loop

Click for more practice questions of loops


The ‘while’ Loop

The while statement executes a block of code repeatedly as long as the control condition of the loop is true. When this condition becomes false, the statements in the body of loop are not executed and the control is transferred to the statement immediately following the body of while loop. If the condition of the while loop is initially false, the body is not executed even once.

Syntax of while Loop is given below:

while test_condition:
        body of while
Flow of control-while loop
Flow of control-while loop

Example 13 : Write a program to print first 5 whole numbers using while loop.

i = 0
while i<=4:
    print(i)
    i = i+1

Output:

0
1
2
3
4

Example 14 : Write a program to print table of 7 using while loop.

i = 1
while i<=10:
    print(i * 7)
    i = i+1

OUTPUT

7
14
21
28
35
42
49
56
63
70

Example 15 : Write a program to find the factors of a number using while loop.

num = int(input("Enter any number : "))
print (1, end=' ') #1 is a factor of every number
f = 2
while f <= num :
     if num % f == 0: 
          print(f, end=' ')
     f += 1
 

OUTPUT:
Enter any number : 25
1 5 25

Enter any number : 91
1 7 13 91

Click for more questions on ‘while’ loop

Click for more practice questions of loops


Break and Continue Statement

Loops makes our work very simple for executing the same statement again and again but sometimes such situations occur where we may want to exit from a loop (come out of the loop forever) or skip some statements of the loop before continuing further in the loop. These requirements can be achieved by using break and continue statements, respectively.

Break Statement :

The break statement alters the normal flow of execution as it terminates the current loop and resumes execution of the statement following that loop.

Example 16: Program to demonstrate use of break statement.

for i in range(1,20): #loop work for values 1 to 19
    if i%11==0: #as soon as this condition becomes True than loop terminates
        break #terminating statement
    else:
        print(i)


OUTPUT:

1
2
3
4
5
6
7
8
9
10

Example 17: Write a program to find the sum of all the numbers accepted from the user till the user entered 0(Zero).

n1 = 0
s1 = 0
while True:
     n1 = int(input("Enter any number"))
     if (n1== 0):
          break
     s1 += n1
print("Sum of all numbers = ", s1)

Example 18: Write a program to check whether a number is prime or not.

c = 0
num = int(input("Enter any number : ")) 
if num > 1 :
    for i in range(2, int(num / 2)):
         if (num % i == 0):
              c = 1 
              break
    if c == 1:
         print("It is not a prime number")
    else:
         print("It is a prime number")
else:
    print("Enter another number")

Continue Statement :

When a continue statement is encountered, the control skips the execution of remaining statements inside the body of the loop and jumps to the beginning of the loop for the next iteration.

Example 19: Write a program to print numbers from 1 to 12 except multiples of 5.

for i in range(1,13):
    if i%5==0:
        continue #This statement will return the control to the loop
    else:
        print(i)

OUTPUT:

1
2
3
4
6
7
8
9
11
12

Nested Loop

A loop inside another loop is called a nested loop.

Python does not impose any restriction on how many loops can be nested inside a loop. Any type of loop (for/while) may be nested within another loop (for/while).

Example 20: Write a program to print the following pattern.

1
1  2
1  2  3
1  2  3  4
1  2  3  4  5
for i in range(1, 6):
    for j in range(1, i+1):
        print(j, end=" ")
    print()

Click for more questions on Nested Loop


SUMMARY


1. The if statement is used for selection or decision making.

2. The looping constructs while and for allow sections of code to be executed repeatedly under some condition. for statement iterates over a range of values or a sequence.

3. The statements within the body of for loop are executed till the range of values is exhausted.

4. The statements within the body of a while are executed over and over until the condition of the while is false.

5. If the condition of the while loop is initially false, the body is not executed even once.

6. The statements within the body of the while loop must ensure that the condition eventually becomes
false; otherwise, the loop will become an infinite loop, leading to a logical error in the program.

7. The break statement immediately exits a loop, skipping the rest of the loop’s body. Execution continues
with the statement immediately following the body of the loop. 

8. When a continue statement is encountered, the control jumps to the beginning of the loop for the next iteration.

9. A loop contained within another loop is called a nested loop

Class 11 : NCERT Solutions

Ch1 : Computer System

Ch2 : Number System

Ch3 : Emerging Trends

Ch5 : Getting Started with Python

Ch6 : Flow of Controls

Ch7 : Functions

Ch8 : String

Ch9 : List

Ch10 : Tuples and Dictionaries

Ch11 : Societal Impacts



MCQ of Computer Science Chapter Wise

1. Functions in Python

2. Flow of Control (Loop and Conditional statement)

3. 140+ MCQ on Introduction to Python

4. 120 MCQ on String in Python

5. 100+ MCQ on List in Python

6. 50+ MCQ on Tuple in Python

7. 100+ MCQ on Flow of Control in Python

8. 60+ MCQ on Dictionary in Python


Important Links

100 Practice Questions on Python Fundamentals

120+ MySQL Practice Questions

90+ Practice Questions on List

50+ Output based Practice Questions

100 Practice Questions on String

70 Practice Questions on Loops

70 Practice Questions on if-else


Disclaimer : I tried to give you the simple ”Flow of Control in Python Class 11 Notes” , but if you feel that there is/are mistakes in the code or explanation of “Flow of Control in Python Class 11 Notes“ given above, you can directly contact me at csiplearninghub@gmail.com


Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes

Flow of Control in Python Class 11 Notes


Share with others

Leave a Reply

error: Content is protected !!