What is the point of the elif statment?

  • Thread starter Cash Fulton
  • Start date
  • Tags
    Point
In summary: In the former block, B is only tested if A is false.In summary, the elif statement, short for else if, is used to choose among three or more alternatives in scripting languages, such as Python. It is essential for adhering to indentation rules and PEP8 standards in Python. While other languages may have a switch statement, Python does not, making elif a necessary alternative. Replacing elif with multiple if statements is not logically equivalent and can result in different outcomes.
  • #1
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.
 
Technology news on Phys.org
  • #2
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).
 
  • #3
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.
 
  • #4
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.
 

FAQ: What is the point of the elif statment?

1. What is the elif statement used for?

The elif statement is used in programming to add an additional condition to an if statement. It allows for multiple conditions to be checked and different blocks of code to be executed based on those conditions.

2. How is the elif statement different from the if statement?

The elif statement is different from the if statement in that it is only executed if the preceding if statement is not satisfied. It is a way to provide an alternative condition to be checked if the first condition is not met.

3. Can multiple elif statements be used in a single if statement?

Yes, multiple elif statements can be used in a single if statement. This allows for more complex and specific conditions to be checked and different blocks of code to be executed based on those conditions.

4. Is the elif statement necessary in programming?

No, the elif statement is not necessary in programming but it can make code more efficient and readable. It is especially useful when there are multiple conditions that need to be checked.

5. Are there any alternatives to using the elif statement?

Yes, instead of using the elif statement, multiple if statements can be used to achieve a similar result. However, this can result in longer and more repetitive code, making the elif statement a more efficient option in most cases.

Similar threads

Replies
2
Views
925
Replies
18
Views
2K
Replies
22
Views
4K
Replies
13
Views
1K
Replies
2
Views
2K
Replies
4
Views
2K
Back
Top