Chap_6 Flow of Control
Chap_6 Flow of Control
Learning
Outcomes
Types of statements in Python
Statement of Flow Control
Program Logic Development
Tools
if statement of Python
Repetition of Task - A necessity
The range() function
Iteration / Looping statement
break and continue statement
Types o f Statement in Python
Compound_Statement_Header :
indented_body containing
multiple simple or compound
statement
Compound Statement has:
Action
1
Fals
e
Statement 1
Action
Statement
2
2
Selection
Fals
Condition e Exit from
? iteration
Tru
e
Statement
1
Loop
Body Python supports for
Action
y=2
if(x
==1
an
d
y==
2):
p
ri
n
t(
‘c
o
n
di
ti
Decision Making
Statement
2. if-else Statements
If-else statement executes some code if the test
expression is true (nonzero) and some other code if
the test expression is false.
Decision Making
Statement
2. if-else Statements
Syntax:
if(condition):
statements
else:
statements
e.g.
a=10
if(a
<
100)
:
pri
nt(
‘le
ss
th
an
10
Decision Making
Statement
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap
year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap
year".format(year))
i f Statement o f Python
‘if’ statement of python is used to execute
statements based on condition. It tests the
condition and if the condition is true it
perform certain action, we can also provide
action for false situation.
if statement in Python is of many forms:
if without false statement
if with else
if with elif
Nested if
Simple “ i f ”
In the simplest form if statement in Python
checks the condition and execute the
statement if the condition is true and do
nothing if the condition is false.
Syntax: All statement
belonging to if
if condition: must have same
indentation
Statement1 level
Statements
* * if ….
statement is compound statement having
header and a body containing intended
statement.
Points to remember with “ i f ”
s1 = int(input("Enter side
1")) s2 = int(input("Enter
side 2")) s3 =
int(input("Enter side 3")) if
s1==s2==s3:
print("Equilateral")
elif s1!=s2!=s3!=s1:
print("Scale
ne")
else:
print("Isosce
les")
Program to calculate and print roots of a quadratic equation:
ax2+bx+c=0(a!=0)
import math
print("For quadratic equation, ax**2 + bx + c =
0, enter coefficients ")
a = int(input("enter
a")) b =
int(input("enter b"))
c = int(input("enter
c")) if a==0:
print("Value of
",a," should not
be zero")
print("Aborting!!!
Program to calculate and print roots of a quadratic equation:
ax2+bx+c=0(a!=0)
if delta >0:
root1=(-b+math.sqrt(delta))/(2*a)
root2=(-b-math.sqrt(delta))/(2*a)
print("Roots are Real and UnEqual")
print("Root1=",root1,"Root2=",root
2)
elif delta==0:
root1=-b/(2*a)
print("Roots are Real and Equal")
print("Root1=",root1,"Root2=",root
1)
else:
To
D o…
1. WAP to enter marks of 5 subject and calculate total, percentage
and also division.
Percentage Division
>=60 First
>=45 Second
>=33 Third
otherwise Failed
2. WAP to enter any number and check it is positive, negative or
zero
number
3.WAP to enter Total Bill amount and calculate discount as per
given table and also calculate Net payable amount (total bill –
discount)
>=20000 15% of Total
Total Bill Discount
>=15000 Bill 10% of
>=10000 Total Bill 5% of
otherwise Total Bill 0 of
Total Bill
To
D
4.oWAP
… to enter Bill amount and ask the user the payment
mode and give the discount based on payment mode. Also
display net payable amount
Mode Discount
Credit Card 10% of bill
Debit Card amount 5% of bill
Net amount 2% of
Banking bill amount 0
5.WAPotherwise
to enter two number and ask the operator (+ - * / )
from the user and display the result by applying operator on
the two number
6. WAP to enter any character and print it is Upper Case,
Lower Case, Digit or symbol
7.WAP to enter 3 number and print the largest number
8.WAP to input day number and print corresponding day
name for e.g if input is 1 output should be SUNDAY and so
on.
Nested i f
In this type of “if” we put if within another if as a
statement of it. Mostly used in a situation where
we want different else for each condition. Syntax:
if condition1:
if condition2:
statements
else:
stateme
nts elif condition3:
stat
ements
else:
stat
WAP to check entered year is leap year or not
1. While Loop
2. For Loop
x=1
while (x < 3):
print('inside while loop value of x is ',x)
x=x+1
else:
print('inside else value of x is ', x)
Output
inside while loop value of x is 1
inside while loop value of x is
2 inside else value of x is 3
*Write a program in python to
find out the factorial of a given
number
Iteration Statements
(Loops)
While Loop continue
Infinite While Loop
e.g.
x=5
while (x == 5):
print(‘inside loop')
Output
Inside loop
Inside
loop
…
…
Iteration Statements
(Loops)
2. For Loop
It is used to iterate over items of any sequence, such as a list
or a string.
Syntax
for val in sequence:
statements
e.g.
for i in range(3,5):
print(i)
Output
3
4
Iteration Statements (Loops)
Output
5
4
range() Function
Parameters
start: Starting number
of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in
the sequence.
Iteration Statements (Loo
Output
1
2
3
No Break
Iteration Statements
(Loops)
2. For Loop continue
Nested For Loop
e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()
Output
123456789
10
2 4 6 8 10 12 14
16 18 20
Iteration Statements
(Loops)
2. For Loop continue
Factorial of a number
factorial = int(input(‘enter a number’))
for i in range(years):
n=n+((n*rate)/100)
print(n)
Iteration Statements
(Loops)
3. Jump Statements
print("Th
e end")
Output
s
t
Iteration Statements
(Loops)
2.continue
It is used to skip all the remaining statements
in the loop and move controls back to the top of
the loop.
e.g.
for val in "init":
if val == "i":
continue
print(val)
print("The
end")
Outp
ut n
t
The
end
Iteration Statements
(Loops)
3. pass Statement
This statement does nothing. It can be used when a
statement is required syntactically but the program
requires no action.
Use in loop
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
In function
It makes a controller to pass by without executing any code.
e.g.
def myfun():
pass #if we don’t use pass here then error message will be shown
print(‘my program')
OUTPUT
My program
Iteration Statements
(Loops)
3. pass Statement continue
e.g.
for i in 'initial':
if(i == 'i'):
pass
else:
print(i)
OUTPUT
n
L
NOTE : continue forces the loop to start at the next iteration
while pass means "there is no code to execute here" and will