You are on page 1of 3

Describe the difference between a 

chained conditional and a nested conditional. Give your


own example of each. Do not copy examples from the textbook.

 A nested conditional is a conditional statement inside another conditional statement


which means there will be “if” or “if—else” statement inside another if or if else
statement. It will be more complicated and difficult to understand when the program
getting bigger. So, as much as possible we shall avoid them.

def my_mark2(x):

if x>=90:

print("A")

else:

if x<90 and x>=80: #it is a conditional statement inside another conditional


statement

print("B")

else:

if x<80 and x>=73:

print("C")

else:

print("You are not qualified to go through at the university of the people")

my_mark2(85)

my_mark2(90)

my_mark2(78)

my_mark2(72)

 A chained condition is a condition which checks if it is true or false in order. we can


evaluate different conditions using “elif” which is the abbreviation word for “else if”. The
condition will become end when we close with “else” statement.

(Downey A, 2015) “Each condition is checked in order. If the first is false, the next is
checked, and so on. If one of them is true, the corresponding branch runs and the
statement ends. Even if more than one condition is true, only the first true branch runs
(P 42)”.

def my_mark(x):

if x>=90:

print("A")

elif x<90 and x>= 80:

print("B")

elif x<80 and x>=73:

print("c")

else:

print("You are not qualified at the university of the people to go through")

my_mark(85)

my_mark(90)

my_mark(78)

my_mark(72)

Deeply nested conditionals can become difficult to read. Describe a strategy for avoiding nested
conditionals. Give your own example of a nested conditional that can be modified to become a
single conditional, and show the equivalent single conditional. Do not copy the example from
the textbook.
def math_dev(x):

if x%2==0:

if x%3==0: #another if statement inside the if statement

print("the number is divisible by 6 without reminder")

else:

print("the number is not divisible by 6 without reminder")

math_dev(6842)

math_dev(6846)

math_dev(12890)

math_dev(2098076)

def math_dev2(y):

if y%2==0 and y%3==0:

print("the number is devisible by 6 without reminder")

else:

print("the number is not devisible by 6 without reminder")

math_dev2(6842)

math_dev2(6846)

math_dev2(12890)

math_dev2(2098076)

You might also like