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:

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction10 - 4 = 6
*Multiplication6 * 2 = 12
/Division (returns double)15 / 4 = 3.75
~/Integer division15 ~/ 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:

OperatorDescriptionExample
=Simple assignmentx = 5
+=Add and assignx += 3 (same as x = x + 3)
-=Subtract and assignx -= 2
*=Multiply and assignx *= 4
/=Divide and assignx /= 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:

OperatorDescriptionExample
==Equal to5 == 5 returns true
!=Not equal to5 != 3 returns true
>Greater than8 > 5 returns true
<Less than3 < 7 returns true
>=Greater than or equal5 >= 5 returns true
<=Less than or equal4 <= 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):

OperatorDescriptionExample
&&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:

OperatorDescriptionExample
++Increment by 1x++ or ++x
--Decrement by 1x-- 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:

OperatorDescriptionExample
isTrue if object has specified typex is String
is!True if object doesn't have specified typex 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. 😊