0

why do i need to use elif?

I find that the same result could also be got by only writing two or more if-statements.

23rd Dec 2019, 4:48 PM
DavidYAi
2 Antworten
+ 1
well...one difference is that with multiple if statements every condition is checked everytime the program runs through them...with elif statements when a condition is found true, the follow up conditions are not checked...lets say you have x = 3 and y = 4 if x == 3 //something if y == 4 //something else in this scenario both if statements are true so both are executed but if we wrote if x == 3 //something elif y == 4 //something else it is found that the first condition is true and so only that condition's body is executed...the program never checks if y == 4 is true or not because × == 3 is true if x == 3 was false than and only than the condition y == 4 is checked
23rd Dec 2019, 5:13 PM
George Samanlian
0
Yes, for sure. But with elif it's shorter: if statement1: some code else: if statement2: some code if statement1: some code elif statement2: some code
23rd Dec 2019, 5:04 PM
Coding Cat
Coding Cat - avatar