Why == works in an if statement?

This is a pretty basic question. I believe that I have seen online examples of if statements with just a simple < or > operator, but never with just a simple = operator. Is the == operator always needed within an an expression found in the if statement? Hoping someone with more experience would be willing to share the reason.

For example, both sketches below compile fine and upload to my Arduino UNO with no error messages. However, when I run the first sketch, the value of cycleCounter remains at 0 when the pushbutton is pressed. When I run the second sketch, the value of cycleCounter does get reset to 10 when the pushbutton is pressed, as desired. The sketches below are abbreviated for simplicity.

Example 1. This one does not work. If Switch 7 is pressed and cycleCounter = 0, then cycleCounter never gets reset to 10.

int cycleCounter = 0;

... unrelated code here...
  
  // If the start push button is pressed (ie. Switch7 is LOW) and the cycleCounter = 0, set the cycleCounter to 10. 
  if ((Switch7 == LOW)  && (cycleCounter = 0)) {
   cycleCounter = 10;
 }

Example 2. This one works! If Switch 7 is pressed and cycleCounter =0, then cycleCounter gets reset to 10.

int cycleCounter = 0;

... unrelated code here...  

  // If the start push button is pressed (ie. Switch7 is LOW) and the cycleCounter = 0, set the cycleCounter to 10. 
  if ((Switch7 == LOW)  && (cycleCounter == 0)) {
   cycleCounter = 10;
   }

The only difference between the sketches is an == vs = operator within the if statement.
== works!
= does not work!

Thanks in advance.

Don

They are two different operators. = is the assignment operator. == is a comparison operator.

In the first sketch,

  if ((Switch7 == LOW)  && (cycleCounter = 0)) {

causes an assignment to happen in the if statement. cycleCounter is set to zero.

In C and C++, assignment statements also return a value, that being the value that the variable on the left side of the statement was set to. This is why the statement above is acceptable syntax but obviously not what is wanted. The value of the assignment statement is zero, which in the context of the if statement is taken as a logical false value. Since this is part of an AND (&&) construct, the overall if statement will never be true because the second argument is always false.

Jack - wow, thank you for the very clear and concise explanation about “assignment” and “comparison” operators. It is very much appreciated. I also appreciate you taking the time to point out what my code was actually doing. It all makes sense now.

Anyone know if > or < are valid comparison operators or is >> and << used instead?

Thanks in advance,

Don

Don, you're welcome. The answer to your next question is in the Arduino Reference or any C++ reference (for example, this one). Let us know if you don't find it :slight_smile:

Jack - Thank you for your fast response and the links.

Don

Anyone know if > or < are valid comparison operators or is >> and << used instead?

< and > are the correct comparison operators as are >=, <=, and != (not equal to).

<< and >> are used as bit shifting operators. (1 << 3) is the digit 1 shifted to the right 3 binary positions (ie, 00000001 becomes 00001000), or 2^3 = 8 decimal.

The questions you are asking are answered very clearly if you look up a C++ reference for ‘operators’.

Hi,
I think what you are curious about is the reason why the simple ‘=’ is not used while checking conditions.

The ‘=’ sign is used as the assignment operator and it assigns the value to its right to the variable to its right. similarly you use ‘==’ to compare values. The reason the same operator is NOT used to do both the comparison and assignment is because it would be quite ambiguous.

For example if you write a = 25 the compiler assigns the value of 25 to the variable a but if you use a == 25 the compiler compares the two values and returns a true if the value of a is 25 else it returns a false.

Also, << and >> are used in bit shift operations and hence are not used to make comparisons.

Abhic & marco_c - thanks for all of your helpful replies. Everything is clearly understood now.

Don

dz63:
This is a pretty basic question. I believe that I have seen online examples of if statements with just a simple < or > operator, but never with just a simple = operator. Is the == operator always needed within an an expression found in the if statement? Hoping someone with more experience would be willing to share the reason.

“==” means “does it equal?”

“=” means “set this to that”.

Example:
** X = 3;**
** if (X == 4) {**
** print (“It’s not the same!”); ← this gets printed because 3 is not equal to 4**
** } else {**
** print (“It matches!”);**
** }**

Now watch what happens here:
** X = 3;**
** if (X = 4) { ← this SETS X to a value of 4**
** print (“It’s not the same!”);**
** } else {**
** print (“It matches!”); ← this gets printed because setting X to anything returns boolean “true”**
** }**

Make sense?

this gets printed because setting X to anything returns boolean "true"

sp. "this gets printed because setting X to any non-zero value returns boolean "true"

Krupski:
“==” means “does it equal?”

“=” means “set this to that”.

Example:
** X = 3;**
** if (X == 4) {**
** print (“It’s not the same!”); ← this gets printed because 3 is not equal to 4**
** } else {**
** print (“It matches!”);**
** }**

Now watch what happens here:
** X = 3;**
** if (X = 4) { ← this SETS X to a value of 4**
** print (“It’s not the same!”);**
** } else {**
** print (“It matches!”); ← this gets printed because setting X to anything returns boolean “true”**
** }**

Make sense?

Eeeerm… I think you have the meaning of true and false mixed up.
What you probably meant is this:

    X = 3;
    if (X == 4) {
        print ("It matches!");
    } else {
        print ("It's not the same!"); <-- this gets printed because 3 is not equal to 4
    }
    X = 3;
    if (X = 4) { <-- this SETS X to a value of 4
        print ("It matches!"); <-- this gets printed because setting X to anything non-zero returns boolean "true"
    } else {
        print ("It's not the same!");
    }

Krupski:
print (“It matches!”); ← this gets printed because setting X to anything returns boolean “true”

Make sense?

No.
Setting X to any non-zero value returns a boolean true
Setting X to zero returns a boolean false

Also, it's a good habit to put the constant before the variable it's being compared to.
i.e.

if (25 == a)
{
    // Some action here
}

The reason for doing this,

if (25 = a)
{
}

Will cause a compiler error. Because you're trying to assign a variable to a constant. This coding habit will catch your error where, if you put = but meant == (a very common mistake). If you do a = 25 it will compile, but obviously not what was intended.

This forum rocks! Lot's of genuinely helpful people here. Thank you all for your generous advice! :slight_smile:

DigitalJohnson - thanks for sharing the "good habit" tip too. I need all of those I can get!