What is the point of the elif statment?

  • Thread starter Thread starter Cash Fulton
  • Start date Start date
  • Tags Tags
    Point
AI Thread Summary
The elif statement, short for "else if," is essential for handling multiple conditions in programming, particularly in Python. It allows for a cleaner and more readable structure when dealing with three or more alternatives, as opposed to a nested series of if and else statements. This is especially important in Python due to its indentation rules and PEP8 standards, which promote code readability. In contrast to compiled languages that may use similar constructs, Python's lack of a switch statement makes elif a practical choice for managing multiple conditions without excessive nesting. The logical structure of elif also differs from using multiple independent if statements, ensuring that only one block of code executes based on the first true condition, which enhances code efficiency and clarity.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
18
Views
3K
Replies
22
Views
5K
Replies
13
Views
1K
Replies
2
Views
2K
Replies
4
Views
2K
Back
Top