What is the point of the elif statment?

  • Thread starter Thread starter Cash Fulton
  • Start date Start date
  • Tags Tags
    Point
Click For Summary

Discussion Overview

The discussion revolves around the purpose and utility of the `elif` statement in programming, particularly in Python. Participants explore its role in control flow structures, comparing it to traditional `if` and `else` statements, and discussing its necessity in handling multiple conditions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants argue that `elif` is essential for choosing among multiple alternatives in a clean and readable manner, especially in languages like Python where indentation is significant.
  • Others point out that compiled languages typically do not have an `elif` statement, instead using a structure like `else if`, which serves a similar purpose.
  • A participant highlights that using multiple `if` statements instead of `elif` can lead to logical differences in the flow of execution, indicating that they are not equivalent in certain scenarios.
  • One participant references Python's PEP8 guidelines to emphasize the importance of `elif` in maintaining code readability and structure.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and functionality of the `elif` statement, with no consensus reached on whether it can be effectively replaced by multiple `if` statements.

Contextual Notes

Some limitations in the discussion include the dependence on specific programming languages and their syntax rules, as well as the varying interpretations of logical equivalence in control flow statements.

Cash Fulton
Messages
24
Reaction score
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.
 
Technology news on Phys.org
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.
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):
C:
if (val == opt1)
{
   // Option for opt1
}
else if (val == opt2)
{
   // Option for opt2
}
else
{
   // Otherwise do this
}
If my code has to choose among more possibilities, I can add more else if clauses (or elif in python).
 
Compiled languages tend not to have an elif statement, or something similar. They instead use something along the lines of

C:
if (condition) {
    do_this;
} else if (other_condition) {
    do that;

The elif statement, or something similar, is much more common in scripting languages. With python, it's almost essential because of the indentation rules and PEP8, the python indentation standard. Consider the following:
Python:
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

Many other languages have a switch statement. Python doesn't. You either need to use a dictionary, an incredibly ugly stair-stepped if ... else if ... else if ... sequence of tatements, or you could use elif.
 
See 4.1 in https://docs.python.org/3/tutorial/controlflow.html for justification.

Can't the if statement be replaced with just if?

This doesn't make sense. I'm guessing you mean "Can't the elif statement be replaced with just if?" If so, the following are not logically equivalent:

Code:
if A:
  a()
elif B:
  b()
else:
  c()

if A:
  a()
if B:
  b()
else:
  c()

In the latter block, the first test is independent of the second.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 22 ·
Replies
22
Views
6K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K