Flow of Control in Python Class 11 Notes, Flow of Control helps to make decisions based on the condition given. This note explains the conditional statements (if, if-else) and loops (for, while) in Python with examples.
Flow of Control in Python
A program runs step by step from top to bottom. But sometimes, we need to change the flow due to making decisions, repeating actions, or skipping some parts. For example,
- If-else: Allows the program to decide based on a condition.
- Loops (for, while): Repeat actions multiple times.
- Functions: Jump to reusable blocks of code.
Use of indentation
Indentation helps to organize the code and makes it easier to read. Python relies on spaces or tabs at the beginning of the code line.
Why is indentation important?
- Indicates code blocks: Used in loops, functions, and if-else statements.
- Improves readability: Makes the code neat and easy to follow.
- Prevents errors: Incorrect indentation causes IndentationError.
if True:
print("Indented correctly!") # Space before the print statement is known as Indentation
Conditional and Iterative flow
There are two important concepts: conditional flow and iterative flow, which control the execution of a program.
Conditional Flow
It allows a program to make decision based on the certain condition like if, if-else and elif.
age = 18
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Iterative Flow
It helps a program repeat actions multiple times using loops like for and while.
for i in range(5):
print("Iteration:", i) # Runs 5 times
Conditional statements
Conditional statements in Python allow you to make decisions based on the given conditions. These statements are important for controlling the flow of a program. The conditional statements in python are:
- If – Else Statement
- Elif Statement
- Nested If Statement
Flowchart Representation of if-else

If – Else Statement
If statement called if..else statement allows us to write two alternative paths and the control condition determines which path gets executed. The syntax for if..else statement is as follows.
The syntax of if statement is:
if condition:
statement(s)
else:
statement(s)
Q. Write a program to find absolute number using if statement.
num = -10
if num < 0:
num = -num
print("Absolute value:", num)
Output:
Absolute value: 10
Q. Write a program to sort 3 numbers and divisibility using if statement.
num1 = 37
num2 = 33
num3 = 59
# Sorting using if statements
if num1 > num2:
temp = num1
num1 = num2
num2 = temp
if num2 > num3:
temp = num2
num2 = num3
num3 = temp
if num1 > num2:
temp = num1
num1 = num2
num2 = temp
print("Sorted numbers:", num1, num2, num3)
Output:
Sorted numbers: 33 37 59
Q. Write a program to accept person age from the user and check weather person is eligible for vote or not.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Output:
Enter your age: 21
Eligible to vote
Note – The int() code is used to convert string values to integer values since input codes only accept input in the form of character (Strings).
Q. Program to print the positive difference of two numbers.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
diff = num1 - num2
else:
diff = num2 - num1
print("The difference of",num1,"and",num2,"is",diff)
Output:
Enter first number: 6
Enter second number: 9
The difference of 6 and 9 is 3
Elif Statement
You can build a chain of if statements using the elif statement. Before the end of the elif chain is reached or one of the if expressions is true, the if statements are evaluated one at a time. If there is no true expression at the end of the elif chain, then else statement will be execuited.
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)
Q. Check whether a number is positive, negative, or zero.
number = int(input("Enter a number: "))
if number > 0:
print("Number is positive")
elif number < 0:
print("Number is negative")
else:
print("Number is zero")
Output:
Enter a number: 6
Number is positive
Q. Display the appropriate message as per the colour of signal at the road crossing.
signal = input("Enter the colour: ")
if signal == "red" or signal == "RED":
print("STOP")
elif signal == "orange" or signal == "ORANGE":
print("Be Slow")
elif signal == "green" or signal == "GREEN":
print("Go!")
Output:
Enter the colour: orange
Be Slow
Q. Write a program to create a simple calculator performing only four basic operations.
result = 0
val1 = float(input("Enter value 1: "))
val2 = float(input("Enter value 2: "))
op = input("Enter any one of the operator (+,-,*,/): ")
if op == "+":
result = val1 + val2
elif op == "-":
if val1 > val2:
result = val1 - val2
else:
result = val2 - val1
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Error! Division by zero is not allowed. Program terminated")
else:
result = val1/val2
else:
print("Wrong input,program terminated")
print("The result is ",result)
Output:
Enter value 1: 8
Enter value 2: 9
Enter any one of the operator (+,-,*,/): +
The result is 17.0
Indentation
The statements included within a block are typically enclosed in curly brackets in programming languages. Python, however, uses indentation for both block and nested block structures. Indentation is the practise of placing leading whitespace (spaces and tabs) at the start of a sentence.
Q. Python program to find the larger of two given numbers using an if statement:
num1 = 5
num2 = 6
if num1 > num2: #Block1
print("first number is larger")
print("Bye")
else: #Block2
print("second number is larger")
print("Bye Bye")
Output:
Second number is larger
Bye Bye
Repetition
The ability to repeatedly run a group of statements in a programme based on a condition is provided by looping constructs. While a certain logical condition is true, the statements in a loop are repeated again.
For Loop
The for statement allows you to specify how many times a statement or compound statement should be repeated. A for statement’s body is executed one or more times until an optional condition is met.
Syntax of the For Loop
for variable in iterable:
# Code to execute in each iteration
Q. Program to print the characters in the string ‘PYTHON’ using for loop.
for letter in 'PYTHON':
print(letter)
Output:
P
Y
T
H
O
N
Q. Program to print the numbers in a given sequence using for loop.
count = [10,20,30,40,50]
for num in count:
print(num)
Output:
10
20
30
40
50
Q. Write a program to find the factorial of a positive number.
num = int(input("Enter a number: "))
factorial = 1
if num >= 0:
for i in range(1, num + 1):
factorial *= i
print(f"Factorial of {num} is {factorial}")
else:
print("Factorial is not defined for negative numbers")
Output:
Enter a number: 10
Factorial of 10 is 3628800
Q. Program to print even numbers in a given sequence using for loop.
numbers = [1,2,3,4,5,6,7,8,9,10]
for num in numbers:
if (num % 2) == 0:
print(num,'is an even Number')
Output:
2 is an even Number
4 is an even Number
6 is an even Number
8 is an even Number
10 is an even Number
The Range() Function
The range() is a built-in function in Python. Syntax of range() function is:
range(start, stop, step)
It is used to generate a list of integers with a difference equal to the specified step value that runs from the given start value to the specified stop value (excluding the stop value). The function range() is often used in for loops for generating a sequence of numbers.
Q. Print the number from 1 to 10.
print("Numbers from 1 to 10:")
for num in range(1, 11):
print(num)
Output:
Numbers from 1 to 10:
1
2
3
4
5
6
7
8
9
10
Q. Print the even numbers from 0 to 20.
print("\nEven numbers from 0 to 20:")
for num in range(0, 21, 2):
print(num)
Output:
Even numbers from 0 to 20:
0
2
4
6
8
10
12
14
16
18
20
Q. Print the number from 10 to 1.
print("\nCountdown from 10 to 1:")
for num in range(10, 0, -1):
print(num)
Output:
Countdown from 10 to 1:
10
9
8
7
6
5
4
3
2
1
Q. Create a list of multiples of 5 up to 50.
multiples_of_five = list(range(5, 51, 5))
print("\nMultiples of 5 up to 50:", multiples_of_five)
Output:
Multiples of 5 up to 50: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
Q. Program to print the multiples of 10 for numbers in a given range.
for num in range(5):
if num > 0:
print(num * 10)
Output:
10
20
30
40
The ‘While’ Loop
The while loop statement evaluates the condition before executing the loop body. If the condition becomes true, then the loop body will execute; otherwise, the loop exits.
Syntax of while Loop
while condition:
# Code will execute while the condition is True
Q. Program to print first 5 natural numbers using while loop.
count = 1
while count <= 5:
print(count)
count += 1
Output:
1
2
3
4
5
Q. Program to find the factors of a whole number using while loop.
num = int(input("Enter a number to find its factor: "))
print (1, end=' ')
factor = 2
while factor <= num/2 :
if num % factor == 0:
print(factor, end=' ')
factor += 1
print (num, end=' ')
Output:
Enter a number to find its factor: 10
1 2 5 10
Break and Continue Statement
Using looping techniques, programmers can efficiently repeat tasks. When a specific condition is met, there are times when we may want to end a loop (coming out of the loop indefinitely) or skip a few of its statements before proceeding. Use of break and continue statements, respectively, can satisfy these criteria. These statements are provided by Python as a tool to provide the programmer additional control over how a programme is executed.
Break Statement
The break statement modifies the normal course of execution by ending the current loop and continuing with the statement that follows it.
Q. Program to demonstrate use of break statement.
num = 0
for num in range(10):
num = num + 1
if num == 8:
break
print('Num has value ' + str(num))
print('Encountered break!! Out of loop')
Output:
Num has value 1
Num has value 2
Num has value 3
Num has value 4
Num has value 5
Num has value 6
Num has value 7
Encountered break!! Out of loop
Q. Find the sum of all the positive numbers entered by the user. As soon as the user enters a neagtive number, stop taking in any further input from the user and display the sum .
entry = 0
sum1 = 0
print("Enter numbers to find their sum, negative number ends the loop:")
while True:
entry = int(input())
if(entry < 0):
break
sum1 += entry
print("Sum =", sum1)
Output:
Enter numbers to find their sum, negative number ends the loop:
15
24
-1
Sum = 39
Q. Program to check if the input number is prime or not.
num = int(input("Enter the number to be checked: "))
flag = 0
if num > 1 :
for i in range(2, int(num / 2)):
if (num % i == 0):
flag = 1
break
if flag == 1:
print(num , "is not a prime number")
else:
print(num , "is a prime number")
else:
print("Entered number is <= 1, execute again!")
Output:
Enter the number to be checked: 7
7 is a prime number
Continue Statement
When a continue statement is found, the control goes to the beginning of the loop for the following iteration instead of executing any leftover statements in the loop’s body for the current iteration. The loop is restarted if the condition that initiated it is still true; else, control is passed to the statement that comes before the loop.
Q. Program to demonstrate the use of continue statement.
num = 0
for num in range(6):
num = num + 1
if num == 3:
continue
print('Num has value ' + str(num))
print('End of loop')
Output:
Num has value 1
Num has value 2
Num has value 4
Num has value 5
Num has value 6
End of loop
Nested Loops
A loop may contain another loop inside it. 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 or on the levels of nesting. Any type of loop (for/while) may be nested within another loop (for/while).
Q. Program to demonstrate working of nested for loops.
for var1 in range(3):
print( "Iteration " + str(var1 + 1) + " of outer loop")
for var2 in range(2):
print(var2 + 1)
print("Out of inner loop")
print("Out of outer loop")
Output:
Iteration 1 of outer loop
1
2
Out of inner loop
Iteration 2 of outer loop
1
2
Out of inner loop
Iteration 3 of outer loop
1
2
Out of inner loop
Out of outer loop
Q. Program to print the pattern for a number input by the user.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
num = int(input("Enter a number to generate its pattern = "))
for i in range(1,num + 1):
for j in range(1,i + 1):
print(j, end = " ")
print()
Output:
Enter a number to generate its pattern = 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Q. Program to print the pattern for a number input by the user.
12345 1234 123 12 1
rows = 5
for i in range(1, rows + 1):
for j in range(rows + 1 - i, 0, -1):
print(j, end="")
print()
Q. Program to print the pattern for a number input by the user.
A AB ABC ABCD ABCDE
rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(chr(65 + j), end="")
print()
Q. Program to find prime numbers between 2 to 50 using nested for loops.
num = 2
for i in range(2, 50):
j= 2
while ( j <= (i/2)):
if (i % j == 0):
break
j += 1
if ( j > i/j):
print ( i, "is a prime number")
print ("Bye Bye!!")
Output:
2 is a prime number
3 is a prime number
5 is a prime number
7 is a prime number
11 is a prime number
13 is a prime number
17 is a prime number
19 is a prime number
23 is a prime number
29 is a prime number
31 is a prime number
37 is a prime number
41 is a prime number
43 is a prime number
47 is a prime number
Bye Bye!!
Q. Write a program to calculate the factorial of a given number.
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, num + 1):
fact = fact * i
print("factorial of ", num, " is ", fact)
Output:
Enter a number: 9
factorial of 9 is 362880
Computer Science Class 11 Notes important links
- Computer Systems and Organisation
- Introduction to problem solving
- Getting Started with Python
- Flow of Control statements in Python
- String in Python
- Lists in Python
- Tuples in Python
- Dictionary in Python
- Society, Law and Ethics
Disclaimer: We have taken an effort to provide you with the accurate handout of “Flow of Control in Python Class 11 Notes“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 11 CBSE Textbook, Sample Paper, Old Sample Paper, Board Paper, NCERT Textbook and Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in