### 3.4. Comparison Errors

Are there any comparisons between variables having different datatypes, such as comparing a character string to an address, date, or number?

Are there any mixed-mode comparisons or comparisons between variables of different lengths? If so, ensure that the conversion rules are well understood.

Are the comparison operators correct? Programmers frequently confuse such relations as at most, at least, greater than, not less than, less than or equal.

Does each Boolean expression state what it is supposed to state? Programmers often make mistakes when writing logical expressions involving and, or, and not.

Are the operands of a Boolean operator Boolean? Have comparison and Boolean operators been erroneously mixed together? This represents another frequent class of mistakes. Examples of a few typical mistakes are illustrated here. If you want to determine whether

`i`is between 2 and 10, the expression`2<i<10`is incorrect; instead, it should be`(2<i) && (i<10)`. If you want to determine whether`i`is greater than`x`or`y, i>x||y`, is incorrect; instead, it should be`(i>x)||(i>y)`. If you want to compare three numbers for equality,`if(a==b==c)`does something quite different. If you want to test the mathematical relation`x>y>z`, the correct expression is`(x>y)&&(y>z)`.Are there any comparisons between fractional or floating-point numbers that are represented in base-2 by the underlying machine? This is an occasional source of errors because of truncation and base-2 approximations ...

Get *The Art of Software Testing, Second Edition* now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.