Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between multiple if's and elif's?

In python, is there a difference between say:

if text == 'sometext':     print(text) if text == 'nottext':     print("notanytext") 

and

 if text == 'sometext':         print(text)  elif text == 'nottext':         print("notanytext") 

Just wondering if multiple ifs could cause any unwanted problems and if it would be better practice to use elifs.

like image 340
Billjk Avatar asked Feb 14 '12 04:02

Billjk


People also ask

What is the difference between using Elseif rather than just more if's?

Summary: By using an ELSE IF structure instead of multiple IF we can avoid “combined conditions” ( x<y && y<z ). Instead we can use a simplified condition (y<z). Furthermore ELSE IF is more efficient because the computer only has to check conditions until it finds a condition that returns the value TRUE.

What is the difference between multiple if statements and else if?

If statements are executed independent of one another; each one will run. Else if statements only execute if the previous if s fail.

Is there a difference between if and Elif?

The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed.

Can you have more than one Elif?

There can be multiple 'elif' blocks, however there is only 'else' block is allowed. 2. Out of all these blocks only one block_of_code gets executed. If the condition is true then the code inside 'if' gets executed, if condition is false then the next condition(associated with elif) is evaluated and so on.


1 Answers

Multiple if's means your code would go and check all the if conditions, where as in case of elif, if one if condition satisfies it would not check other conditions..

like image 120
avasal Avatar answered Oct 22 '22 16:10

avasal