You are on page 1of 20

Unit – III

Flow of Control
Definition
• Flow of control determines how statements of program are executed.
Types of flow control
• Sequence: In this type pf flow control each statements of program are
executed in sequence one after the another. It is the simplest form of
flow control. Each statement in the program is executed atleast once.
• Selection: In this type of flow control a condition is checked and
depending on the outcome of condition particular code block is
executed. So, each statement in the program need not necessarily
execute atleast once.
• Repetition: In this type of flow control particular code block may
executes more then one time. It is used to repeat execution of
particular code block for specific number of times, for specific items
in the sequence or until some condition is satisfied.
Selection
• In this type of flow control a condition is checked and depending upon the
outcome of condition particular code block is executed. So, each statement
in the program need not necessarily execute atleast once.
• It is also known as decision making flow control statement.
• Decision making statement first test for the condition and based on the
outcome of that condition or expression allows you to control the flow of
different portion of the program at run time.
• The decision making flow control statement includes:
1. If statement
2. If else statement
3. If..elif…else statement
4. Nested if statement
Selection
• suppose we have `10 to buy a pen. On visiting the
stationery shop, there are a variety of pens priced at
10 each.
• 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.
• Now, suppose we want to display the positive
difference of the two numbers num1 and num2
• Look at the flowchart shown in Figure 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.
If statement
If..else
If..else
If elif else
Number of elif is
dependent on the
number of
conditions to be
checked.
If the first 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
Nested If
3.3.1 Repetition
Often, we repeat a tasks, for example, payment of
electricity bill, which is done every month.
Figure shows the life cycle of butterfly that involves four
stages, i.e., a butterfly lays eggs, turns into a caterpillar,
becomes a pupa, and finally matures
as a butterfly. The cycle starts again with laying of eggs
by the butterfly.
This kind of repetition is also called iteration. Repetition
of a set of statements in a program is made possible
using looping constructs.
For Loop
For Loop

2
0 5
for x in range(6) 8
1
: 11
2
print(x) 14
3 for x in range(2, 30, 3):
4 print(x) 17
5 20
23
26
29
While Loop
While Loop
Nested For Loop
red apple
red banana
adj = ["red", "big", "tasty"] red cherry
fruits = ["apple", "banana", "cherry"] big apple
big banana
for x in adj: big cherry
for y in fruits: tasty apple
print(x, y) tasty banana
tasty cherry
3.4.1 Break
Statement
Break Example
# Use of break statement inside the
loop
s
for val in "string": t
if val == "i": r
break The end
print(val)

print("The end")
3.4.1 Continue Statement
Continue Example

# Program to show the use of continue statement inside loops


s
for val in "string": t
if val == "i": r
continue n
print(val) g
The end
print("The end")

You might also like