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?
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?
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.
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?
}
}
It will have an impact on else
& nested else if
conditions but other than that no difference except readability.
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
Syntaxically different and semantically same.
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).
Effectively, there is no difference; //some code
will only be executed if both condition1
and condition2
evaluate to non-zero (true
).
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..
They do the same and are evaluated in the same order.
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.