Answer to Question #99879 in Python for oladepo

Question #99879
describe the difference between a chained conditional and a nested conditional,give example for each
1
Expert's answer
2019-12-05T10:34:11-0500

Nested conditional. It is the case, when we have one (or more) if/else condition inside another if/else. For example:

		if a > 2:
          print("a is greater then 2")
        else:
          if a < 2:
            print("a is less then 2")
          else:
            print("a is equal to 2")

Here the first if is checked first and in case the statement a>2 is not true, compiler goes to the first else. But here we have another if to be checked.


Chained conditional. It represents the same idea, but in more simple way of coding, using if/elif/else:

		if a > 2:
          print("a is greater then 2")
        elif a < 2:
          print("a is less then 2")
        else:
          print("a is equal to 2")

Here first the if statement is checked. If it is true, everething ends. If not, then elif statement a<2 is checked. Again, if it is true, this block finishes, if not - goes to else. There could be more then one elif statement. The output of later program is the same as of former one.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS