交换 if 语句

发布于 2024-09-30 03:35:09 字数 306 浏览 7 评论 0原文

我正在致力于代码最小化和增强。我的问题是:是否可以在不破坏代码逻辑的情况下交换上面的 if 语句?

    int c1 = Integer.parseInt(args[0]) ;
    int c2 = Integer.parseInt(args[1]) ;
    int c3 = Integer.parseInt(args[2]) ;

    if (c2-c1==0)
      if ( c1 != c3 )

由于两个 if 语句之间没有写入变量的操作,所以我会说是,但我不确定。

有什么想法吗?

I am working on code minimization and enhancement. My question is: is it possible to swap the if statements above without destroying the logic of the code?

    int c1 = Integer.parseInt(args[0]) ;
    int c2 = Integer.parseInt(args[1]) ;
    int c3 = Integer.parseInt(args[2]) ;

    if (c2-c1==0)
      if ( c1 != c3 )

Since between the both if statments are no operations that write the variables I would say yes but I am not sure.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奶茶白久 2024-10-07 03:35:09

我真的不明白为什么你想要 (c2 - c1 == 0) 而不是 c1 == c2

为什么不走

if ((c1 == c2) && (c1 != c3)){
  do_stuff();
}

or

if ((c1 != c3) && (c1 == c2)){
  do_stuff();
}

路线

作为注释,没有在任何地方将 c1 切换为 c2 的惩罚或优势。其中,将最有可能失败的条件放在第一位稍微更有效,因为如果第一个条件失败,则不会评估第二个条件。

另请注意,这是一个微观优化,您永远不应该考虑速度。

第三个注意点,如果你的程序不做任何其他事情,并且如果这个条件不成立则退出,并且你确实想要微优化(你不想),我建议在你知道参数之前不要解析它们需要。如果大多数情况下 c1 不等于 c2,那么您可以等待解析 c3,直到您知道必须对其进行检查。这严格来说是理论。在实践中不要这样做,因为这会让你的代码更难阅读。尽快将所有命令行变量处理为合理的东西会更清晰。

I don't really see why you want (c2 - c1 == 0) instead of c1 == c2

why not go the

if ((c1 == c2) && (c1 != c3)){
  do_stuff();
}

or

if ((c1 != c3) && (c1 == c2)){
  do_stuff();
}

route

As a note, there is no penalty or advantage in switching c1 for c2 anywhere. Of the above, putting the most likely to fail condition first is slightly more efficient, because the second condition will not be evaluated if the first one fails.

Also note, this is a micro optimisation, you should never be considering for speed.

Third note, if your program doesn't do anything else, and exits if this condition doesn't hold, and you really do want to micro-optimise (which you don't) I suggest not parsing the arguments before you know they are needed. if c1 will be unequal to c2 most of the time, then you can wait with parsing c3 until you know you have to check against it. This is strictly theory. Don't do this in practice as it will make your code that much harder to read. It's much clearer to process all the commandline vars to something sensible as soon as you can.

海之角 2024-10-07 03:35:09
int c1 = Integer.parseInt(args[0]) ;
int c2 = Integer.parseInt(args[1]) ;
int c3 = Integer.parseInt(args[2]) ;

if (c2 == c1 && c1 != c3 ) {
   ...
}
int c1 = Integer.parseInt(args[0]) ;
int c2 = Integer.parseInt(args[1]) ;
int c3 = Integer.parseInt(args[2]) ;

if (c2 == c1 && c1 != c3 ) {
   ...
}
木緿 2024-10-07 03:35:09

是的。
您还可以编写 if ((c2-c1==0) && (c1 != c3))

Yes.
You can also write if ((c2-c1==0) && (c1 != c3)).

酒浓于脸红 2024-10-07 03:35:09

如果您的意思是

if ((c1 != c3) && (c2-c1==0))

并且您不打算仅在 c2-c1==0 时做一些特殊的事情,那么是的。

If you mean

if ((c1 != c3) && (c2-c1==0))

and you do not plan to do something special only if c2-c1==0 then yes.

苄①跕圉湢 2024-10-07 03:35:09

如果不涉及其他块,那么我建议您将其写为:

if (c2-c1==0 && c1 != c3 )

或者,如果您想交换它们,

if (c1 != c3 && c2-c1==0)

If there are no else blocks involved, then I suggest you to write it as:

if (c2-c1==0 && c1 != c3 )

or, if you want to swap them,

if (c1 != c3 && c2-c1==0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文