用AND替换嵌套的if语句 [英] Replacing Nested if Statement With AND

查看:152
本文介绍了用AND替换嵌套的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道if是否比AND语句更好.我有一个循环运行了很多次,因此我正在考虑可以更快地执行.以下是与我的代码具有相同逻辑的代码.嵌套的if语句在循环内.

I am wondering whether nested if is better than AND statement. I have a loop that goes so many times so I am thinking of faster execution available. Below is the code that has same logic with my code. The nested if statement is inside a loop.

   for ( int i = 0; i < array.length; i++)
   {
      // do stuff
      if (x == 5)
      {
         if (y == 3)
         {
             // do stuff
         }
      }
   }  

如果我用this和STATEMENT替换嵌套的if,我的代码会更快吗?

Will my code be faster by a significant difference if I replace the nested if with this And STATEMENT?

     if ((x == 5) && (y == 3))
           // do stuff

我已经阅读了此链接,但没有找到答案.我是一名学生,仍在学习,感谢您的所有反馈!

I have read this link but I didn't find the answer. I am a student and still learning, thanks for all the feedback!

推荐答案

.NET将停止检查条件的第一部分是否为false,因此两者之间没有性能差异.

.NET will stop checking if the first part of the conditional is false, so there will be no performance difference between the two.

这篇关于用AND替换嵌套的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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