==

Description

Compares the variable on the left with the value or variable on the right of the operator. Returns true when the two operands are equal. Please note that you may compare variables of different data types, but that could generate unpredictable results, it is therefore recommended to compare variables of the same data type including the signed/unsigned type.

Syntax

x == y; // is true if x is equal to y and it is false if x is not equal to y

Parameter Values

  • x: variable. Allowed data types: int, float, double, byte, short, long.
  • y: variable or constant. Allowed data types: int, float, double, byte, short, long.

Example Code

void setup() { Serial.begin(9600); int x = 1; int y = 1; if (x == y) // tests if x is equal to y Serial.println("x is equal to y"); else Serial.println("x is NOT equal y"); } void loop() { }

The result on Serial Monitor:

COM6
Send
x is equal to y
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTES AND WARNINGS:

Don't confuse the "equal to" comparison operator == (double equal signs) with the assignment operator = (single equal sign)

  • == evaluates whether two expressions are equal or not.
  • = stores value to a variable

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES