4

What are the differences between writing:

if(condition1 && condition2){
    //some code
}

And:

if(condition1){
    if(condition2){
        //some code
    }
}

If there are any, which one is better?

4

10 回答 10

9

The differences are mainly in readability and maintenance.

Concatenation of 2 logical conditions should usually imply that there is a semantic relation between them.

Another thing to consider is the scoping. The nested if gives you additional flexibility in this area.

于 2013-08-06T11:26:56.000 回答
3

Others have already said what need be said. They are essentially same. The first one is condition check in one liner. and second can be used for more verbose checks. second one can prove good when debugging. eg. just add a debug action on false case. Following is a fun little example i can think of

if("shell we meet today?" && "would you like some coffee?")
{
     //Check both at the same time.
     /*
      *Sequential order of evaluation for && ensures that 
      *left hand side operand (condition) gets evaluated first
      */
     //take some chocolates with the coffee
}

if("shell we meet today?")
{  
    //then
    //if you want to figure out the options first
    if("would you like some coffee?")
    {
        //take some chocolates with the coffee
    }
    else if("how about lunch?")
    {
           //Pizza?
    }
    else
    {
           //some fall back scenario?
    }
}
于 2013-08-06T12:24:08.893 回答
2

It will have an impact on else & nested else if conditions but other than that no difference except readability.

于 2013-08-06T11:27:15.813 回答
2

In the first case, whatever code is executed in the block is executed for any case evaluating to true. In the second If statement, there may be cases where the first condition equates to true, but the second does not. and there may be a need for the developer to :
- execute some code if one condition is met,
- and then more code where the second condition is met
- no code If only the second condition is met and not the first (eg: condition1 = ball is not dead AND condition2 = ball entered net [where more code would register a goal in a soccer game])

the code:

if(condition1){
   //some code
   if(condition2){
       //more code
   }
}

which is better?

that depends on what you're using it for

于 2013-08-06T11:33:09.203 回答
1

Syntaxically different and semantically same.

于 2013-08-06T11:24:25.267 回答
1

There's no functional difference between each way. It is likely that the compiler will optimise them down to the same machine code (though it is not guaranteed).

于 2013-08-06T11:25:03.493 回答
1

Effectively, there is no difference; //some code will only be executed if both condition1 and condition2 evaluate to non-zero (true).

于 2013-08-06T11:25:44.997 回答
1

The first one, if(condition 1 && condition 2) , can contain only a single else part, providing your code to work in the assumption that both conditions are right, or both are wrong. Where as in the other part, you can have two else parts, each for the failure of each conditions, thus increasing the scope of the problem..

于 2013-08-06T11:33:34.827 回答
0

They do the same and are evaluated in the same order.

于 2013-08-06T11:22:45.170 回答
0

For me nothing special but the second one makes the code more nested, which I would like to avoid in general. Both have same effects.

于 2013-08-06T11:30:38.010 回答