CHAPTER-3
PROGRAM FLOW CONTROL
1. CONDITIONAL CONSTRUCT – if else STATEMENT
CONDITIONAL CONSTRUCT – if else STATEMENT
Conditional constructs (also known as if
statements) provide a way to execute a chosen
block of code based on the run-time evaluation of
one or more Boolean expressions. In Python, the
most general form of a conditional is written as
follows:
Contd.. Next Slide
CONDITIONAL CONSTRUCT – if else STATEMENT
: Colon Must
if first condition:
first body
elif second condition:
second body
elif third condition:
third body
else:
fourth body
CONDITIONAL CONSTRUCT – if else STATEMENT
✓Each condition is a Boolean expression, and
each body contains one or more commands that
are to be executed conditionally.
✓If the first condition succeeds, the first body will
be executed; no other conditions or bodies are
evaluated in that case.
CONDITIONAL CONSTRUCT – if else STATEMENT
✓If the first condition fails, then the process
continues in similar manner with the evaluation
of the second condition. The execution of this
overall construct will cause precisely one of the
bodies to be executed.
✓There may be any number of elif clauses
(including zero), and
✓ The final else clause is optional.
CONDITIONAL CONSTRUCT – if else STATEMENT
EXAMPLE - PROGRAM
EXAMPLES – if STATEMENT
else is missing,
it is an optional
statement
OUT PUT
CONDITIONAL CONSTRUCT
EXAMPLE – if else STATEMENT
EXAMPLE – if else STATEMENT
: Colon Must
else is
used
OUT PUT
CONDITIONAL CONSTRUCT
EXAMPLES – if elif STATEMENT
EXAMPLES – if elif STATEMENT
READ AS
18 is less
than age
and
18 is less
than 60
OUTPUT
2. ITERATION OR LOOPING
ITERATION
3. ITERATION OR LOOPING
What is loop or iteration?
Loops can execute a block of code number
of times until a certain condition is met.
OR
The iteration statement allows instructions to
be executed until a certain condition is to be
fulfilled.
The iteration statements are also called as
loops or Looping statements.
3. ITERATION OR LOOPING
Python provides two kinds of loops &
they are,
while loop
for loop
while loop
while loop
A while loop allows general repetition
based upon the repeated testing of a Boolean
condition
The syntax for a while loop in Python is as
follows:
while condition: : Colon Must
body
Where, loop body contain the single
statement or set of statements (compound
statement) or an empty statement.
Contd..
while loop
The loop iterates while the expression
evaluates to true, when expression becomes
false the loop terminates.
FLOW CHART
while loop
while loop – Programming example
while loop - programs
# Natural Numbers generation
OUTPUT
while loop - programs
# Calculating Sum of Natural Numbers
OUTPUT
while loop - programs
#Generating Fibonacci numbers
while loop - programs
#Generating Fibonacci numbers
OUTPUT
for LOOP
for LOOP
Python’s for-loop syntax is a more
convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,
for element in iterable:
body
for LOOP
Python’s for-loop syntax is a more
convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,
for element in iterable:
body
for LOOP
Till the list
exhaust for loop
will continue to
execute.
OUTPUT
for LOOP
range KEYWORD
for LOOP - range KEYWORD
The range() function returns a
sequence of numbers, starting from 0 by
default, and increments by 1 (by default),
and ends at a specified number.
range(start, stop, step)
x = range(3, 6)
for n in range(3,6):
OR for n in x:
print(n)
print(n)
for LOOP - range KEYWORD
#Generating series of numbers
OUTPUT
for LOOP - range KEYWORD
#Generating even numbers
OUTPUT
for LOOP – len() FUNCTION
for LOOP - range KEYWORD
# print string character by character
OUTPUT
else statement in loop
else statement in loop
else can be used in for and while loops
the else body will be executed as and when the
loop’s conditional expression evaluates to false
OUTPUT
3. BRANCHING OR JUMPING STATEMENTS
3. BRANCHING OR JUMPING STATEMENTS
Python has an unconditional branching
statements and they are,
1. break STATEMENT
2. continue STATEMENT
3. BRANCHING OR JUMPING STATEMENTS
1. break STATEMENT
Break can be used to unconditionally
jump out of the loop. It terminates the
execution of the loop. Break can be used in
while loop and for loop. Break is mostly
required, when because of some external
condition, we need to exit from a loop.
1. break STATEMENT
Loop
Condition ? break
causes
jump
True
Statement 1
break
1. break STATEMENT
OUT PUT
2. continue STATEMENT
The continue statement in
Python returns the control to the
beginning of the while loop. The continue
statement rejects all the
remaining statements in the current
iteration of the loop and moves the
control back to the top of the loop.
The continue statement can be used in
both while and for loops.
2. continue STATEMENT
Loop False
Condition ?
True
Statement 1
Statements
ignored or continue
skipped
continue
Statement n
causes
jump
2. continue STATEMENT
when i value becomes 2 the print statement gets
skipped, continue statement goes for next iteration,
hence in the out put 2 is not printed
pass STATEMENT
pass STATEMENT
The pass statement in Python is used when a
statement is required syntactically but you do not
want any command or code to execute.
The pass statement is a null operation; nothing
happens when it executes.
The pass is also useful in places where your
code will eventually go, but has not been written
yet (e.g., in stubs for example):
pass STATEMENT
pass in loop has
pass in loop no output
Difference Between break and continue
break continue
Difference Between break and continue
BREAK CONTINUE
It terminates the execution It terminates only the current
of remaining iteration of iteration of the loop.
the loop.
'break' resumes the control 'continue' resumes the control
of the program to the end of the program to the next
of loop enclosing that iteration of that loop enclosing
'break'. 'continue'.
It causes early termination It causes early execution of
of loop. the next iteration.
'break' stops the 'continue' do not stops the
continuation of loop. continuation of loop, it only
stops the current iteration.