Avatar of Praveen Gupta
Praveen Gupta

asked on 

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?
C++

Avatar of undefined
Last Comment
Fabrice Lambert
SOLUTION
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Kevin Stanush
Kevin Stanush
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Fabrice Lambert
Fabrice Lambert
Flag of France image

Solutions provided
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo