简体   繁体   English

嵌套if和&&之间有什么区别?

[英]What are the differences between nested if and &&?

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. 嵌套if在这方面为您提供了额外的灵活性。

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. 只需在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?
    }
}

else if条件不同,它会对else和嵌套的else if产生影响,但除了可读性之外没有区别。

In the first case, whatever code is executed in the block is executed for any case evaluating to true. 在第一种情况下,对于任何评估为true的情况,执行块中执行的任何代码。 In the second If statement, there may be cases where the first condition equates to true, but the second does not. 在第二个If语句中,可能存在第一个条件等于true但第二个条件不等于true的情况。 and there may be a need for the developer to : 并且开发人员可能需要:
- execute some code if one condition is met, - 如果满足一个条件,执行some code
- and then more code where the second condition is met - 然后是满足第二个条件的more code
- 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]) - 没有代码如果只满足第二个条件而不是第一个条件(例如:condition1 =球没有死,条件2 =球进入网[ more code将在足球比赛中注册进球])

the code: 编码:

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

which is better? 哪个更好?

that depends on what you're using it for 这取决于你使用它的是什么

语法上 不同语义 相同

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 ). //some code如果condition1condition2计算为非零( true ),则只会执行//some code

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. 第一个, if(condition 1 && condition 2) ,只能包含一个else部分,在假设两个条件都正确或两者都错误的情况下提供代码。 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.. 在另一部分中,您可以有两个其他部分,每个部分用于每个条件的失败,从而增加问题的范围。

他们做同样的事情,并按相同的顺序进行评估。

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. 两者都有相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM