Operators are fundamental to any programming language. They allow developers to perform calculations, make comparisons, and control the flow of logic. In Dart, operators are symbols or keywords that operate on variables and values to produce results. Understanding them is essential for writing clean, efficient, and error-free code.
In this comprehensive guide, we’ll explore:
- Introduction to Dart operators
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Operator precedence and associativity
- Practical examples and use cases
- Best practices for using operators
Introduction to Dart Operators
Dart operators are categorized based on their functionality. Using the correct operator helps developers perform mathematical calculations, compare values, combine logical expressions, and assign values efficiently.
Categories of Operators in Dart
- Arithmetic Operators – Perform mathematical operations.
- Relational Operators – Compare values.
- Logical Operators – Combine Boolean expressions.
- Assignment Operators – Assign values to variables.
- Unary Operators – Operate on a single operand.
- Bitwise and Type Test Operators – Advanced operations (less common for beginners).
This guide focuses on the most commonly used operators in day-to-day Dart programming.
Arithmetic Operators
Arithmetic operators allow performing basic mathematical operations on numbers. Dart supports both int and double types.
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division (produces double) | 5 / 2 = 2.5 |
~/ | Integer Division | 5 ~/ 2 = 2 |
% | Modulus (remainder) | 5 % 2 = 1 |
++ | Increment by 1 | x++ |
-- | Decrement by 1 | x-- |
Examples
int a = 10;
int b = 3;
print(a + b); // 13
print(a - b); // 7
print(a * b); // 30
print(a / b); // 3.3333333333
print(a ~/ b); // 3
print(a % b); // 1
int x = 5;
x++;
print(x); // 6
x--;
print(x); // 5
Relational Operators
Relational operators are used to compare two values. The result is always a Boolean (true or false).
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 → true |
!= | Not equal to | 5 != 3 → true |
> | Greater than | 5 > 3 → true |
< | Less than | 3 < 5 → true |
>= | Greater or equal | 5 >= 5 → true |
<= | Less or equal | 3 <= 5 → true |
Examples
int a = 10;
int b = 20;
print(a == b); // false
print(a != b); // true
print(a > b); // false
print(a < b); // true
print(a >= 10); // true
print(b <= 20); // true
Logical Operators
Logical operators are used to combine Boolean expressions.
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | true && false → false |
</td><td></td><td> | ||
! | Logical NOT | !true → false |
Examples
bool isLoggedIn = true;
bool hasAccess = false;
print(isLoggedIn && hasAccess); // false
print(isLoggedIn || hasAccess); // true
print(!isLoggedIn); // false
Logical operators are often used in conditional statements to control the flow of a program:
if (isLoggedIn && hasAccess) {
print("Access granted");
} else {
print("Access denied");
}
Assignment Operators
Assignment operators are used to assign values to variables. Dart supports shorthand operators for mathematical operations.
| Operator | Description | Example |
|---|---|---|
= | Assign value | x = 5 |
+= | Add and assign | x += 3 → x = x + 3 |
-= | Subtract and assign | x -= 2 → x = x - 2 |
*= | Multiply and assign | x *= 2 → x = x * 2 |
/= | Divide and assign | x /= 2 → x = x / 2 |
%= | Modulus and assign | x %= 3 → x = x % 3 |
Examples
int x = 10;
x += 5; // 15
x -= 3; // 12
x *= 2; // 24
x ~/= 4; // 6
x %= 5; // 1
Operator Precedence and Associativity
Dart evaluates operators based on precedence rules, determining the order of operations.
- Highest Precedence:
(),[],.,++,-- - High:
*,/,~/,% - Medium:
+,- - Low:
<,<=,>,>= - Lower:
==,!= - Logical AND/OR:
&&,|| - Assignment Operators:
=,+=,-=
Example:
int result = 10 + 3 * 2; // 16, multiplication first
Practical Examples
Example 1: Calculator
int a = 20;
int b = 5;
print("Addition: ${a + b}");
print("Subtraction: ${a - b}");
print("Multiplication: ${a * b}");
print("Division: ${a / b}");
Example 2: Login Validation
bool isLoggedIn = true;
bool hasAccess = false;
if (isLoggedIn && hasAccess) {
print("Welcome!");
} else {
print("Access Denied");
}
Example 3: Score Update
int score = 10;
score += 5; // 15
score %= 7; // 1
Best Practices
- Use parentheses for clarity when combining multiple operators.
- Prefer
~/for integer division when needed. - Use logical operators in conditional statements to simplify code.
- Use shorthand assignment operators for concise code.
- Follow Dart style guide for spacing around operators for readability.
Leave a Reply