Places where assignment operator(=) is used logically incorrectly instead of comparison operator(==) in C++. Logical error e.g. if (a=10) it should be if (a==10). Can you help me list all such cases?

I'm doing a C++ compiler project where I need to flag warnings at places where assignment operator (`=`) can be used wrongly instead of the comparison operator (`==`)...e.g
while doing comparison in `if` statement , to check variable `a` as `10` sometimes we wrongly type if(`a`= `10`), which will result in `if` statement always `true` whereas I wanted to be `true` only if `a` is `10`. Some of the cases I can think of are :
1. `if(var = a)`, logically it should be `if( var==a )`
2. `while(var = a )`
3. `for(;var=a;)`
4. `do{}while(var=a)`
5. `var=a? "some XYZ": "some ABC"`

Can you please help me with the more cases where this logical error can occur, where the user was supposed to use `==` and by mistake `=` was used?
Praveen GuptaAsked:
Who is Participating?
 
Kevin StanushApplication DeveloperCommented:
For instances where you have a variable compared to a constant, change it to:

if (constant == variable)

ie.

if (0 == var)

if you have a typo in this, ie only having one = symbol, it won't compile.  You just have to get into the habit of coding like this.
1
 
Fabrice LambertFabrice LambertCommented:
Hi,

On a return statement:
bool isTen(int num)
{
    return num = 10;    // should be: num == 10;
}

Open in new window

1
 
Fabrice LambertFabrice LambertCommented:
Solutions provided
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.