You are currently viewing Dart Relational Operators

Dart Relational Operators

Dart, a modern, versatile programming language developed by Google, provides developers with a powerful set of tools to create efficient and robust applications. Among these tools are the relational operators, essential components that enable developers to compare values and make decisions based on those comparisons. In this article, we’ll explore Dart’s relational operators, and provide code examples to solidify your understanding.

What are Relational Operators?

Relational operators are symbols or keywords in a programming language that compare two values and return a boolean result, indicating whether the relationship between the values is true or false. Dart provides a set of relational operators that enable developers to perform various types of comparisons.

Here are the main relational operators in Dart:

Equality Operator (==)

This operator checks if two values are equal.

void main() {

  int a = 5;
  int b = 7;

  bool result = (a == b);
  
  print(result);  // Output: false
  
}

In this example, the equality operator compares a and b, returning false because they are not equal.

Inequality Operator (!=)

This operator checks if two values are not equal.

void main() {

  int a = 5;
  int b = 5;

  bool result = (a != b);
  
  print(result);  // Output: false
  
}

The inequality operator compares a and b, returning false because they are equal.

Greater Than Operator (>)

This operator checks if the value on the left is greater than the value on the right.

void main() {

  int x = 8;
  int y = 5;

  bool result = (x > y);
  
  print(result);  // Output: true
  
}

In this case, the result is true because x is greater than y.

Less Than Operator (<)

This operator checks if the value on the left is less than the value on the right.

void main() {

  int p = 3;
  int q = 7;

  bool result = (p < q);
  
  print(result);  // Output: true
  
}

The result is true because p is less than q.

Greater Than or Equal To Operator (>=)

This operator checks if the value on the left is greater than or equal to the value on the right.

void main() {

  int m = 10;
  int n = 10;

  bool result = (m >= n);
  
  print(result);  // Output: true
  
}

Here, the result is true because m is equal to n.

Less Than or Equal To Operator (<=)

This operator checks if the value on the left is less than or equal to the value on the right.

void main() {

  int r = 4;
  int s = 7;

  bool result = (r <= s);
  
  print(result);  // Output: true
  
}

The result is true because r is less than s.

Understanding Equality and Identity

In Dart, it is essential to distinguish between equality and identity. The equality operators (== and !=) check whether the values of two objects are the same or, for complex objects, whether they point to the same object reference. The identical() function determines if two references point to the same object in memory, mirroring the behavior of equality operators (== and !=) when dealing with complex object types. This distinction is fundamental for accurate comparisons and effective use of these operators in Dart programming.

Let’s explore this with examples:

Equality Operator (==)

The equality operator compares the values of two objects:

void main() {

  String str1 = 'Dart';
  String str2 = 'Dart';

  bool result = (str1 == str2);
  
  print(result);  // Output: true
  
}

In this case, the equality operator returns true because the values of str1 and str2 are the same. It’s noteworthy that the equality operators (== and !=) are the only comparison operators defined on Strings in Dart, and they function as expected by determining whether two strings have the same content. Other relational operators besides equality operators (== and !=) aren’t defined on Strings in Dart.

Identity Function (identical())

The identity function identical() checks if two references point to the same object:

void main() {

    List<int> list1 = [1, 2, 3];
    List<int> list2 = list1;

    List<int> list3 = [1, 2, 3];

    bool result1 = identical(list1, list2);
    print(result1);  // Output: true

    bool result2 = identical(list1, list3);
    print(result2);  // Output: false
    
}

Here, list1 and list2 reference the same list object, so identical() returns true. On the other hand, list1 and list3 reference different list objects, so identical() returns false.

Understanding the distinction between equality and identity is essential for effective programming in Dart.

Data Types and Relational Operators

Dart’s relational operators are designed to work with a variety of data types, including integers, doubles, and strings. Let’s explore how these operators behave with different data types.

Integers

Relational operators work seamlessly with integer values, allowing you to compare and make decisions based on numeric conditions.

void main() {

  int num1 = 10;
  int num2 = 20;

  print(num1 == num2);  // Output: false
  print(num1 != num2);  // Output: true
  print(num1 < num2);   // Output: true
  print(num1 > num2);   // Output: false
  print(num1 <= num2);  // Output: true
  print(num1 >= num2);  // Output: false
  
}

In this example, the output reflects the comparisons between two integer values.

Doubles

Relational operators are equally applicable to double values, facilitating the comparison of floating-point numbers.

void main() {

  double price1 = 5.99;
  double price2 = 9.99;

  print(price1 == price2);  // Output: false
  print(price1 != price2);  // Output: true
  print(price1 < price2);   // Output: true
  print(price1 > price2);   // Output: false
  print(price1 <= price2);  // Output: true
  print(price1 >= price2);  // Output: false
  
}

Here, the code demonstrates how to use relational operators with double values.

Strings

Relational operators can be used with strings to compare their equality, checking whether two strings have the same content.

void main() {

  String text1 = "apple";
  String text2 = "banana";

  print(text1 == text2);  // Output: false
  print(text1 != text2);  // Output: true
  
}

In this example, the relational operators are used to compare the equality of two strings. It’s important to note that, for other objects, strings included, only the equality (==) and inequality (!=) operators are defined; other relational operators are not defined. Notably, for strings, these operators behave as expected, checking whether two strings have the same content. However, for other objects, the equality operators check for identity, determining whether two objects point to the same object reference in memory.

Other Objects

In Dart, the == or != operators for other objects typically checks for object identity, not the equality of values. This means that two different objects with the same content won’t be considered equal using the == operator.

void main() {

    List<int> list1 = [1, 2, 3];
    List<int> list2 = [1, 2, 3];

    bool result = list1 == list2;
    print(result);  // Output: false
    
}

In this example, list1 and list2 are distinct objects in memory, even though they have the same content. Consequently, the == operator returns false because it compares the object references, not the content.

If you want to compare the content of two lists, you’ll need to use other means, such as comparing each element or using utility functions that facilitate content-based equality checks. For instance, you could convert the lists to strings and then compare them:

void main() {

    List<int> list1 = [1, 2, 3];
    List<int> list2 = [1, 2, 3];
	
    bool result = list1.toString() == list2.toString();
    print(result);  // Output: true
	
}

This approach converts both lists to strings and then compares the string representations. While this works for simple cases, keep in mind that it might not be the most efficient or reliable method, especially if the lists contain nested structures or complex objects.

Chaining Relational Operators

Dart allows you to chain multiple relational operators together for more complex comparisons. This is particularly useful when you need to check multiple conditions simultaneously.

void main() {

  int x = 10;
  int y = 15;
  int z = 20;

  // Combining operators
  bool complexCondition = (x < y) && (y < z);
  
  print('($x < $y) && ($y < $z) : $complexCondition');  // Output: (10 < 15) && (15 < 20) : true
  
}

In this example, the && (logical AND) operator combines the conditions, checking if x is less than y AND y is less than z. The result is true because both conditions are satisfied.

Conclusion

Understanding Dart’s relational operators is fundamental to writing effective and efficient code. These operators empower developers to create conditions, make decisions, and perform various types of comparisons in their programs.

In this article, we explored the different relational operators in Dart, including equality, inequality, greater than, less than, greater than or equal to, and less than or equal to. We also explored the distinction between equality and identity, emphasizing the importance of recognizing when to use each.

References:

Leave a Reply