Final Revised Conditional and Iterative Stmnts 2020-21
Final Revised Conditional and Iterative Stmnts 2020-21
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.
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
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
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