top of page

Control flow - if else


Whenever we write useful programs we need to control the flow of execution, and one way to do the same is to check some condition and proceed accordingly on the basis of the result. For example if we need to check whether the user has entered a positive number or not we can use if statement as show in the following example:


The syntax for if statement is:

if condition:




The flow control graph for the if statement is as follows:


Now, what happens if we enter a negative number to the input, we observe that program simply terminates, this happens because we have only specified what happens if the number is positive but what happens when we enter a negative number? The answer to this question is given in else statement, so else come into picture when the condition of if fails as shown in the following example:



So, in general we can say the syntax of if-else will be:


if expression:

#Statements to be executed if expression is True

else:

#Statements to be executed if expression is False


Now here is the control flow diagram for if-else


Now there are situations when the conditions can only be either True or False but in some cases like among three numbers to test which one is greatest simply if else is not going to work as there can be three possibilities

i) a is the greatest -> if a is greater than b and also c

ii) b is the greatest -> if b is greater than a and also c

iii)c is the greatest -> if c is greater than a and also b


Therefore in such cases when we have more than one condition to check we need something called as elif (stands for else if), now to write a program for finding out the greatest number among three can be done in the following way:


That is one way of doing that but the program can be simplified further by knowing the fact that after first condition is evaluated there is no need to check b against a in second condition because if the first condition is false it automatically means that a is not greatest hence for b to be greatest it need not compare to a just compare it with c (possibilty for a being greatest already ruled out), also in the last condition for checking c to be greatest we need not give any condition because if a is not greatest and b is not greatest c is bound to be the greatest, so we can just use else statement here, this will be clear for the following program:



So, in general we can say the syntax of elif ladder will be:


if condition1:

statement(s)

elif condition2:

statement(s)

else:

statement(s)


Here is the control flow graph for the elif ladder:



One thing to note is that there is no limit for the number of elif statements, the above diagram is showing control flow with one elif statement.

Now some folks might think that why even we need elif or else, why just we don't construct the whole program using if, well the problem with that is that we might end up getting inside multiple conditional code blocks after doing so because in case of elif we only go inside one of the conditional code block but in if the blocks aren't connected hence the flow can go inside any of them for example if we take this program to see the range of a number:



We can't do something like that because we are getting inside multiple conditions, though the program is 100% correct but this is not something we want, what we want is this:



Here we get inside second elif only when number is greater than 20.

Now there is one more interesting property about else if statements that they can nested, i.e we can have multiple if else statements inside one. The following program is modified version to find the greatest number among three using nested if else:



So, that's it folks for if else, all the codes used are given in the code section below, I advice you to play with them do some modifications and see what happens, hope you liked the article, see you in the next one.




Code Section:


i) IF

Number = int(input("Enter the number: ")) if(Number > 0): print("The number is positive")


ii) IF ELSE

Number = int(input("Enter the number: ")) if(Number > 0): print("The number is positive")

else:

print("The number is negative")


iii) IF ELIF

a = int(input("Enter the first number: ")) b = int(input("Enter the second number: ")) c = int(input("Enter the third number: "))

if(a>b and a>c): #Possibility (i) print("{} is greatest".format(a))

elif(b>a and b>c): #Possibility (ii) print("{} is greatest".format(b))

elif(c>a and c>b): #Possibility (iii) print("{} is greatest".format(c)) iv) IF ELIF ELSE

a = int(input("Enter the first number: ")) b = int(input("Enter the second number: ")) c = int(input("Enter the third number: "))

if(a>b and a>c): #a is greatest print("{} is greatest".format(a))

elif(b>c): #a is not greatest, either b or c is print("{} is greatest".format(b))

else: #neither b or c is greatest hence c is print("{} is greatest".format(c)) v) IF (Range)

Number = int(input("Enter the number: "))

if Number>0: print("The number is greater than zero")

if Number<20: print("The number is in between 0 and 20")

if Number<100: print("The number is less than 100")

vi) ELIF (Range)

Number = int(input("Enter the number: "))

if Number>0: print("The number is greater than zero and less than twenty")

elif Number<20: print("The number is in between 0 and 20")

elif Number<100: print("The number is greater than 20 and less than 100")

vi) Nested IF ELSE

n1 = int(input("Enter the first number: ")) n2 = int(input("Enter the second number: ")) n3 = int(input("Enter the third number: "))

if n1>n2: #Check if n1 is greater than n2

if n1>n3: #and check if n1 is greater than n3 also print("{} is greatest".format(n1))

else: #means that n1 is greater than n2 but not n3 print("{} is greatest".format(n2))

else: #n1 being ruled out to be greatest either of n2 or n3 is greatest

if n2>n3: #means that n2 is greatest print("{} is greatest".format(n2))

else: print("{} is greatest".format(n3))



bottom of page