Dart Operators
Symbols that perform operations on variables and values
⚡ What are Dart Operators?
Operators are symbols that perform operations on variables and values. Dart provides arithmetic, comparison, logical, and assignment operators for various calculations and comparisons.
// Basic operators in action
int a = 10, b = 5;
int sum = a + b; // Addition
bool isEqual = a == b; // Comparison
Types of Operators
Arithmetic
Mathematical operations
int sum = 5 + 3; // 8
int diff = 10 - 4; // 6
Comparison
Compare values and return bool
bool isEqual = 5 == 5; // true
bool isGreater = 8 > 3; // true
Logical
Combine boolean expressions
bool result = true && false; // false
bool orResult = true || false; // true
Assignment
Assign values to variables
int x = 10;
x += 5; // x = x + 5 = 15
🔹 Arithmetic Operators
Perform mathematical calculations:
int a = 15, b = 4;
// Basic arithmetic
int addition = a + b; // 19
int subtraction = a - b; // 11
int multiplication = a * b; // 60
double division = a / b; // 3.75
int intDivision = a ~/ b; // 3 (integer division)
int remainder = a % b; // 3 (modulo)
print('$a + $b = $addition');
print('$a / $b = $division');
print('$a % $b = $remainder');
Output:
15 + 4 = 19
15 / 4 = 3.75
15 % 4 = 3
🔹 Comparison Operators
Compare values and return true or false:
int score1 = 85, score2 = 92;
// Comparison operators
bool isEqual = score1 == score2; // false
bool isNotEqual = score1 != score2; // true
bool isLess = score1 < score2; // true
bool isGreater = score1 > score2; // false
bool isLessEqual = score1 <= score2; // true
bool isGreaterEqual = score1 >= score2; // false
print('$score1 == $score2: $isEqual');
print('$score1 < $score2: $isLess');
print('$score1 >= $score2: $isGreaterEqual');
Output:
85 == 92: false
85 < 92: true
85 >= 92: false
🔹 Logical Operators
Combine and manipulate boolean values:
bool hasAccount = true;
bool isVerified = false;
bool isPremium = true;
// Logical AND (&&) - both must be true
bool canAccess = hasAccount && isVerified; // false
// Logical OR (||) - at least one must be true
bool canLogin = hasAccount || isVerified; // true
// Logical NOT (!) - reverses the boolean
bool isNotVerified = !isVerified; // true
print('Can access: $canAccess');
print('Can login: $canLogin');
print('Is not verified: $isNotVerified');
Output:
Can access: false
Can login: true
Is not verified: true
🔹 Assignment Operators
Assign and modify variable values:
int points = 100;
// Basic assignment
points = 150; // points is now 150
// Compound assignment operators
points += 25; // points = points + 25 = 175
points -= 10; // points = points - 10 = 165
points *= 2; // points = points * 2 = 330
points ~/= 3; // points = points ~/ 3 = 110
print('Final points: $points');
// Null-aware assignment
String? name;
name ??= 'Default'; // Assign only if null
print('Name: $name');
Output:
Final points: 110
Name: Default
🔹 Increment and Decrement
Increase or decrease values by 1:
int counter = 5;
// Pre-increment (increment first, then use)
int preInc = ++counter; // counter = 6, preInc = 6
// Post-increment (use first, then increment)
int postInc = counter++; // postInc = 6, counter = 7
// Pre-decrement
int preDec = --counter; // counter = 6, preDec = 6
// Post-decrement
int postDec = counter--; // postDec = 6, counter = 5
print('Counter: $counter');
print('Pre-increment: $preInc');
print('Post-increment: $postInc');
Output:
Counter: 5
Pre-increment: 6
Post-increment: 6