Page 1
6.1 Introduct Ion In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as ??ow of control. The ??ow of control can be
implemented using control structures. Python supports
two types of control structures—selection and repetition.
“Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
code is properly indented.”
– G. van Rossum
Chapter 6
Flow of Control
In this chapter
» Introduction to Flow
of Control
» Selection
» Indentation
» Repetition
» Break and Continue
Statements
» Nested Loops
Ch 6.indd 121 08-Apr-19 12:37:51 PM
Reprint 2025-26
Page 2
6.1 Introduct Ion In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as ??ow of control. The ??ow of control can be
implemented using control structures. Python supports
two types of control structures—selection and repetition.
“Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
code is properly indented.”
– G. van Rossum
Chapter 6
Flow of Control
In this chapter
» Introduction to Flow
of Control
» Selection
» Indentation
» Repetition
» Break and Continue
Statements
» Nested Loops
Ch 6.indd 121 08-Apr-19 12:37:51 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 122
Program 6-1 Program to print the difference of two
numbers.
#Program 6-1
#Program to print the di??erence of two input numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
di?? = num1 - num2
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number 5
Enter second number 7
The di??erence of 5 and 7 is -2
6.2 Select Ion Now suppose we have `10 to buy a pen. On visiting the
stationery shop, there are a variety of pens priced at
`10 each. Here, we have to decide which pen to buy.
Similarly, when we use the direction services of a digital
map, 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.
Now, suppose we want to
display the positive difference of
the two numbers num1 and num2
given at program 6-1. For that,
we need to modify our approach.
Look at the ??owchart shown in
Figure 6.2 that subtracts the
smaller number from the bigger
number so that we always get a positive difference. This
selection is based upon the values that are input for the
two numbers num1 and num2.
Figure 6.2: Flow chart depicting decision making
Ch 6.indd 122 08-Apr-19 12:37:52 PM
Reprint 2025-26
Page 3
6.1 Introduct Ion In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as ??ow of control. The ??ow of control can be
implemented using control structures. Python supports
two types of control structures—selection and repetition.
“Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
code is properly indented.”
– G. van Rossum
Chapter 6
Flow of Control
In this chapter
» Introduction to Flow
of Control
» Selection
» Indentation
» Repetition
» Break and Continue
Statements
» Nested Loops
Ch 6.indd 121 08-Apr-19 12:37:51 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 122
Program 6-1 Program to print the difference of two
numbers.
#Program 6-1
#Program to print the di??erence of two input numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
di?? = num1 - num2
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number 5
Enter second number 7
The di??erence of 5 and 7 is -2
6.2 Select Ion Now suppose we have `10 to buy a pen. On visiting the
stationery shop, there are a variety of pens priced at
`10 each. Here, we have to decide which pen to buy.
Similarly, when we use the direction services of a digital
map, 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.
Now, suppose we want to
display the positive difference of
the two numbers num1 and num2
given at program 6-1. For that,
we need to modify our approach.
Look at the ??owchart shown in
Figure 6.2 that subtracts the
smaller number from the bigger
number so that we always get a positive difference. This
selection is based upon the values that are input for the
two numbers num1 and num2.
Figure 6.2: Flow chart depicting decision making
Ch 6.indd 122 08-Apr-19 12:37:52 PM
Reprint 2025-26
Flow o F Control 123
The syntax of if statement is:
if condition:
statement(s)
In the following example, if the age entered by the
user is greater than 18, then print that the user is
eligible to vote. If the condition is true, then the indented
statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no
limit on the number of statements that can appear as a
block under the if statement.
Example 6.1
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")
A variant of 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.
if condition:
statement(s)
else:
statement(s)
Let us now modify the example on voting with the
condition that if the age entered by the user is greater
than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Now let us use the same concept to modify program
6-1, so that it always gives a positive difference as the
output. From the ??ow chart in Figure 6.2, it is clear that
we need to decide whether num1 > num2 or not and
take action accordingly.
We have to specify two blocks of statements since
num1 can be greater than num2 or vice-versa as shown
in program 6-2.
Many a times there are situations that require
multiple conditions to be checked and it may lead to
many alternatives. In such cases we can chain the
conditions using if..elif (elif means else..if).
n ote S
Ch 6.indd 123 08-Apr-19 12:37:52 PM
Reprint 2025-26
Page 4
6.1 Introduct Ion In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as ??ow of control. The ??ow of control can be
implemented using control structures. Python supports
two types of control structures—selection and repetition.
“Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
code is properly indented.”
– G. van Rossum
Chapter 6
Flow of Control
In this chapter
» Introduction to Flow
of Control
» Selection
» Indentation
» Repetition
» Break and Continue
Statements
» Nested Loops
Ch 6.indd 121 08-Apr-19 12:37:51 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 122
Program 6-1 Program to print the difference of two
numbers.
#Program 6-1
#Program to print the di??erence of two input numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
di?? = num1 - num2
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number 5
Enter second number 7
The di??erence of 5 and 7 is -2
6.2 Select Ion Now suppose we have `10 to buy a pen. On visiting the
stationery shop, there are a variety of pens priced at
`10 each. Here, we have to decide which pen to buy.
Similarly, when we use the direction services of a digital
map, 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.
Now, suppose we want to
display the positive difference of
the two numbers num1 and num2
given at program 6-1. For that,
we need to modify our approach.
Look at the ??owchart shown in
Figure 6.2 that subtracts the
smaller number from the bigger
number so that we always get a positive difference. This
selection is based upon the values that are input for the
two numbers num1 and num2.
Figure 6.2: Flow chart depicting decision making
Ch 6.indd 122 08-Apr-19 12:37:52 PM
Reprint 2025-26
Flow o F Control 123
The syntax of if statement is:
if condition:
statement(s)
In the following example, if the age entered by the
user is greater than 18, then print that the user is
eligible to vote. If the condition is true, then the indented
statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no
limit on the number of statements that can appear as a
block under the if statement.
Example 6.1
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")
A variant of 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.
if condition:
statement(s)
else:
statement(s)
Let us now modify the example on voting with the
condition that if the age entered by the user is greater
than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Now let us use the same concept to modify program
6-1, so that it always gives a positive difference as the
output. From the ??ow chart in Figure 6.2, it is clear that
we need to decide whether num1 > num2 or not and
take action accordingly.
We have to specify two blocks of statements since
num1 can be greater than num2 or vice-versa as shown
in program 6-2.
Many a times there are situations that require
multiple conditions to be checked and it may lead to
many alternatives. In such cases we can chain the
conditions using if..elif (elif means else..if).
n ote S
Ch 6.indd 123 08-Apr-19 12:37:52 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 124
Program 6-2 Program to print the positive difference
of two numbers.
#Program 6-2
#Program to print the positive di??erence of two numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
di?? = num1 - num2
else:
di?? = num2 - num1
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number: 5
Enter second number: 6
The di??erence of 5 and 6 is 1
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 6.2 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")
Example 6.3 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":
Ch 6.indd 124 08-Apr-19 12:37:52 PM
Reprint 2025-26
Page 5
6.1 Introduct Ion In Figure 6.1, we see a bus carrying the children to
school. There is only one way to reach the school. The
driver has no choice, but to follow the road one milestone
after another to reach the school. We learnt in Chapter
5 that this is the concept of sequence, where Python
executes one statement after another from beginning to
the end of the program. These are the kind of programs
we have been writing till now.
Figure 6.1: Bus carrying students to school
Let us consider a program 6-1 that executes in
sequence, that is, statements are executed in an order
in which they are written.
The order of execution of the statements in a program
is known as ??ow of control. The ??ow of control can be
implemented using control structures. Python supports
two types of control structures—selection and repetition.
“Don't you hate code that's
not properly indented?
Making it [indenting] part of
the syntax guarantees that all
code is properly indented.”
– G. van Rossum
Chapter 6
Flow of Control
In this chapter
» Introduction to Flow
of Control
» Selection
» Indentation
» Repetition
» Break and Continue
Statements
» Nested Loops
Ch 6.indd 121 08-Apr-19 12:37:51 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 122
Program 6-1 Program to print the difference of two
numbers.
#Program 6-1
#Program to print the di??erence of two input numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
di?? = num1 - num2
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number 5
Enter second number 7
The di??erence of 5 and 7 is -2
6.2 Select Ion Now suppose we have `10 to buy a pen. On visiting the
stationery shop, there are a variety of pens priced at
`10 each. Here, we have to decide which pen to buy.
Similarly, when we use the direction services of a digital
map, 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.
Now, suppose we want to
display the positive difference of
the two numbers num1 and num2
given at program 6-1. For that,
we need to modify our approach.
Look at the ??owchart shown in
Figure 6.2 that subtracts the
smaller number from the bigger
number so that we always get a positive difference. This
selection is based upon the values that are input for the
two numbers num1 and num2.
Figure 6.2: Flow chart depicting decision making
Ch 6.indd 122 08-Apr-19 12:37:52 PM
Reprint 2025-26
Flow o F Control 123
The syntax of if statement is:
if condition:
statement(s)
In the following example, if the age entered by the
user is greater than 18, then print that the user is
eligible to vote. If the condition is true, then the indented
statement(s) are executed. The indentation implies that
its execution is dependent on the condition. There is no
limit on the number of statements that can appear as a
block under the if statement.
Example 6.1
age = int(input("Enter your age "))
if age >= 18:
print("Eligible to vote")
A variant of 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.
if condition:
statement(s)
else:
statement(s)
Let us now modify the example on voting with the
condition that if the age entered by the user is greater
than 18, then to display that the user is eligible to vote.
Otherwise display that the user is not eligible to vote.
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Now let us use the same concept to modify program
6-1, so that it always gives a positive difference as the
output. From the ??ow chart in Figure 6.2, it is clear that
we need to decide whether num1 > num2 or not and
take action accordingly.
We have to specify two blocks of statements since
num1 can be greater than num2 or vice-versa as shown
in program 6-2.
Many a times there are situations that require
multiple conditions to be checked and it may lead to
many alternatives. In such cases we can chain the
conditions using if..elif (elif means else..if).
n ote S
Ch 6.indd 123 08-Apr-19 12:37:52 PM
Reprint 2025-26
Computer SCien Ce – Cla SS xi 124
Program 6-2 Program to print the positive difference
of two numbers.
#Program 6-2
#Program to print the positive di??erence of two numbers
num1 = int(input("Enter ??rst number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
di?? = num1 - num2
else:
di?? = num2 - num1
print("The di??erence of",num1,"and",num2,"is",di??)
Output:
Enter ??rst number: 5
Enter second number: 6
The di??erence of 5 and 6 is 1
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 6.2 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")
Example 6.3 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":
Ch 6.indd 124 08-Apr-19 12:37:52 PM
Reprint 2025-26
Flow o F Control 125
print("Be Slow")
elif signal == "green" or signal == "GREEN":
print("Go!")
Number of elif is dependent on the number of
conditions to be checked. If the ??rst condition is false,
then the next condition is checked, and so on. If one of
the conditions is true, then the corresponding indented
block executes, and the if statement terminates.
Let us write a program to create a simple calculator
to perform basic arithmetic operations on two numbers.
The program should do the following:
• Accept two numbers from the user.
• Ask user to input any of the operator (+, -, *, /).
An error message is displayed if the user enters
anything else.
• Display only positive difference in case of the
operator "-".
• Display a message “Please enter a value other
than 0” if the user enters the second number as
0 and operator ‘/’ is entered.
Program 6-3 Write a program to create a simple
calculator performing only four basic
operations.
#Program to create a four function calculator
result = 0
val1 = ??oat(input("Enter value 1: "))
val2 = ??oat(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)
Ch 6.indd 125 08-Apr-19 12:37:52 PM
Reprint 2025-26
Read More