0% found this document useful (0 votes)
21 views

Final Revised Conditional and Iterative Stmnts 2020-21

The document discusses different types of control flow statements in Python including sequential, conditional, and iterative statements. It provides examples of if, if-else, and if-elif-else conditional statements to make decisions in a program. It also discusses for and while loop statements that allow iterative execution of blocks of code. Various examples of programs that can be written using these control flow statements are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Final Revised Conditional and Iterative Stmnts 2020-21

The document discusses different types of control flow statements in Python including sequential, conditional, and iterative statements. It provides examples of if, if-else, and if-elif-else conditional statements to make decisions in a program. It also discusses for and while loop statements that allow iterative execution of blocks of code. Various examples of programs that can be written using these control flow statements are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Know Python Bytes

w w w. k n o w p y t h o n b y te s . b l o g s p o t . c o m
Mrs. Payal Bhattacharjee, PGT(C.Sc.)
K V No.1 Kanchrapara
KVS-RO(Kolkata)
CONTENTS
( Learning Outcomes ) 
XI CS 2020-21
FLOW OF CONTROL
Types of Statements
INTRODUCTION TO FLOW OF CONTROL
Conditional statements: if, if-else, if-elif-else;
simple programs: Eg.
: absolute value : sort 3 numbers : divisibility of a number.
 Notion of iterative computation and control flow:
for(range(),len()), while
 Suggested programs:
: calculation of simple and compound interests,
: finding the factorial of a positive number etc.
 Some more extra program codes to strengthen your programming.
 using flowcharts (Covered in Computational Thinking and Problem Solving topic)
XI IP 2020-21
Control Statements: if-else, for loop
TYPES OF STATEMENTS in Python
Statements are the instructions given to computer to perform any
kind of action viz. data movements, making decision, repeating actions
Statements form the smallest executable unit within a program.
• Empty statement/ null operation statement
– Statement which does nothing. It is as follows: 
• Simple statement (Single statement)
– Any single executable statement is a simple statement.
• Compound statement
A group of statements executed as a unit is a compound statement.
A compound statement has:
(i) a header line which begins with a keyword and ends with a colon.
(ii) a body consisting of one or more Python statements, each indented
inside the header line. All the statements are at the same level of
indentation.
Header Line
COMPOUND
BODY STATEMENT
FLOW OF CONTROL OR
CONTROL STRUCTURES OR

CONTROL FLOW OF
STATEMENTS
Flow of Control is referred as the
way/ order of the flow of execution
of the program statements.

In a program, statement may be executed


sequentially, selectively or iteratively.
Every program
• Every program language
language provides
provides or
or supports the
supports the following following :-

Types of constructs :
1) Sequence

2) Selection/Conditional/Decisional

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 again False
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 statements
 If statement must be provided with a condition.
 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 expression>:
[statements]
statement
else:
[statements]
statement
[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.)
if <condition expression>:
Form Form 2
statement
1 [statements]
if <condition expression>:
if <condition expression>: elif <condition expression>:
if <condition expression>:
if <condition if <condition expression>:
statement(s)
expression>: [statements]
else: else:
[statements]
[statements] statement(s)
else:
else: elif <condition expression>:
[statements]
statement if <condition expression>:
elif <condition
[statements] statement(s)
expression>:
if <condition expression>: else:
Statement
statement statement(s)
[Statements]
[statements] else:
else:
elif <condition expression>: if <condition expression>:
Statement
statement statement(s)
[Statements] [statements] else:
else:
statement(s)
if <condition expression>:
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
> Symbol indicates RIGHT alignment
Total 25 characters including spaces
after printing SALES:
Right alignment for > symbol
.2 denotes no. of characters after
decimal.
Here, 2 digits(characters) after
decimal .
f denotes float value
d denotes integer value
Form 2

PROGRAM: To calculate SIMPLE Interest


and COMPOUND Interest .
Simple Interest=Principal*Rate*Time/100

Compound Interest=TotalAmount-Principal
where , TotalAmount=P*(1+r/n)t*n
ITERATIVE CONSTRUCT
or
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

CONDITIONAL LOOPS: DUMBO, move


in the loop on
the loops that repeat the road till
until a certain thing you meet
JERRY
happens i.e. they keep
repeating as long as the
condition is true
Eg. while loop
The range() function
 The range() function in Python generates a list
which is a special sequence type.
 A sequence in Python is a succession of values
bound together by a single name.
 Some Python sequence types/ iterables are:
strings, lists, tuples etc.

The range() function is used with for loop in Python.


The working of range() function
range( <lower limit>,<upper limit>)
The function in the form range(L,U) will produce a list having
values starting from L,L+1,L+2 ……..(U-1)
# L and U being integers
# both limits should be integers

Examples of range() function


Command OUTPUT
range(0,5) [0,1,2,3,4]
range(5,0) Empty list [ ]
range(12,18) [12,13,14,15,16,17]
Default step value in values is +1
Variation in range() function
range(<lower limit>,<upper limit>,<step value>)
# all value should be integers
range(L,U,s)
# will produce a list having values as L,L+s,L+2s,….<=U-1

STATEMENT VALUES GENERATED / OUTPUT


range(10) 0,1,2,3,4,5,6,7,8,9
range(5,10) 5,6,7,8,9
range(3,7) 3,4,5,6
range(5,15,3) 5,8,11,14
range(9,3,-1) 9,8,7,6,5,4
range(10,1,-2) 10,8,6,4,2
The for Loop
This determines how many
times the loop will get repeated

for <variable> in <sequence> : Colon : must be


placed here
statements to repeat-BODY

Body of the loop (statements to be repeated)

NOTE on for loop :


The loop variable contains the highest value of the list
after the for loop is over.
Control enters the loop 10
times till the range gets
over and loop terminates

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

-30 -27 -24 ….. 0

5 10 15 20..…200

1 -4 7 -10 13 ….. -40

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

PROGRAM: MULTIPLICATION TABLE


of a Number
JUMP statements
Python offers two types of jump statements to be used
within loops to jump out of loop iterations.
i. break
Break statements are the jump statements which
terminates the very loop it lies within and skips over
a part of the code(i.e. rest of the loop) and jumps
over to the statement following the loop.
(FORCEFUL termination of the loop)
ii. continue
Continue is the jump statement which forces the next
iteration of the loop to take place and skips the rest
of the loop statements.
(FORCEFUL re-iteration of the loop)
The working of a break statement
while <test-condition> : for <var> in <sequence> :
statement 1 statement 1
if <condition> : if <condition> :
break break
statement 2 statement 2
statement 3 statement 3
statement 4 statement 4
statement 5 statement 5
The working of a continue statement
while <test-condition> : for <var> in <sequence> :
statement 1 statement 1
if <condition> : if <condition> :
continue continue
statement 2 statement 2
statement 3 statement 3
statement 4 statement 4
statement 5 statement 5
# Program to illustrate the
difference between break and continue
PROGRAM OUTPUT

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).

NESTED LOOP ENCLOSING LOOP/


/ INNER LOOP OUTER 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

You might also like