Compiler Error: lvalue required as left operand of assignment

I am getting this error: lvalue required as left operand of assignment...

if(ammoCount == 240 || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){

Errors in IDE:

DC_17_v4.cpp: In function 'void loop()':
DC_17_v4:367: error: lvalue required as left operand of assignment
DC_17_v4:423: error: lvalue required as left operand of assignment

If I just have this: (for example).... it works as usual. (compiles, and project works as intended)

if(ammoCount == 240){

if I try this:

if((ammoCount == 240) || (ammoCount == 180) || (ammoCount == 120) || (ammoCount = 60)){

it 'compiles' & upload... but it doesnt 'work'.. project doesnt work in same/correct fashion as it should..

full statement if needed:

//at top
int ammoCount = 300;

//counts does with button press...calling this conditional check each time

//check for magazine empty/reload time
      if(ammoCount == 240 || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){
        if(isReloaded == false){          
          Serial.print(F("MAGAZINE EMPTY: "));
          //digitalWrite(currentFireColor, HIGH);//HIGH IS OFF FOR COMMON ANNODE RGB LEDS
          playcomplete("empty.WAV");
          reloadTime = true;
          
        }else if(isReloaded == true){
          ammoCount--;
          playfile("semi.WAV");
          updateDisplay();
         }          
      }

how do I fix this? is this a syntax error? or some sort of data type mis-match?

thanks!

if(ammoCount == 240 || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){

ammoCount = 60
OR
ammoCount == 60

ha.. I was just trying to delete the thread.. (but says I cant)..

as I had caught the typo after I posted! (assignment vs. equals) DOH!..

thanks!

as I had caught the typo after I posted! (assignment vs. equals) DOH!..

Sure, but do you understand why that typo caused the problem? I think that understanding that would help a lot.

PaulS:

as I had caught the typo after I posted! (assignment vs. equals) DOH!..

Sure, but do you understand why that typo caused the problem? I think that understanding that would help a lot.

"I" think I do... :slight_smile:

and I'll give it a shot at explaining..

in general...

using = means you are ASSIGNING a value

using == means you are conditionally checking for equality..

in my specific case.. I did this:
if(ammoCount == 240 || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){

so when I was checking for equality against all those vars/value pairs... when I got to the last one.. I assigned ammoCount to be '60' instead of checking to see if it was equal to 60.
hence throwing an error..and throwing the whole sketch off..

?

"I" think I do...

No, you don't. There is nothing wrong with assigning ammoCount a value in the if statement, but that is not what is happening.

Lets look at the statement when ammoCount is 37.
if(ammoCount == 240 || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){
becomes
if(false || ammoCount == 180 || ammoCount == 120 || ammoCount = 60){
which becomes
if(false || false || ammoCount == 120 || ammoCount = 60){
which becomes
if(false || false || false || ammoCount = 60){
Now, the problem arises. false or false or false or ammoCount is true, so the statement becomes
if(true = 60){

Now, it should be obvious that you can't assign 60 to true.

Operator precedence is the source of the "problem" here.

Had you used parentheses around each "variable operator value" construct, the compiler would not have complained, but the results would not have been what you expected.

ahh..

I see now how it works/is working..

I (again) just assumed it would keep conditional check separate, .. and as you say using the parentheses would have forced that order/separation.. but I wouldnt have gotten working code as I expected.

thank you for sharing/explaining :slight_smile: