Reversing equality test to avoid errors

Common mistake

if(x=2)

instead of

if(x==2)

Write the rvalue of the expression at the left side of the equality operator (==):

if(2==x)

In this case the compiler will warn you if you typed the assignment operator (=) by mistake.

About

I love coffee!

Posted in Programming Tagged with:
1 comments
Sumant
Sumant

Many people find this (2==x) less clear to read than (x==2) !