-6

Why should I use an elif instead of using if statements over and over again. I can't find any documentation on the matter. Thank you in advance.

Samuel Focht
  • 53
  • 1
  • 3
  • you really only need it if there are >= 3 conditions and one of them is an `else` – Paul H Aug 02 '16 at 15:16
  • One big reason is that multiple `if` statements are considered independent statements and therefore would each get checked, regardless of if one passes the condition in the chain. In an `if-elif` chain, the condition checking is over if any of the statements pass. – wkl Aug 02 '16 at 15:19
  • @PaulH Not necessarily, there are many good reasons to use `if / elif` without an `else`, even for only 2 conditions – MrAlexBailey Aug 02 '16 at 15:19
  • 2
    @Yvette It's a dupe of [multiple](http://stackoverflow.com/questions/22782785/when-to-use-if-vs-elif-in-python) [questions](http://stackoverflow.com/questions/9271712/difference-between-multiple-ifs-and-elifs-python). – wkl Aug 02 '16 at 15:20
  • What I find nowhere in the answers to previous questions is the connection between Pythons use of mandatory indentation and the fact that elif is really needed (as explained in my answer, and as opposed to e.g. in C, C++, Java and JavaScript). This is also why the C/C++ preprocesor needs an #elif but the language itself doesn't have an elif. So perhaps we could consider this question not a complete duplicate having many complete answers. – Jacques de Hooge Aug 02 '16 at 15:28
  • Thank you for all of the information. I know that it was best practices but I couldn't find the reasoning. Thank you for all the answers. – Samuel Focht Aug 02 '16 at 15:46

5 Answers5

5

You use if, elif and else if you want one and only one of the cases to execute. In other words, the cases are mutually exclusive.

It also allows for short-circuiting logic. Upon find the first if of elif that is True, you can skip checking the conditions for all following cases. If you simply had a series of if statements, you'd have to check all of their conditions no matter what.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
4

Consider the differences, between the first if/elif group and the second group of ifs:

x = 0

if x == 0:
    print("Zero")
elif x % 2 == 0:
    print("Even")
elif x % 2 == 1:
    print("Odd")

print('---')

if x == 0:
    print("Zero")
if x % 2 == 0:
    print("Even")
if x % 2 == 1:
    print("Odd")

In the first, output will only be:

Zero

In the second, output will be:

Zero
Even

By using if/elif, once a condition has been met, the rest of that "ladder" will be skipped. By using a bunch of individual ifs, each will be tested, and a single variable can meet multiple conditions (as x does here).

There certainly are reasons for each, but it's important to know the differences

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Thank you. To save my life i couldn't find any reasoning. I knew it was best practice but I didn't understand why. Thanks again!!!! – Samuel Focht Aug 02 '16 at 15:41
2

The elif statement is only called it the first if statement is false. Consider the following example:

if foo==2:
   print("foo is exactly 2")
elif foo%2 == 0:
   print("foo is not 2 but divisible by 2")

If you would use two if statements, in the case of foo==2, both messages would be printed.

Rafael Albert
  • 445
  • 2
  • 8
1

The fact that Python has an elif, whereas other languages using {} to delimit blocks don't, is indentation. Without it you would have to have a very wide screen in some cases...

You'd have to write:

if a:
    pass
else:
    if b:
        pass
    else:
        if c:
            pass
        else:
            pass

Rather then:

if a:
    pass
elif b:
    pass
elif c:
    pass
else:
    pass

Languages with that don't use mandatory indentation would allow you to write:

if (a) {
    // pass
}
else if (b) {
    // pass
}
else if (c) {
    // pass
}
else {
    // pass
}
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
0

When you write:

if a:
    do_something1()
if b:
    do_something2()

the two conditions are independent of each other. If both a and b evaluate to True, therefore, both do_something* function calls will be made. If instead you write

if a:
    do_something1()
elif b:
    do_something2()

then if a evaluates to True then do_something1 will be callsed, but not do_something2 - even if b is also true. So the elif construct allows you to easily specify mutually exclusive conditions.

holdenweb
  • 33,305
  • 7
  • 57
  • 77