0

I was wondering why we write "elif", if we could just write "if" for a "elif" statement.

Just a question of curiosity, why do we write "elif e == (f + 10)" instead of just "if e == (f+10)"?

e = 20

f = 10

elif e == (f + 10):
    print("e is 10 more than f")

Why can't I replace the "elif" with "if" and tell me why that wont work if I replace with my adulation.

2
  • 2
    we don't. we use 'if' in this case. Commented Dec 29, 2018 at 3:55
  • 7
    I'm not sure what you're asking. The posted code is invalid, because there is no if before the elif. Commented Dec 29, 2018 at 3:55

3 Answers 3

2

‘elif’ stands for ‘else if’ so basically the reason why you wouldn’t simply just use an ‘if’ statement is because say you have an initial ‘if’ statement that the program passes through and satisfies the condition, it will not pass through to the ‘elif’ statement following. If you had two ‘if’ statements after another then both will be passed through.

However an elif statement must follow an if statement, unlike the way you’ve used in your example

Sign up to request clarification or add additional context in comments.

Comments

1

elif always has to be after a if, elif is really:

  • a representation of else and if mixed together.

Usually people use it in a if then elif then else, it's really standing for:

  • if the first condition (with if) is incorrect, do this condition, if it is still incorrect, got to else and if there is no else, just don't do anything and exit condition.

P.S. after all i forgot to tell you that the code is invalid.

Comments

0

imagine you have to check multiple conditions. so in the if case, you can only check one condition. but with elif you can check multiple conditions.

if (x==1):
    print("x is one")
elif (x==2):
    print("x is two")
else :
    print("x is not valid")

In here we've checked whether x=1 or x=2 and wrote the code relevantly. I wrote the same code (print()) , but you could write whatever is necessary in each condition. That's the point of having it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.