Branching programs allow us to make choices and do different things. Let’s have look at the following flow diagram.
Observing the flow diagram:
- A test (expression that evaluates to
TrueorFalse) - A block of code to execute
- If the test is
True - An optional block of code to execute if the test is
False
Table of Contents
- A simple example of
ifandelsein Python: - Nested
if,elseandelifin Python: - Compound conditions inside
if
Such a desire can be achieved using if else clauses in Python.
A simple example of if and else in Python:
x = int(raw_input('Enter an integer: ')) # Will wait for you to enter an int
if x%2 == 0: # % (modulo) returns the remainder when x/2
print('Even')
else:
print('Odd')
print('Done with condition')Analysis of the above code:
- The expression
x%2 == 0evaluates toTruewhen the remainder ofxdivided by2is0 - Note that
==is used for comparison, since=is reserved for assignment - The indentation is important! Each indented set of expressions denotes a block of instructions
- For example, if the last statement were indented, it would be executed as part of the
elseblock of code
- For example, if the last statement were indented, it would be executed as part of the
- Note how this indentation provides a visual structure that reflects the semantic structure of the program
Nested if, else and elif in Python:
An example program to check if a number if divisible by 2 and/or 3.
x = int(raw_input('Enter an integer: ')) # Will wait for you to enter an int
if x%2 == 0:
if x%3 == 0:
print('Divisible by 2 and 3')
else:
print('Divisible by 2 but not by 3')
elif x%3 == 0:
print('Divisible by 3 but not by 2')
else:
print('Not divisible by either 2 or 3')Analysis of the above code:
- Notice the
if elseblock inside the firstifblock. This is called nesting. - We have an
elifblock, this is executed when the code does not flow throughif.elifis, as you might have guessed, just a short form for ‘else if’. - The last
elseblock is executed with the code does not flow through either ofiforelif
Compound conditions inside if
Let’s consider a program to find the minimum of three integers.
x, y, z = map(int, raw_input('Enter three space separated integer: ').split())
if x < y and x < z:
print('Minimum among ' + str(x) + ', ' + str(y) + ', '+ str(z) + ' is ' + str(x))
elif y < z:
print('Minimum among ' + str(x) + ', ' + str(y) + ', '+ str(z) + ' is ' + str(y))
else:
print('Minimum among ' + str(x) + ', ' + str(y) + ', '+ str(z) + ' is ' + str(z))Analysis of the above code:
- The first statement itself is a mess here, what is does is, reads a string from input and tries to split them by space and then convert each of the parts into an integer type. If the conversion of all of them is successful, it assigns each of them to
x,y, andzrespectively. - We check for two conditions in the
ifclause, and then print a human readable string by joining several strings and numbers. Sincex,y, andzwere integers, we first need to convert them intostrobjects to be able to join them elifandelseare quite understandable
Next, we will discuss how to perform repetitive tasks by creating a loop in Python.
Note: This is a part of what I learned in an online Open Course Ware offered by MIT on edX. Its for my personal reference & also for those who would like to revisit the course.