Python Flow Control
if…else
for Loop
while loop
Break and continue
Pass statement
if...else
 Decision making is required when we want to
execute a code only if a certain condition is satisfied.
 The if…elif…else statement is used in Python for
decision making.
 In Python, the body of the if statement is indicated by
the indentation.
 Body starts with an indentation and the first
unindented line marks the end.
 Python interprets non-zero values
asTrue. None and 0 are interpreted as False.
Exercise – if
 Write a program to give a discount of
10% if the total bill amount exceeds 1000.
if...else Statement
 The if..else statement evaluates test expression and will
execute body of if only when test condition is True.
 If the condition is False, body of else is executed.
Indentation is used to separate the blocks.
Exercise
 Write a program to check if a given
number is a multiple of 5.
if...elif...else
 The elif is short for else if. It allows us
to check for multiple expressions.
 If the condition for if is False, it checks
the condition of the next elif block
and so on.
 If all the conditions are False, body of
else is executed.
 Only one block among the
several if...elif...else blocks is executed
according to the condition.
 The if block can have only
one else block. But it can have
multiple elifblocks.
Programming Task
 Minimum age to Cast yourVote : 18
 Minimum age to Contest an Election: 25
 Given the age verify if the person can
vote and can s(he) contest an election.
Exercise
 Write a program to check if a given year is leap year or not.
 Logic:
 if a year is not divisible by 4, its not a leap year.
 If a year is divisible by 4 and not divisible by 100, it’s a leap year.
 If a year is divisible by 4 and 100 then it should be divisible by 400
to be a leap year
Loops
 Loops are used in programming to
repeat a specific block of code.
 Looping Constructs in Python
◦ while
◦ for
While loop
 The while loop in Python is used to iterate over
a block of code as long as the test expression
(condition) is true.
 We generally use this loop when we don't know
beforehand, the number of times to iterate.
 Write a program to print the number of
digits of a given number using while loop.
For loop
 For loops iterate over a given sequence.
 Here, val is the variable that takes the value of the item
inside the sequence on each iteration.
 Loop continues until we reach the last item in the
sequence.The body of for loop is separated from the
rest of the code using indentation.
Range Function
 We can generate a sequence of numbers
using range() function.
 range(10) will generate numbers from 0 to 9 (10
numbers).
 To force this function to output all the items, we can
use the function list().
Range function
 We can also define the start, stop and
step size as range(start,stop,step size).
 step size defaults to 1 if not provided.
Exercise
 Write a program to calculate the factorial
of a given number.
break and continue
 Loops iterate over a block of code until test expression
is false, but sometimes we wish to terminate the
current iteration or even the whole loop without
checking test expression.
 break statement
 The break statement terminates the loop containing it.
Control of the program flows to the statement
immediately after the body of the loop.
 If break statement is inside a nested loop (loop inside
another loop), break will terminate the innermost loop.
Exercise
 Write a program to check if a given
number is prime or not.
 If a number is divisible by any number
between 2 and n-1, its not prime,
otherwise prime
continue statement
 The continue statement is used to skip the rest of the
code inside a loop for the current iteration only. Loop
does not terminate but continues on with the next
iteration.
for loop with else
 A for loop can have an optional else block as well.
The else part is executed if the items in the sequence
used in for loop exhausts.
 break statement can be used to stop a for loop. In such
case, the else part is ignored.
 Hence, a for loop's else part runs if no break occurs.
for loop with else
Nested Loop
while loop with else
 Same as that of for loop, we can have an
optional else block with while loop as well.
Pass statement
 Suppose we have a loop or a function that is not
implemented yet, but we want to implement it in the
future.
 They cannot have an empty body.
 We use the pass statement to construct a body that
does nothing.

Python Flow Control

  • 1.
    Python Flow Control if…else forLoop while loop Break and continue Pass statement
  • 2.
    if...else  Decision makingis required when we want to execute a code only if a certain condition is satisfied.  The if…elif…else statement is used in Python for decision making.  In Python, the body of the if statement is indicated by the indentation.  Body starts with an indentation and the first unindented line marks the end.  Python interprets non-zero values asTrue. None and 0 are interpreted as False.
  • 4.
    Exercise – if Write a program to give a discount of 10% if the total bill amount exceeds 1000.
  • 6.
    if...else Statement  Theif..else statement evaluates test expression and will execute body of if only when test condition is True.  If the condition is False, body of else is executed. Indentation is used to separate the blocks.
  • 8.
    Exercise  Write aprogram to check if a given number is a multiple of 5.
  • 9.
    if...elif...else  The elifis short for else if. It allows us to check for multiple expressions.  If the condition for if is False, it checks the condition of the next elif block and so on.  If all the conditions are False, body of else is executed.  Only one block among the several if...elif...else blocks is executed according to the condition.  The if block can have only one else block. But it can have multiple elifblocks.
  • 12.
    Programming Task  Minimumage to Cast yourVote : 18  Minimum age to Contest an Election: 25  Given the age verify if the person can vote and can s(he) contest an election.
  • 13.
    Exercise  Write aprogram to check if a given year is leap year or not.  Logic:  if a year is not divisible by 4, its not a leap year.  If a year is divisible by 4 and not divisible by 100, it’s a leap year.  If a year is divisible by 4 and 100 then it should be divisible by 400 to be a leap year
  • 17.
    Loops  Loops areused in programming to repeat a specific block of code.  Looping Constructs in Python ◦ while ◦ for
  • 18.
    While loop  Thewhile loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.  We generally use this loop when we don't know beforehand, the number of times to iterate.
  • 20.
     Write aprogram to print the number of digits of a given number using while loop.
  • 22.
    For loop  Forloops iterate over a given sequence.  Here, val is the variable that takes the value of the item inside the sequence on each iteration.  Loop continues until we reach the last item in the sequence.The body of for loop is separated from the rest of the code using indentation.
  • 24.
    Range Function  Wecan generate a sequence of numbers using range() function.  range(10) will generate numbers from 0 to 9 (10 numbers).  To force this function to output all the items, we can use the function list().
  • 25.
    Range function  Wecan also define the start, stop and step size as range(start,stop,step size).  step size defaults to 1 if not provided.
  • 27.
    Exercise  Write aprogram to calculate the factorial of a given number.
  • 30.
    break and continue Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking test expression.  break statement  The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.  If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.
  • 32.
    Exercise  Write aprogram to check if a given number is prime or not.  If a number is divisible by any number between 2 and n-1, its not prime, otherwise prime
  • 34.
    continue statement  Thecontinue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
  • 35.
    for loop withelse  A for loop can have an optional else block as well. The else part is executed if the items in the sequence used in for loop exhausts.  break statement can be used to stop a for loop. In such case, the else part is ignored.  Hence, a for loop's else part runs if no break occurs.
  • 36.
  • 37.
  • 38.
    while loop withelse  Same as that of for loop, we can have an optional else block with while loop as well.
  • 39.
    Pass statement  Supposewe have a loop or a function that is not implemented yet, but we want to implement it in the future.  They cannot have an empty body.  We use the pass statement to construct a body that does nothing.