嵌套if和&&amp ;?之间有什么区别? [英] What are the differences between nested if and &&?

查看:176
本文介绍了嵌套if和&&amp ;?之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写作之间有什么区别:

  if(condition1&& condition2){
//一些代码
}

并且:

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

如果有,哪一个更好?

解决方案

差异主要在于可读性和维护性。



2个逻辑条件的连接通常意味着它们之间存在语义关系。 / p>

另一件需要考虑的事情是范围界定。嵌套的 if 为您提供了额外的灵活性。


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和&&amp ;?之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆