Cash Fulton
- 24
- 1
What's the point of the elif statement? Why not just keep the if and else statements and leave it at that? Can't the if statement be replaced with just if? Thanks.
It's short for else if. A simple if ... else statement is used to choose one of two possible alternatives. If you have to choose among three or more alternatives you want a control structure that looks like this (using C rather than python, though):Cash Fulton said:What's the point of the elif statement? Why not just keep the if and else statements and leave it at that? Can't the if statement be replaced with just if? Thanks.
if (val == opt1)
{
// Option for opt1
}
else if (val == opt2)
{
// Option for opt2
}
else
{
// Otherwise do this
}
if (condition) {
do_this;
} else if (other_condition) {
do that;
class Foo :
def fun (self, val) :
if val == 0 :
self.handle_zero ()
else :
if val == 1 :
self.handle_one ()
else :
if val == 2 :
self.handle_two ()
else :
# Lines are supposed to be at most 79 characters long in python.
# Do you see how quickly I'm running out of space?
# There are lots of enums with 16 or more members.
pass
Can't the if statement be replaced with just if?
if A:
a()
elif B:
b()
else:
c()
if A:
a()
if B:
b()
else:
c()