Operators in Dart
Operators are special symbols that perform operations on variables and values. Think of them as the tools in your programming toolbox - each one has a specific job to make your code work!
Arithmetic Operators
These operators perform mathematical calculations, just like a calculator:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 10 - 4 = 6 |
* | Multiplication | 6 * 2 = 12 |
/ | Division (returns double) | 15 / 4 = 3.75 |
~/ | Integer division | 15 ~/ 4 = 3 |
% | Modulus (remainder) | 15 % 4 = 3 |
void main() {
int a = 10;
int b = 3;
print('Addition: ${a + b}'); // 13
print('Subtraction: ${a - b}'); // 7
print('Multiplication: ${a * b}'); // 30
print('Division: ${a / b}'); // 3.3333...
print('Integer division: ${a ~/ b}'); // 3
print('Modulus: ${a % b}'); // 1
}
Assignment Operators
These operators assign values to variables:
| Operator | Description | Example |
|---|---|---|
= | Simple assignment | x = 5 |
+= | Add and assign | x += 3 (same as x = x + 3) |
-= | Subtract and assign | x -= 2 |
*= | Multiply and assign | x *= 4 |
/= | Divide and assign | x /= 2 |
void main() {
int score = 100;
score += 50; // score is now 150
score -= 25; // score is now 125
score *= 2; // score is now 250
score ~/= 5; // score is now 50
print('Final score: $score');
}
Comparison Operators
These operators compare values and return true or false:
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 returns true |
!= | Not equal to | 5 != 3 returns true |
> | Greater than | 8 > 5 returns true |
< | Less than | 3 < 7 returns true |
>= | Greater than or equal | 5 >= 5 returns true |
<= | Less than or equal | 4 <= 6 returns true |
void main() {
int age = 18;
print('Is adult? ${age >= 18}'); // true
print('Is teenager? ${age < 20}'); // true
print('Exactly 18? ${age == 18}'); // true
}
Logical Operators
These operators work with boolean values (true and false):
| Operator | Description | Example |
|---|---|---|
&& | AND (both must be true) | true && false returns false |
|| | OR (at least one must be true) | true || false returns true |
! | NOT (reverses the boolean) | !true returns false |
void main() {
bool hasLicense = true;
bool hasInsurance = false;
int age = 20;
bool canDrive = hasLicense && hasInsurance && (age >= 18);
print('Can drive? $canDrive'); // false
bool needsHelp = !hasLicense || age < 18;
print('Needs help? $needsHelp'); // false
}
Increment and Decrement Operators
These are shortcuts for adding or subtracting 1:
| Operator | Description | Example |
|---|---|---|
++ | Increment by 1 | x++ or ++x |
-- | Decrement by 1 | x-- or --x |
void main() {
int counter = 5;
print('Before: $counter'); // 5
counter++; // Same as counter = counter + 1
print('After ++: $counter'); // 6
counter--; // Same as counter = counter - 1
print('After --: $counter'); // 5
}
Type Test Operators
These operators check the type of an object:
| Operator | Description | Example |
|---|---|---|
is | True if object has specified type | x is String |
is! | True if object doesn't have specified type | x is! int |
void main() {
var name = 'Alice';
var age = 25;
print('name is String: ${name is String}'); // true
print('age is String: ${age is String}'); // false
print('age is not String: ${age is! String}'); // true
}
Operator Precedence
Just like in math, operators have an order of operations. Multiplication happens before addition, for example:
void main() {
int result = 2 + 3 * 4; // 3 * 4 happens first, then + 2
print(result); // 14, not 20
int result2 = (2 + 3) * 4; // Parentheses change the order
print(result2); // 20
}
Remember: When in doubt, use parentheses to make your intentions clear! Your future self (and your teammates) will thank you. 😊