What is the difference between >0 and >=1?

syntax

Solution

There's a performance consideration here- a CPU can in some circumstances compare with 0 faster than with 1. A clever compiler might be able to optimize this, but comparing to 0 is generally a better practice where possible.

EDIT: A bit of clarification on this- Processors have a 'zero' flag that gets set when the result of an arithmetic operation or compare instruction results in a value of zero. There's also a 'negative' flag as well. A 'compare' instruction is actually more or less identical to a 'subtract' instruction, except the result is not stored, but the flags are set.

It does depend on context, but if that variable had just been set as a result of an arithmetic operation, and it's now 0, the zero flag will already have been set, and no compare instruction is necessary to determine if x > 0. When it's 1, it's necessary to do a compare with the constant of 1 to set the zero flag, and satisfy the condition.

For (pseudocode) example, some compilers (I've seen Delphi do 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 do 'compare x with 10' every time, as the zero flag is already set on the last iteration as a result of decrementing x. Of course it can only do this if x isn't referenced within the loop, otherwise it'll change the functionality.

Wikipedia's got further clarification on the 'zero' flag: http://en.wikipedia.org/wiki/Zero_flag

Problem

The title says it all. Is there any reason why some professors and programmers in general write ``` if ( x >= 1 ) ``` instead of ``` if ( x > 0 ) ``` ?

Original source