Skip to main content

Overview

Assignment operators are used to assign values to variables. Dart provides compound assignment operators that combine arithmetic operations with assignment, making your code more concise.

Assignment Operators Table

Basic Example

Here’s a complete example showing how assignment operators work:
Output:
The comment in the original code has calculation errors, but the actual execution follows the correct logic: 17 + 5 = 22, 22 - 3 = 19, 19 * 2 = 38, 38 ~/ 4 = 9.

Why Use Compound Operators?

Compound assignment operators make your code:
  • More concise: Less typing required
  • More readable: Clear intent to modify a variable
  • Less error-prone: You don’t repeat the variable name

Comparison

For incrementing or decrementing by 1, Dart also provides ++ and -- operators:
  • counter++ is equivalent to counter += 1
  • counter-- is equivalent to counter -= 1

Practical Examples

Accumulating Values

Updating Calculations

Integer Division Assignment