Skip to main content
The %= operator is a compound assignment operator that calculates the Euclidean modulo of the left operand by the right operand and assigns the result back to the left operand.

Syntax

variable %= expression;
This operation is equivalent to variable = variable % expression, with the distinction that variable is evaluated only once.

Operational Semantics

The execution of the %= operator follows this specific order:
  1. LHS Evaluation: The left-hand side (LHS) expression is evaluated to determine the storage location and current value.
  2. RHS Evaluation: The right-hand side (RHS) expression is evaluated.
  3. Operation: The Euclidean modulo operator (%) is invoked on the LHS value with the RHS value as the argument.
  4. Assignment: The result is stored in the LHS variable.

Mathematical Definition

The operator implements the Euclidean modulo algorithm. For any two finite numbers a and n (where n is non-zero), the result r of a %= n satisfies: 0r<n0 \le r < |n| Unlike the remainder operator found in C-like languages (which typically performs truncation towards zero), the result of the Dart %= operator is always non-negative, regardless of the sign of the operands.

Examples

Integer Assignment
int a = 10;
a %= 3; 
print(a); // Output: 1
Floating-Point Assignment
double b = 5.5;
b %= 2.0;
print(b); // Output: 1.5
Negative Operand Behavior Because the operator calculates the Euclidean modulo, the result is strictly non-negative even when operands are negative.
int c = -10;
c %= 3;
print(c); // Output: 2

int d = 10;
d %= -3;
print(d); // Output: 1
Master Dart with Deep Grasping Methodology!Learn More