0

What is the difference between using 'if' repeatedly and using 'elif' in Python?

14th Mar 2017, 9:00 PM
Shibaayan Maity
Shibaayan Maity - avatar
6 ответов
0
Well to make it a simple way to explain If "this is a question" Give an answer Else Say I don't know Simple if else. elif is a little bit different If "This is a question" "Give answer 1" elif "Give answer 2" elif "Give answer 3" else "Say I don't know I know its not the code but as you can see there is a vital difference, you want to give multiple IF states with elif. while if its IF you give a IF and ELSE statement.
14th Mar 2017, 9:03 PM
Simen Daehlin
Simen Daehlin - avatar
+ 14
Multiple 'if' statements get checked one after another with no regard to the former's results. 'Elif' acts as 'else if', so is checked only in the 'else' case - that is only then, if the first 'if' is False. If the first 'if' is True, all 'elif' statements are skipped and not checked anymore.
14th Mar 2017, 9:26 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 9
By using if repeatedly you will ask all if conditions, no matter if any other before that was true. With elif you evaluate the first if, and if it is false THEN evaluate the second, if both of them were false THEN the third and so on, if any of them was true you stop evaluating the next conditions
14th Mar 2017, 9:19 PM
C.E.
C.E. - avatar
+ 2
Now I got it! Thanks.. 😋
14th Mar 2017, 9:22 PM
Shibaayan Maity
Shibaayan Maity - avatar
0
Here's the difference: (if - if - else) x = input() if x = 1: print("x is equal to 1") if x != 2: print("x is not equal to 2") else: print("poop") say that we input 1 the output would be: x is equal to 1 x is not equal to 2 however if we use elif instead of another if: (if - elif - else) x = input() if x = 1: print("x is equal to 1") elif x != 2: print("x is not equal to 2") else: print("poop") say that we input 1 the output would only be: x is equal to 1 just supplementing what kuba said. YAY!
16th Feb 2019, 4:02 AM
Wikibear
Wikibear - avatar
- 3
Actually I want to say .... if - requires a condition, can be repeatedly used using different conditions. elif - also requires a condition, can be repeatedly used using different conditions. I found no difference between using is statement repeatedly and using elif statement. Like we can use 'if' statement also in case of 'elif'...so what is the necessity of using 'elif'?
14th Mar 2017, 9:08 PM
Shibaayan Maity
Shibaayan Maity - avatar