I am writing a program in where one function requires 4 if statements, is it better to write this like this
or is it better to doCode:if(statement) code ... if(statement) code ...
Code:if(statement) code .... else if(statement) code ....
I am writing a program in where one function requires 4 if statements, is it better to write this like this
or is it better to doCode:if(statement) code ... if(statement) code ...
Code:if(statement) code .... else if(statement) code ....
It depends on the actual code.
If the statement in the if() statements are related and is one of several choices, then the first might be the better choice. If the statements are not related than the second is probably better.
Again, we would need to see actual code to better advise.
Every if is evaluated.
The first if to evaluate to true breaks the chain.Code:if(statement) code ... if(statement) code ... if(statement) code ... if(statement) code ...
Code:if(statement) code .... else if(statement) code .... else if(statement) code .... else if(statement) code ....
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.