Flow of
Control
CHAPTER -VI
CLASS – XI
INTRODUCTION
In this chapter we will discuss about the if, for
and while statements. We will also discuss some jump statements of
Python which are break and continue.
TYPES
OF STATEMENTS IN PYTHON
Statements are the instructions given to the computer
to perform any kind of action, be it data movements, and be it making decisions
or be it repeating actions. Statements are the smallest executable unit within
a Python program. Python statements can be belong to one of the following three
types:
Empty
Statement
Simple
Statement (Single statement)
Compound
Statement
1. Empty Statement: The simplest statement is the empty statement i.e.,
a statement which does nothing. In Python an empty statement is pass statement.
It takes the pass form.
In other word, the pass statement of Python is a
do-nothing statement i.e. empty statement or null operation statement.
A pass statement is useful in those instances
where the syntax of the language requires the presence of a statement but where
the logic of the program does not. We will see it in loops and their bodies.
2. Simple Statement:
Any single executable statement is a simple statement
in Python. For example, following is a simple statement in Python:
name
= (“Write Your Name:”)
Another example of simple statement is:
print(name) # print function called
3. Compound Statement: A compound statement represents a group of statements
executed as a unit. The compound statements of Python are written in a specific
pattern as shown below:
<compound
statement header>:
<indented
body containing multiple simple or compound statements>
That is, a compound statement has:
Header line
which begins with a keyword and ends with a colon.
Body consisting of one or more Python statements, each indented inside the header line. All statements in the body are the same level of indentation.
FLOW CHART
A flowchart is simply a
graphical representation of steps. It shows steps in a sequential order, and is
widely used in presenting flow of algorithms, workflow or processes. Typically,
flowchart shows the steps as boxes of various kinds, and their order by
connecting them with arrows.
Flowchart Symbols
Different
flowchart shapes have different conventional meanings. The meanings of some of
the more common shapes are as follows:
1. Terminator: The terminator symbol
represents the starting or ending point of the system.
2. Process: A box indicates some
particular operation.
3. Document: This represents a printout, such as a document or a report.
4. Decision: A diamond represents a
decision or branching point. Lines coming out from the diamond indicates
different possible situations, leading to different subprocesses.
5. Data:It represents information
entering or leaving the system. An input might be an order from a customer. An
output can be a product to be delivered.
6. Subprocess:This symbol use to invoke a procedure
written already.
7. Flow:Lines represent flow of
the sequence and direction of a process.
Control Statements
Control statements are used to control the flow of execution
depending upon the specified condition/logic. There are three types of control
statements.
1. Decision Making Statements
2. Iteration Statements (Loops)
3. Jump Statements (break, continue, pass)
1. Decision
Making Statement: Decision
making statement used to control the flow of execution of program depending
upon condition.
There are three types of decision-making statement.
1. if statements
2. if-else statements
3. Nested if-else statement
Decision Making Statement
1. if statements: An if
statement is a programming conditional statement that, if proved true, performs
a function or displays information.
Other
Example
Using
logical operator in if 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.
3. Nested if-else statement:The nested
if...else statement allows you to check for multiple test expressions and
execute different codes for more than two conditions.
ITERATION
STATEMENTS (LOOPS)
Iteration
statements(loop) are used to execute a block of statements as long as the
condition is true. Loops statements are used when we need to run same code
again and again.
Python Iteration
(Loops) statements are of three type :-
1. While Loop
2. For Loop
3. Nested For Loops
1. While Loop: It is used to execute a block of statement as long as a given condition is true. And when the condition become false, the control will come out of the loop. The condition is checked every time at the beginning of the loop.
While
Loop with Else
Example:
x = 1
while (x
< 3):
print(‘inside
while loop value of x is’ , x)
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 5
Example
of Infinite While Loop
a = 10
while (a == 10):
print(‘Inside loop’)
Output:
Inside loop
Inside loop
….
….
2. For Loop:It is used to iterate over items of any sequence, such as a list or a string
Jump Statements: Jump
statements are used to transfer the program's control from one location to
another. Means these are used to alter the flow of a loop like - to skip a part
of a loop or terminate a loop.
There are three types
of jump statements used in python.
1.break
2.continue
3.pass
2.
Continue: It is used to skip
all the remaining statements in the loop and move controls back to the top of
the loop.
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.
Example:
def myfunc():
pass
#if we don’t use pass here then error message will be shown
print(‘My PythonProgram’)
Output:
My
Python Program
Other
example of pass statement
for I in
‘initial’:
if(i ==
‘i’):
pass
else:
print(i)
Output:
n
t
a
l
Note:continue forces the loop to start at the next
iteration while pass means "there is no code to execute here" and
will continue through the remainder or the loop body.
!!! THANK YOU !!!