The difference between evaluating while (n> 0) and while (n! = 0)

Is the assessment while(n>0)and while(n!=0)the other? Basically, both should exit in the same state. So is there a scenario when one of them should be used? Or can it affect performance by changing the condition of the cycle when the number of cycles that are evaluated is the same?

+4
source share
3 answers

It depends on your platform, but overall it will work the same. For example, for x86 architecture, architecture cmpwill be used both for >and for !=. Here you can read more.

+4
source

, , .

+2

.

In any case, one could maliciously think of special schemes like

while (n > 0)
{
  ...
  n++;
}

vs.

while (n != 0)
{
  ...
  n++;
}

where the compiler can conclude that in the first fragment the test should be performed only at the first iteration and deploy whileto if.

+1
source

Source: https://habr.com/ru/post/1620702/


All Articles