8.flow of Control
8.flow of Control
Empty Statement
A statement that does nothing.In Python an empty statement is pass statement.It takes the
following form:
Pass
Whenever Python encounters a pass statement, Python does nothing and moves to the next
statement in the flow of control.
Simple Statement
Any executable statement is a simple statement in Python. For example:
Name=input(“enter your name”)
Compound Statement
A compound statement represents a group of statements executed as a unit. The compound
statements in Python are written in a specific pattern .
It has :
● A header line which begins with a keyword and ends with a colon.
● A body consisting of one or more Python statements, each indented inside the header
line.
SEQUENCE
● The sequence construct means the statements are
being executed sequentially.
● Sequence refers to the normal flow of control in a
program and is the simplest one.
1
SELECTION
● The selection of a construct means the execution of a statement. depending upon a
condition test.
● If a condition evaluates to true, a cause of action (a set of statements) is followed,
otherwise another course of action (a different set of statements) is followed.
● This construct (selection construct) is also called decision construct because it helps in
making decisions about which set of statements is to be executed.
● Decision making can be implemented in python using:
1. if statements
2. if-else statements
3. if-elif statements
4. nested if statements
2
Program Logic Development Tools:
Before developing the solution of a problem in terms of a program, we should read and analyze
the given problem and decide about basic sub tasks needed to solve a problem i.e., algorithm.
The various logic development tools are:
● Flowcharts
● Decision trees
● Pseudocode
3
DECISION TREES:- A decision tree is a decision support tool that uses a tree-like model
of decisions and their possible consequences, including chance event outcomes, resource
costs, and utility. It is one way to display an algorithm that only contains conditional control
statements.
FLOW CHART
4
If-else statement :
● An else statement can be combined with an if statement.
● An else statement contains the block of code that executes if the conditional expression
in the if statement resolves to 0 or a false value.
● The else statement is an optional statement and there could be at most only one else
statement following if.
The syntax of if..else is:
if expression:
statement(s)
else:
statement(s)
Example :
if grade == ‘A’ :
print (“well done”)
else:
print (”try again”)
FLOW CHART
5
Example :
num=int(input(“enter the number”))
if num<0:
print(“Invalid entry. Valid range is 0 to 999.’’
elif num<10:
print(“single digit number is entered”
elif num<100:
print(“double digit number is entered”
elif num<=999:
print(“three digit number is entered”)
else:
print(“invalid entry.valid range is 0 to 999.”)
FLOW CHART
6
Example:
a=10
b=20
c=5
if a>b:
if a>c:
print("Greatest number is ",a)
else:
print("Greatest number is",c)
else:
if b>c:
print("Greatest number is ",b)
else:
print("Greatest number",c)
Flow Chart
STORING CONDITION
● Sometimes the conditions being used in code are complex and repetitive.
● In such cases, to make your program more readable, you can use named conditions
i.e., you can store conditions in a name and then use that named conditional in the if
statements.
7
Syntax of range() function is:
range([start], [stop], [step])
For example:-
range(0,10, 2)
Output:- [ 0, 2, 4, 6, 8 ]
For example:-
'a' in "trade"
Output:- True
as 'a' is contained in sequence. (string type) "trade".
Syntax:-
for <variable> in <sequence>
statements_to_repeat
For example:-
for a in [ 1, 4, 7 ]
print(a)
print(a*a)
8
A for loop in Python is processed as:
● The loop-variable is assigned the first value in the sequence.
● All the statements in the body of the for loop are executed with the assigned value of the
loop variable (step 2).
● Once step 2 is over, the loop- variable is assigned the next value in the sequence and
the loop-body is executed (i.e., step 2 repeated) with the new value of loop-variable.
● This continues until all values in the sequences are processed.
Syntax:-
while test_condition:
body of while
For example:-
a=5
while a > 0 :
print ("hello" , a)
a=a-3
print ("Loop Over !!")
Output:-
hello 5
hello 2
Loop Over !!
● The loop variable must be updated inside the body- of -the -while in a way that after
some time the test-condition becomes false otherwise the loop will become an endless
loop or infinite loop.
Consider the following code :
a=5
while a > 0 :
print(a)
print ("Thank You")
ENDLESS LOOP! Because the loop- variable a remains 5 always as its value is not updated in the loop-body hence the
loop-condition a>0 always remains true.
The corrected form of above loop will be:
a=5
while a > 0 :
print(a)
a -= 1
print ("Thank You")
● To cancel the running of an endless loop, we can press CTRL + C keys anytime during
its endless repetition to stop it.
10
JUMP STATEMENT - BREAK AND CONTINUE
Python offers two jump statements to be used within loops to jump out of loop-iterations.
For example :-
a=b=c=0
for i in range(1,21):
a = int (input("Enter number 1 :"))
b = int (input("Enter number 2 :"))
if b == 0:
print ("Division by zero error! Aborting!")
break
else:
c = a // b
print ("Quotient = ", c)
print ("Program over !")
Output:-
Enter number 1 : 6
Enter number 2 : 2
Quotient = 3
Enter number 1 : 8
Enter number 2 : 3
Quotient = 2
Enter number 1 : 5
Enter number 2 : 0
Division by zero error! Aborting!
Program over !
11
INFINITE LOOPS AND BREAK STATEMENT
Sometimes, programmer's create infinite loops purposely by specifying an expression which
always remains true, but for such purposely created infinite loops, they incorporate some
condition inside the loop-body on which they can break out of the loop using a break statement.
For example:-
a=2
while True :
print (a)
a*=2
if a>100:
break
For example:-
a=b=c=0
for i in range(0,3):
print ("Enter 2 numbers")
a = int (input("Number 1 :"))
b = int (input("Number 2 :"))
if b == 0:
print ("\n The denominator cannot be zero. Enter again !")
continue
else:
c = a // b
print ("Quotient = ", c)
12
Output:-
Enter 2 numbers
Number 1 : 5
Number 2 : 0
The denominator cannot be zero. Enter again !
Enter 2 numbers
Number 1 : 5
Number 2 : 2
Quotient = 2
Enter 2 numbers
Number 1 : 6
Number 2 : 2
Quotient = 3
Syntax of python loops along with the else clause is as given below:-
for <variable> in <sequence> : while <test condition> :
statement 1 statement 1
statement 2 statement 2
: :
else : else :
statement(s) statement(s)
13