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

ii_qooc
  • 14

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

回复
阅读 3.6k
6 个回答
✓ 已被采纳

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.

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

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

a > b || a == b

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏