如果condition:使用条件OR或在以下情况下重复调用则更好 [英] Which is better if condition : using conditional OR or calling repeatedly if

查看:89
本文介绍了如果condition:使用条件OR或在以下情况下重复调用则更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

1)反复调用if

Hello friends,

1) repeatedly call if

if (i == 1)
    return false;
if (i == 2)
    return false;
if (i == 3)
    return false;
if (i == 4)
    return false;
if (i == 5)
    return false;
if (i == 6)
    return false;



2)有条件的OR



2) conditional OR

if (i == 1 || i == 2 || i == 3 || i == 4 || i == 5 || i == 6)
                return false;



在这里您可以看到两种不同的逻辑..

我想知道哪种是性能更好的软件,还是更好的程序员,以及为什么.

谢谢



Here you can see two different logic..

I want to know which is better as a performance or as a good programmer and why.

Thanks

推荐答案

在这种情况下,最好使用使用&&(和)的表达式.
In this case you would be better using an expression that uses && (and).


尝试:
switch (i)
   {
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
      return false;
   }

为什么?它更易于阅读,更灵活,执行起来也更快!

Why? It''s easier to read, it''s more flexible, and it''s quicker to execute!


这篇关于如果condition:使用条件OR或在以下情况下重复调用则更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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