Python Programming
About Lesson

Decision/Selection/Branching Statements
Simple if statement
This statement checks a given condition and allows its corresponding block to be executed when the condition is true. In this developer don’t want to specify something special to be done when the condition is false. If the condition is false then the rest of the code in the program will be continued.

Example:
a=10
if (a>0):
print(“a is TEN”)
print(“Rest of the code”)

The multiple if statement
More than one simple if is known as multiple if. This is required when programmer wants to specify different code segments to be executed based on the evaluation of different independent conditions. Independent conditions means that we cannot determine the evaluation of one conditions based on the evaluation of other. For example, x==2 and y>3, we cannot determine whether y is greater than 3 or not based on the evaluation result of the first condition x is equal to 2.

#Example code showing how multiple if will work
x=int(input(“Enter any number from 1,2 or 3 only”))

if(x==1):
print(“x is ONE”)
if(x==2):
print(“x is TWO”)
if(x==3):
print(“x is THREE”)

print(“rest of the code”)

In the execution of above program, note CPU checks all the if conditions irrespective of evaluation of one of them. This is disadvantage of multiple if. For example, for x value 1, the print(“x is ONE”) will be executed which is fine. But after this, the following two if statements will be checked and as a result they are returned false, CPU moves forward in the program.

The if…else statement
We have just learned what is multiple if. It is used when more than one independent condition to be checked and perform certain actions accordingly. But what if we want to check two dependent conditions with multiple if. It seems unnecessary to write another if statement. For example, x==1 and x!=1, if you observed, it is unnecessary to check one of them when we know the truth value of other.

#example code showing two dependent conditions
a=int(input(“Enter your first number:”))
b=int(input(“Enter your second number:”))
if (a>b):
print(“\n”,a,” is big”)
if(b>a):
print(“\n”,b,” is big”)

#example code showing use of if…else for dependent conditions
a=int(input(“Enter your first number:”))
b=int(input(“Enter your second number:”))
if (a>b):
print(“\n”,a,” is big”)
else :
print(“\n”,b,” is big”)

else is also known as optional part of simple if. We can say, there can be if without else block. But no else can be without if block.

Nested if
We learnt that if statement keeps other program statements under its control and decides their execution. One if statement under the control of another if is called as nested if. The container if statement is called as outer if and the content if is called as inner if.
The inner if gets the chance of execution only when outer if condition returned true. The code under the control of inner if will get the chance of execution only when both outer and inner conditions are true

#Example code showing how Nested if will work
x=int(input(“Enter any number:”))

if(x>=1):
print(“x is Natural Number”)
if(x%2==0):
print(“x is even number too”)
print(“rest of the code in outer if”)

print(“rest of the code in main program”)

It is understood that inner if is found under the true part of outer if. In addition to this, inner can be simple if or if…else too

#Example code showing if…else in nesting
x=int(input(“Enter any number:”))

if(x>=1):
print(“x is Natural Number”)
if(x%2==0):
print(“x is even number too”)
else:
print(“x is odd number too”)
print(“rest of the code in outer if”)

print(“rest of the code in main program”)

The if…elif statement
We have seen one if statement under the true part of another if. Now, what if we want to check further condition when a given condition is false. That means, we will see if statement under else block.

#Example code showing if under else part
x=int(input(“Enter any number:”))

if(x<=0):
print(“x is not Natural Number”)
else:
print(“x is Natural Number”)
if(x%2==0):
print(“x is even number too”)
print(“rest of the code in outer if”)

print(“rest of the code in main program”)

in the above program, we can see there are some other statements under else block along with if statement. If else block is to be executed only after evaluating other condition, then we use elif statement.

#Example code showing elif can be used
x=int(input(“Enter any number:”))

if(x<0):
print(“x is Negative”)
elif(x==0):
print(“x is zero”)
else:
print(“x is positive”)

print(“rest of the code in main program”)

#Example 2 to print grade of student based on the following criteria
“””
Marks – Grade
0-35 – Fail
36-55 – C
56-75 – B
76-95 – A
96-100 – A+
“””
m=int(input(“Enter Marks:”))

if(m>=0 and m<=35):
print(“fail”)
elif(m>=36 and m<=55):
print(“Grade C”)
elif(m>=56 and m<=75):
print(“Grade B”)
elif(m>=76 and m<=95):
print(“Grade A”)
elif(m>=96 and m<=100):
print(“Grade A+”)
else:
print(“Invalid Marks”)

print(“rest of the code in main program”)

You cannot copy content of this page