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

  • 14


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


如果等于这个,程序会更慢,我觉得应该等同于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)

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 ,这两种都一样