please help fast:
Compare and contrast if-else and elif statements. Give examples.
Answers
Answer:
I’d rather explain it as if you were an intelligent person, which you are.
In simple terms, control flow is the path of actions/events that happen in your program. In the simplest case, the path is linear (one statement is executed after another in the order that they appear in the source code). The code on the left creates the control flow you see on the righT
if...elif...else statements introduce branches in the control flow.
Let a, b, and c be conditions (Boolean expressions that can be either True or False, such as x < 3). Then an if...else makes a branch:
The program will only do this if a is True, and do that instead if a is False.
Each elif lets you introduce another branch:
The important thing to note here is that, in order to get to do that, not only b has to be True, but a has to be False. And that’s the difference between an if...elif...else and a series of ifs:
if a:
do this
elif b:
do that
elif c:
do something else
else:
print "none of the above"
is equivalent to (exactly the same as) this:
if a:
do this
if (not a) and b:
do that
if (not a) and (not b) and c:
do something else
if (not a) and (not b) and (not c):
print "none of the above"
As you can see, the elif...else notation allows you to write the same logic (the same control flow) without repeating the a condition over and over again.
PLEASE REFER THE PICTURES BELOW
Explanation: