# The elif statement in Python

**URL:** <https://boards.straightdope.com/t/the-elif-statement-in-python/931533>\
**Category:** Factual Questions\
**Created:** [January 26, 2021, 3:42pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533 "2021-01-26T15:42:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Schnitte](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/schnitte/32/9033_2.png) [@Schnitte](https://boards.straightdope.com/u/Schnitte)\
**Post date:** [January 26, 2021, 3:42pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533/1 "2021-01-26T15:42:23Z")

</div>

[Wikipedia](https://en.wikipedia.org/wiki/Conditional_(computer_programming)#Else_if) gives the following example for and else-if construction in programming (I don’t know which language exactly this is, from the context of the Wiki article I would suppose it’s Ada):

> if condition then  
> –statements  
> elseif condition then  
> – more statements  
> elseif condition then  
> – more statements;  
> …  
> else  
> – other statements;  
> end if;

Now obviously all the statements introduced by _elseif_ are executed if and only if the preceding conditions introduced by _if_ or _elseif_ are not met.

But note the _end if_ at the very end of this snippet. I suppose this marks the end of the block introduced by the initial _if_. After this _endif_, the _if_ condition, on which all subsequent _elseifs_ hinged, does not matter anymore.

But Python (where the equivalent to _elseif_ above is _elif_) does not seem to require an _end if_ - at least I’ve tried code that had _if_ blocks but no _end if_, and it ran fine. So where does the block end in which the initial _if_ determines what the subsequent _elifs_ refer to? I cold certainly mark it with an _else_, but that seems optional. It can’t be a matter of indentation either - in the _elif_ examples that I’ve seen, that command is not indented to the right of the preceding _if_ but stands at the same level of indentation. So am I correct in assuming that the _if_ condition that defines subsequent _elifs_ is only the immediately preceding one, and that earlier _ifs_ at a previous point in the program don’t affect the meaning of a subsequent _elif_ anymore as soon as a new _if_ has intervened?

---

<div class="post-metadata">

**Author:** ![AlsoNamedBort](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/alsonamedbort/32/3213_2.png) [@AlsoNamedBort](https://boards.straightdope.com/u/AlsoNamedBort)\
**Post date:** [January 26, 2021, 3:57pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533/2 "2021-01-26T15:57:27Z")

</div>

> [@Schnitte](#):
>
> It can’t be a matter of indentation either - in the _elif_ examples that I’ve seen, that command is not indented to the right of the preceding _if_ but stands at the same level of indentation. So am I correct in assuming that the _if_ condition that defines subsequent _elifs_ is only the immediately preceding one, and that earlier _ifs_ at a previous point in the program don’t affect the meaning of a subsequent _elif_ anymore as soon as a new _if_ has intervened?

Indentation does matter. **elif** will reference the previous **if** as long as there’s no other unindented line in between them. If there is one, it’s a syntax error.

---

<div class="post-metadata">

**Author:** ![naita](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/naita/32/5862_2.png) [@naita](https://boards.straightdope.com/u/naita)\
**Post date:** [January 26, 2021, 4:07pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533/3 "2021-01-26T16:07:59Z")

</div>

This is fairly easily tested, is it not? Just open a python terminal and try it out.

elif is just a concatenation of else and if. The else to an if has to be the next statement at the same level of indentation as the if. Multiple consecutive elif just chains this, so the else of the next elif belongs with the if of the previous one.

---

<div class="post-metadata">

**Author:** ![Pleonast](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/pleonast/32/1183_2.png) [@Pleonast](https://boards.straightdope.com/u/Pleonast)\
**Post date:** [January 26, 2021, 4:10pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533/4 "2021-01-26T16:10:11Z")

</div>

> [@AlsoNamedBort](#):
>
> Indentation does matter. **elif** will reference the previous **if** as long as there’s no other unindented line in between them. If there is one, it’s a syntax error.

Yes.

In Python, the if, elif, and else lines must all be at the same indentation.

---

<div class="post-metadata">

**Author:** ![naita](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/naita/32/5862_2.png) [@naita](https://boards.straightdope.com/u/naita)\
**Post date:** [January 26, 2021, 4:14pm UTC](https://boards.straightdope.com/t/the-elif-statement-in-python/931533/5 "2021-01-26T16:14:33Z")

</div>

Or more general, in python all code blocks end when you drop a level of indentation.

```
statement a
    more statements
    even more statements
statement in new block

```

in python is equivalent to

```
statement a {
    more statements
    even more statements
}
statement in new block

```

in a language that use brackets to define code blocks. In that language the indentation of the second and third line is just for readability, in python they are what defines the block. And if “statement a” is an if statement you can’t throw in an else anywhere later in the code other than right after the end of the block defined for the if-statement.
