+ 1
Why elif if just "if" works
Can someone explain why I should use "elif" instead just "if". For example this code just works as well for me and I don't understand why I need "elif" X=7 If x==5: print("five") If x==11: print("eleven") If x==7: print("seven") else: print("it's not 5, 7 or 11")
5 Respuestas
+ 4
If you just use if the program will have to go through and check every if condition vs if you use if elif elif elif else, if one of the statements are true it'll skip the rest of the statements below in the block. Its saving time for the program, like processing and execution or something like that.
+ 4
In addition to what Kilowac said, there are legitimate uses. Elif is evaluated when the first if-condition evaluates to false and you want to check for additional conditions
+ 1
If there is so many if else then better solution is use switch case. It save time to execution because when any condition will true then it will go outside the switch case because of break statement.
But in if else statement it check every condition so it will take time.
0
if you use elif rather than if, you can be free of indentation erro, but if you use if statement, you should very careful about indentation.
- 1
In a python program you can not use multiple if's, only one though. But with elif you can use them multiple times.