(iii) Flow of Control (Sequence,Conditional, Iterative Statements)
(iii) Flow of Control (Sequence,Conditional, Iterative Statements)
CONTROL FLOW OF
STATEMENTS
Flow of Control is referred as the
way/ order of the flow of
execution of the program
statements.
Types of constructs :
1) Sequence
2)
Selection/Conditional/Decisio
nal
3) Iteration/Looping
For
loop,while loop
1. SEQUENCE CONSTRUCT /
SEQUENTIAL FLOW OF CONTROL
• Sequence construct
means statement are
executed sequentially. Statement 1
• It represents the default flow of
statements.
Statement 2
• Every program begins with the first
statement of the program. When
the final statement of the program
is executed, the program is done. Statement 3
2. SELECTION / DECISIONAL / CONDITIONAL CONSTRUCT
or SELECTION FLOW OF CONTROL
• The Selection construct means the execution of statement(s)
depending upon a condition-test.
• If a condition evaluates to true, a course-of-action(a set of
statements) is followed otherwise another course-of-action (a
different set of statements).
• It helps in making decision about which set-of-statements is to be
executed.
NO
Condition ? Statement 1 Statement 2
YES
Statement 1
Statement 2
3. ITERATION CONSTRUCT /
LOOPING CONSTRUCT /
ITERATIVE FLOW OF CONTROL
Iteration construct means repetition
of set of statements depending upon a
condition test till the time of condition is
true ( or false depending upon the loop)
A set of statements are repeated False
again and again. As soon as the Condition?
condition become false (or true), the
repetition stops.
The iteration construct is also called
True
“Looping Construct”.
Eg. for loop , while loop
Statement 1 The
Loop
Body
Statement 2
SELECTION/ CONDITIONAL/
DECISION MAKING
CONSTRUCT
There are three types of decision making
statement.
1. if statements
2. if-else statements
3. Nested if-else statement
if
If statement must be provided with a condition.
statements
If the condition is True, the indented body / block gets
executed.
If the condition is False, then the control doesn’t execute
the if body/block.
Condition
if statements examples
contd.
if statements examples
contd.
Note:
To indicate a block of
code in Python,
you must
indent each line of the
block by the same
amount. In above e.g.
both print statements
are part of if condition
because of both are at
same level indented.
HANDS ON….FOR YOU
Programs on only if
Q1) Write a program to accept
three integers and print the
largest of the three. Make use of
only if statement.
if-else statements
This form of if statement tests a condition and if the
condition evaluates to true, it carries out statements
indented below if and in case condition evaluates to false,
it carries out statements indented below else.
NOTE:
Conditions
are provided
only with if
and not with
the else
OUTPUT clause.
COMPARISON BETWEEN
only if statements and if-else statements
If-else statements are
MORE EFFICIENT
BECAUSE NO. OF
COMPARISONS AND
CONDITION CHECKS
ARE LESS THAN ONLY if
statements.
Both
programs will
give the same
output.
if-elif statements
Sometimes, we need to check another condition in case the test-
condition of if evaluates to false. That is , we want to check a
condition when control reaches else part i.e. condition test in
the form of else if.
Python syntax ( if-elif
statements)
if <condition expression>:
statement
if <condition expression>:
[statements]
statement
elif <condition expression>:
[statements]
statement
elif <condition
[statements]
expression>:
else:
statement
statement
[statements]
[statements]
Python program to
check whether a
no. taken from the
user is
Positive,Negative
or Zero(EXAMPLE
of if-elif
OUTPUT statements)
Python program on
cricketer’s score.
Check for
century,fifty or less
than 50 (EXAMPLE
OUTPUT
of if-elif
statements)
MORE PROGRAMS using if-elif
OUTPUT
The nested if statement
A nested if is an if that has another if in its if’s
body or in elif’s body or in its else’s body.
The nested if can have one of the following
forms:
Form1 (if inside if’s body)
Form2 (if inside elif’s body)
Form3 (if inside else’s body
Form 4 (if inside if’s as well as else’s or elif’s body, i.e.
elif’s body , i.e. multiple ifs inside.)
Form if <condition expression>:
Form 2
statement
1 [statements]
if <condition expression>:
if <condition expression>: elif <condition
expression>: if <condition expression>:
if <condition statement(s)
expression>: if <condition expression>:
[statements] else:
[state
else: statement(s)
ments
] [statements] elif <condition expression>:
else: else: if <condition expression>:
[state statement statement(s)
ments [statements] else:
if <condition expression>:
] statement statement(s)
elif <condition [statements] else:
expression>: elif <condition expression>: if <condition expression>:
Statement statement statement(s)
[Statements] [statements] else:
else: else:
statement(s)
Statement if <condition expression>:
[Statements]
Form 3 statement(s)
else: Form
statement(s) 4
EXAMPLE PROGRAM on nested if : SORT 3
numbers
When x is minimum
When y is minimum
When z is minimum
format () Function
format() function is used with Strings for presenting the output in a desired format.
In format() function,
< symbol indicates LEFT alignment
>
S
y
m
b
o
l
i
n
d
i
c
a
t
e
s
Form 2
Compound Interest=TotalAmount-Principal
where , TotalAmount=P*(1+r/n)t*n
ITERATIVE
or
CONSTRUCT
LOOPING CONSTRUCT
Following are the Looping constructs:-
1. For loop
2. While loop
TYPES OF LOOPING STATEMENTS
COUNTING LOOPS: DUMBO, move 10 times
in the loop on the road
the loops that repeat a
certain number of
times
Eg. for loop
OUTPUT
OUTPUT
NOTE: If end=‘ ‘ is removed , then the output will be shown in vertical order by default.
PROGRAM CODE OUTPUT
PROGRAM CODE OUTPUT
PROGRAM CODE OUTPUT
PROGRAM CODE OUTPUT
While Loop
A while loop is a conditional loop that will
repeat the instructions within itself as long as a
condition remains true .
Test
while <logicalexpression> : False
condition/
loop body expression
Where the loop-body may contain a single
statement or multiple statements or an empty
statement (i.e. pass statement). The loop True
iterates while the logical expression evaluates
to true. When the expression becomes false,
the program control passes to the line after the
loop-body. Body of
EXAMPLE: while
INITIALIZATION Statement
CONDITION Statement
Exit from
BODY OF THE LOOP
loop
UPDATION Statement
PROGRAM CODE OUTPUT
TASK: Write codes for the following series in IDE ??
SERIES : Using for loop Using while loop
1 2 3 4 5 6…….n
5 10 15 20..…200
1 4 7 10 13 … 40
S=1+x+x2+x3+x4+…..xn
PROGRAM: Sum of n Natural Numbers PROGRAM: Factorial of a Number
PROGRAM: Find the REVERSE of a no. and check whether it is a PALINDROME or not.
PROGRAM: Print FIBONACCI SERIES to n no. of Terms
HINT:
i) When a smaller no. is divided by a greater no. to calculate its remainder(modulo %) then the
smaller is the answer as its remainder. For eg. 1%4 gives 1 as output.
ii) Break brings the control out of the loop.
iii) Continue takes the control for the next iteration.
Loop Else clause
The else clause of a Python loop executes when the loop
terminates normally,i.e., when test condition results into false for a
while loop or for loop has executed for the last value in the
sequence; and not when the break statement terminates the
loop.
NOTE:
The else clause of a loop appears at the same indentation as that
of loop keyword while or for.
The loop else suite executes only in the normal termination of
loop.
Loop else clause works when the loop is terminating normally-
after the last element of sequence in for loop and when the test-
condition becomes false in while loop.
PROGRAM to show the execution PROGRAM to show the skip of Loop Else
of Loop Else Clause Clause due to presence of break
NESTED LOOPS
A loop containing another loop
inside its body is known as nested loop.
Note:
In a nested loop, inner loop(or
nested loop) must be terminated before
the outer loop(enclosing loop).
NOTE: First time when the control enters the outer loop, then with that value it enters the
inner loop. Until the inner loop doesn’t get over, the outer loop does not take the next value
from the range of outer loop. Again, when it enters the outer loop with next value from its
range, then with that value it starts the inner loop afresh and continues inner loop till its
range gets over. This process is repeated until the outer loop gets exhausted with all its values
in the sequence/range ,except any accidental termination(i.e. encountering break statement)
PROGRAM CODE OUTPUT
PROGRAM CODE OUTPUT
PROGRAM CODE OUTPUT
NOTE:
ord() function is used to get the ASCII value of a character
chr() function is used to get the equivalent character of the ASCII no.
NOTE:
ASCII stands for American Standard Code for Information Interchange.
Bibliograhy and References
• Google
• Wikipedia
• XI Computer Science , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
• XI Informatics Practices , By Sumita Arora,
Dhanpat Rai Publication 2020 Edition
Programming is
an ART….
Add your colours to it and
make your own