Operators in Dart Arithmetic, Relational

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

  1. Arithmetic Operators – Perform mathematical operations.
  2. Relational Operators – Compare values.
  3. Logical Operators – Combine Boolean expressions.
  4. Assignment Operators – Assign values to variables.
  5. Unary Operators – Operate on a single operand.
  6. 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.

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division (produces double)5 / 2 = 2.5
~/Integer Division5 ~/ 2 = 2
%Modulus (remainder)5 % 2 = 1
++Increment by 1x++
--Decrement by 1x--

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).

OperatorDescriptionExample
==Equal to5 == 5 → true
!=Not equal to5 != 3 → true
>Greater than5 > 3 → true
<Less than3 < 5 → true
>=Greater or equal5 >= 5 → true
<=Less or equal3 <= 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.

OperatorDescriptionExample
&&Logical ANDtrue && 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.

OperatorDescriptionExample
=Assign valuex = 5
+=Add and assignx += 3 → x = x + 3
-=Subtract and assignx -= 2 → x = x - 2
*=Multiply and assignx *= 2 → x = x * 2
/=Divide and assignx /= 2 → x = x / 2
%=Modulus and assignx %= 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

  1. Use parentheses for clarity when combining multiple operators.
  2. Prefer ~/ for integer division when needed.
  3. Use logical operators in conditional statements to simplify code.
  4. Use shorthand assignment operators for concise code.
  5. Follow Dart style guide for spacing around operators for readability.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *