Skip to content

Comparison operators

A comparison operator is an operator which usually compares two values.

It then gives a boolean result, which we can use to make decisions in our code (e.g. in if statements or loops).

The key thing is that they evaluate to a boolean value (true or false).

OperatorNameDescriptionExampleResult
==Equal toReturns true if both values are equal5 == 5true
!=Not equal toReturns true if both values are not equal5 != 3true
>Greater thanReturns true if the left value is greater than the right value7 > 4true
<Less thanReturns true if the left value is less than the right value2 < 5true
>=Greater than or equal toReturns true if the left value is greater than or equal to the right value6 >= 6true
<=Less than or equal toReturns true if the left value is less than or equal to the right value3 <= 4true

The equal to operator (==) checks if two values are the same.

  • If they are the same, it returns true.
  • If they are different, it returns false.
int a = 5;
int b = 3;
bool result = (a == b); // true

Because the value of a (5) is not equal to the value of b (3), the comparison will evaluate to false.

The not equal to operator (!=) checks if two values are different.

  • If they are the same, it returns false.
  • If they are different, it returns true.
int a = 5;
int b = 3;
bool result = (a != b); // true

Because the value of a (5) is different from the value of b (3), the comparison will evaluate to true.

The greater than operator (>) checks if the value on the left is larger than the value on the right.

  • If the left value is smaller or equal, it returns false.
  • If the left value is larger, it returns true.
int a = 7;
int b = 4;
bool result = (a > b); // true

Because the value of a (7) is larger than the value of b (4), the comparison will evaluate to true.

The less than operator (<) checks if the value on the left is smaller than the value on the right.

  • If the left value is smaller, it returns true.
  • If the left value is larger or equal, it returns false.
int a = 2;
int b = 5;
bool result = (a < b); // true

Because the value of a (2) is smaller than the value of b (5), the comparison will evaluate to true.

This operator (>=) checks if the value on the left is larger than or equal to the value on the right.

  • If the left value is larger or equal, it returns true.
  • If the left value is smaller, it returns false.
int a = 6;
int b = 6;
bool result = (a >= b); // true

Because the value of a (6) is equal to the value of b (6), the comparison will evaluate to true.

int a = 8;
int b = 5;
bool result = (a >= b); // true

Because the value of a (8) is larger than the value of b (5), the comparison will evaluate to true.

This operator (<=) checks if the value on the left is smaller than or equal to the value on the right.

  • If the left value is smaller or equal, it returns true.
  • If the left value is larger, it returns false.
int a = 4;
int b = 4;
bool result = (a <= b); // true

Because the value of a (4) is equal to the value of b (4), the comparison will evaluate to true.

int a = 7;
int b = 5;
bool result = (a <= b); // false

Because the value of a (7) is larger than the value of b (5), the comparison will evaluate to false.