Dart Relational Operators
Relational operators in Dart are used to compare two values or expressions. These operators return a boolean value (true
or false
) based on the outcome of the comparison. Relational operators are essential for making decisions in your code, such as in conditional statements and loops.
Overview of Relational Operators
Here’s a list of relational operators available in Dart:
- Equal to (
==
) - Not equal to (
!=
) - Greater than (
>
) - Less than (
<
) - Greater than or equal to (
>=
) - Less than or equal to (
<=
)
1. Equal to (==
)
- Description: Checks if two values are equal.
- Usage: Returns
true
if the values are equal, otherwisefalse
.
Example:
2. Not equal to (!=
)
- Description: Checks if two values are not equal.
- Usage: Returns
true
if the values are not equal, otherwisefalse
.
Example:
3. Greater than (>
)
- Description: Checks if the left operand is greater than the right operand.
- Usage: Returns
true
if the left operand is greater, otherwisefalse
.
Example:
4. Less than (<
)
- Description: Checks if the left operand is less than the right operand.
- Usage: Returns
true
if the left operand is less, otherwisefalse
.
Example:
5. Greater than or equal to (>=
)
- Description: Checks if the left operand is greater than or equal to the right operand.
- Usage: Returns
true
if the left operand is greater or equal, otherwisefalse
.
Example:
6. Less than or equal to (<=
)
- Description: Checks if the left operand is less than or equal to the right operand.
- Usage: Returns
true
if the left operand is less or equal, otherwisefalse
.
Example:
Summary of Relational Operators
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 (results in true ) |
!= | Not equal to | 5 != 3 (results in true ) |
> | Greater than | 5 > 3 (results in true ) |
< | Less than | 5 < 8 (results in true ) |
>= | Greater than or equal to | 5 >= 5 (results in true ) |
<= | Less than or equal to | 3 <= 5 (results in true ) |
Conclusion
Relational operators in Dart are crucial for comparing values and controlling the flow of your program. They help in decision-making processes, such as within if
statements, loops, and while filtering data. Understanding how to use relational operators effectively allows you to write more dynamic and responsive Dart applications.