a>=b 是 先判断a是否等于b 还是先判断a是否大于b?

发布于 2022-09-07 09:05:46 字数 70 浏览 28 评论 0

换句话说,
a>=b
等同于
a==b||a>b
还是
a>b||a==b?

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

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

发布评论

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

评论(6

不再见 2022-09-14 09:05:46

Update:

如果等于这个,程序会更慢,我觉得应该等同于a == b || a > b才对。

That's not true. Your platform will not split >= into == and >. The implementation varies from platform to platform and compiler to compiler. From Assembly(https://godbolt.org/g/6MCvQq):

if (a >= b)

=>

  mov eax, dword ptr [rbp - 12]
  cmp eax, dword ptr [rbp - 16]
  jl .LBB0_4

   if (a > b)

=>

   cmp r8d, dword ptr [rbp - 16]
   mov dword ptr [rbp - 20], eax # 4-byte Spill
   jle .LBB0_2

Both have 3 instructions.

Also take a look at these awesome answers: https://stackoverflow.com/que...


Both are true. || is logic or operator in some languages(which belongs to logic operators). I assume you have limited the symbols to c++(because you didn't use the more general terms like logic or and logic and. In standard ml, logic or is expressed as or, same for logic and)

In c++, please correct = to ==. The former is a Direct assignment operator, while the latter is Equal to operator. If a satisfy the condition, the b will not be used. For example:

if (a && b)

is equal to

if (a)
    if(b)
        ...
        

BTW, in some algorithms, if you want to judge if a node exists (like arr[x][y] == 'x'), it should be put in b rather than a. Because sometimes a is used to filter out-of-range conditions.

入怼 2022-09-14 09:05:46

都对,|| 有一个为真就是真,

古镇旧梦 2022-09-14 09:05:46

a > b || a == b等价于a == b || a > b||有一个真就为真。

谁许谁一生繁华 2022-09-14 09:05:46
a > b || a == b
日久见人心 2022-09-14 09:05:46

a > b || a == b, a == b||a > b ,这两种都一样

凝望流年 2022-09-14 09:05:46

为啥我觉得这两个没区别啊

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