Tag Archives: instructions

On double and or

A small remark on programming conditionals. This is true for C and C++, but it might be true also for Java and other languages. I haven’t checked all of them.

It is about conditionals.

I’m seeing lots and lots of double OR and double AND in conditionals. In fact, I find it rare to see a single single operator these days. (Sorry, I couldn’t resist the pun). Almost without exception, every multiple conditional I see has the form:

if ((x>0) && (x<MAX_VAL))
{
   do something....
}

Which is kind of silly, really. The double operator && is the right thing to use when you have a complex conditional with function calls. But when all you do is compare arithmetical conditions? What one should do is use the simple single-operator

if ((x>0) & (x<MAX_VAL))
{
   do something....
}

Continue reading

Advertisements