Performance is considered here - in some cases, the CPU can compare with 0 faster than with 1. A smart compiler can optimize this, but comparing with 0 is usually better when possible.
EDIT: A little clarification on this issue - Processors have a flag of "zero", which is set when the result of the operation of an arithmetic operation or comparison leads to a value of zero. There is also a "negative" flag. The “compare” command is actually more or less identical to the “subtract” instruction, except that the result is not saved, but the flags are set.
It depends on the context, but if this variable was just set as a result of an arithmetic operation, and now it is 0, the zero flag will already be set, and no comparison command will be needed to determine if x> 0. When it is 1 , you must perform a comparison with constant 1 to set the flag to zero, and to fulfill the condition.
For example, for (pseudo-code) some compilers (I saw that Delphi does this) will optimize
for x = 0 to 10 { print "hello world " }
to
for x = 10 down to 0 { .. }
simply because it doesn’t need to “compare x with 10” each time, since the zero flag is already set at the last iteration as a result of decreasing x. Of course, this can only be done if there is no link in the loop, otherwise it will change functionality.
Wikipedia has received further clarification regarding the zero flag: http://en.wikipedia.org/wiki/Zero_flag
source share