Search⌘ K
AI Features

Arithmetic Operators

Learn how to use Dart's arithmetic operators to perform basic math operations and understand the differences between prefix and postfix increments and decrements. Discover how the truncating division and unary minus operators work to manage numbers and expressions effectively.

Arithmetic operators perform mathematical operations such as addition and subtraction. Below is a list of the arithmetic operators supported by Dart.

Operator

Use

+

Adds two operands

-

Subtracts the second operand from the first

-expr

Reverses the sign of the expression (unary minus)

*

Multiplies both operands

/

Divides the first operand by the second operand

~/

Divides the first operand by the second operand and returns an integer by truncating toward zero

%

Gets the remainder after division of one number by another

Most of the operators behave identically to standard mathematics. The truncating division operator (~/) divides both operands and removes the fractional part from the decimal value.

We can observe how the basic operators process numeric values in the following file.

Dart
void main() {
var operand1 = 10;
var operand2 = 7;
print(operand1 + operand2);
print(operand1 - operand2);
print(operand1 * operand2);
print(operand1 / operand2);
print(operand1 ~/ operand2);
print(-7 ~/ 2);
print(operand1 % operand2);
}
  • Lines 5—8: We perform standard mathematical operations including addition, subtraction, multiplication, and standard division.

  • Line 10: We use the truncating division operator (~/) to divide 10 by 7. This drops the fractional part and evaluates to 1.

  • Line 11: We demonstrate ...