Operators

Dart supports all operators listed in the table below, sorted from highest to lowest precedence

CategoryOperatorsAssociativity
Postfix Unary Operatorsexpr++ expr-- () [] ?[] . ?. !N/A
Prefix Unary Operators- expr ! expr ~ expr ++ expr -- expr await exprN/A
Multiplicative Operators* / % ~/Left-associative
Additive Operators+ -Left-associative
Shift Operators<< >> >>>Left-associative
Bitwise AND&Left-associative
Bitwise XOR^Left-associative
Bitwise OR|Left-associative
Relational & Type Test Operators>= > <= < as is is!N/A
Equality Operators== !=N/A
Logical AND&&Left-associative
Logical OR||Left-associative
Null-Coalescing Operator??Left-associative
Ternary Conditional Operatorexpr1 ? expr2 : expr3Right-associative
Cascade Operators.. ?..Left-associative
Assignment Operators= *= /= += -= &= ^= and othersRight-associative
Spread Operators (see notes)... ...?N/A

Combine these operators with operands to build valid expressions

Simple examples:

a++
a + b
a = b
a == b
c ? a : b
a is T

Below are complete working samples:

void main() {
  // 1. Postfix increment: a++
  int a = 10;
  print("1. Postfix increment a++");
  print("Value before increment: a = $a");
  int temp = a++; // Retrieve value first, then increment
  print("temp (original value) = $temp");
  print("Value after increment: a = $a\n");

  // 2. Addition expression: a + b
  int b = 20;
  int sum = a + b;
  print("2. Addition a + b");
  print("$a + $b = $sum\n");

  // 3. Assignment expression: a = b
  a = b;
  print("3. Assignment a = b");
  print("Value after assignment: a = $a\n");

  // 4. Equality check: a == b
  bool isEqual = a == b;
  print("4. Equality comparison a == b");
  print("Result of a == b: $isEqual\n");

  // 5. Ternary conditional expression: c ? a : b
  int c = 5;
  String result = c > 0 ? "c is positive" : "c is zero or negative";
  print("5. Ternary operator c ? a : b");
  print("Check c>0: $result\n");

  // 6. Type test expression: a is T
  dynamic testVal = 100;
  print("6. Type check a is T");
  print("testVal is int: ${testVal is int}");
  print("testVal is String: ${testVal is String}");
}
Code language: Dart (dart)

Feel free to copy this code and run it locally with: dart run

Core Rules for Operator Precedence

1. How Precedence Order Works

Operator precedence decreases as you move down the table: operators listed in earlier rows run before any operators in rows below them.

Note: The “>” symbol here means “takes precedence over”, not a greater-than comparison operator.

Execution priority order: Modulo % > Equality check == > Logical AND &&

Breakdown of execution order for n % i == 0 && d % i == 0:

  1. Evaluate n % i and d % i first (modulo % has highest precedence)
  2. Run the two equality checks using ==
  3. Finally evaluate the middle logical AND &&
2. How Parentheses Affect Evaluation

Wrapping code in parentheses ((n % i == 0) && (d % i == 0)) only improves readability and does not alter the natural evaluation order;

The version without parentheses n % i == 0 && d % i == 0 relies on native precedence rules to execute automatically. Both snippets behave identically and produce the exact same output;

Extra note: Parentheses only change execution flow when you need to override the default precedence order.

void main() {
  int n = 10;
  int d = 20;
  int i = 2;

  // Version 1: Parentheses added for clearer readability
  if ((n % i == 0) && (d % i == 0)) {
    print("Version 1: Both values divide evenly");
  }

  // Version 2: No parentheses, native precedence handles ordering automatically
  if (n % i == 0 && d % i == 0) {
    print("Version 2: Both values divide evenly");
  }
}Code language: PHP (php)

Program output:

Version 1: Both values divide evenly
Version 2: Both values divide evenly

Leave a Reply

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