Flutter: Relational operators

We can use relational operators when we want to compare two variables. In the following table, we can see the relational operators in Dart:

Name.OperatorDescription
Equal toa == bCheck if a is equal to b
Not equal toa != bCheck if a is not equal to b
Greater thana > bCheck if a is greater than b
Less thana < bCheck if a is less than b.
Less or equal toa <= bCheck if a is less or equal to b
Greater or equal toa >= bCheck if a is greater or equal to b

With equal to (==) and not equal to (!=), we can check if two variables are equal or not equal, for example:

int a = 10;
int b = 15;

if (a == b) {
  print('a is equal to b');
}

if (a != b) {
  print('a is not equal to b');
}

Sometimes we also want to know if the variable is greater than or less than. To do it we can use the rest of the operators, for example:

int a = 10;
int b = 15;

if (a > b) {
  print('a is greater than b');
}

if (a < b) {
  print('a is less than b');
}

if (a >= b) {
  print('a is greater or equal to b');
}

if (a <= b) {
  print('a is less or equal to b');
}

Let's take a look at the previous examples in DartPad:

Share this article