Does this condition:
if (x > 1)
//some logic
else if (y >47)
//some logic
else
//default logic
Does this always translate t开发者_开发知识库o :
if (x >1)
{some logic}
else{
if (y >47)
{some logic}
else
{default logic}
}
Or are there exceptions? If no exceptions, then is there a best practice of how to write this?
These forms are logically equivalent, yes. I think the best practise is to write the code so it is as easy to understand as possible.
Consider:
if (A) { // code #1 }
else if (B) { // code #2 }
else { // code #3 }
The truth table is then
A B code
T T #1 *
T F #1 *
F T #2
F F #3
Which is the same as for
if (A) { // code #1 }
else {
if (B) { // code #2 }
else { // code #3 }
}
*
NB: Most languages will short-circuit these cases and not evaluate B at all, but some do. This doesn't affect the logic though.
Presumably what you're asking is whether an else
is always associated with the most recent if
for which an else
was not already found.
If so, then the answer is yes for all languages of which I'm aware.
As for alternatives, it depends. Just for example, some languages allow arbitrary expressions in their case
or switch
statement. If that's allowed, it's generally preferable to an if/then/else ladder unless the number of possible choices is very small.
Depending on what you're testing in the condition, you might be able to use a polymorphic type instead (assuming your language supports that).
no, not necessarily. In plenty of languages, only one statement will be considered inside the if statement without the braces. If you use the braces, all statements within will be considered inside.
Easier-to-understand code would be:
if (x >1)
{some logic}
elseif (y >47)
{some logic}
else
{default logic}
(and your original post was missing a }
).
Much depends upon the language you are using. Some use 'else' to terminate the context of an 'if' (Cobol comes to mind), others use the end of the first statement following the 'if' to terminate its context (all the C and Java flavored languages).
Without knowing a specific language, a good general rule is to always use the explicit terminators, in this case { } to explicitly declare the scope.
In some languages it does. (Almost all high level languages are this way; else if, elseif, or elsif act as keywords meaning something along the lines of "subsequent condition statements.") In some other languages, you need to use the braces to indicate scope.
Best practice depends entirely on the language you're writing in, who you're writing for (work, school, yourself), and magic smoke. (Not really on the magic smoke, but sometimes it seems like it.)
When coding for someone else, ask them what the best way to do it is. When I code for myself, I find that lots of curly braces make my code less easy for me to understand, so I like using else if
statements in most of my code. Some languages (like Java) use it frequently in their own documentation, which is usually a pretty good sign what the "accepted" standards are.
is there a best practice of how to write this?
With two if conditions as you have it here, there is not much advantage in either form. Beyond that, I would definitely go with the flat left-aligned form. Code with more than three levels of indentation is hard to follow, and is almost always an indication that something needs to be broken into multiple methods. If it's just a sequential series of if conditions, then there is no reason to signal complexity with large indentations.
This should be the code which is optimized:
if(x > 1)
{do something and return}
if(y > 47)
{do something and return}
do something
精彩评论