Arithmetic operators

Basic Binary Arithmetic Operators

These operators perform calculations between two numeric values, with a total of seven variants, covering standard four arithmetic operations, integer division, modulo, and unary negation operators.

OperatorDescription
+Addition
-Subtraction
-expressionUnary minus (negates the numeric value, flips positive/negative sign)
*Multiplication
/Regular division, always returns a double floating-point value
~/Integer division, discards decimal fractions and returns an int integer value
%Modulo (retrieves the remainder from integer division)

Division behaves uniquely here compared to other programming languages: the / operator always outputs a floating-point number. Use ~/ if you require an integer result. Readers with prior coding experience should pay special attention to this distinction.

Example

void main() {
        // Verify basic four arithmetic, integer division and modulo results
	print(2 + 3 == 5);
	print(2 - 3 == -1);
	print(2 * 3 == 6);
	print(5 / 2 == 2.5);  // Regular division outputs decimal double value
	print(5 ~/ 2 == 2);   // Integer division retains only integer part as int
	print(5 % 2 == 1);    // Get remainder of division; % modulo operator only fetches remainder from integer division

	// String concatenation example combining integer division and remainder
	print('5/2 = ${5 ~/ 2} r ${5 % 2}' == '5/2 = 2 r 1');
}Code language: Dart (dart)

Output Result

Increment / Decrement Operators

Dart supports two forms of increment ++ and decrement -- operators: prefix and postfix. The core difference is whether the variable is modified before or after its value is retrieved.

OperatorExecution LogicFinal Return Value of Expression
++varIncrement variable by 1 first, then participate in calculationsNew value after increment
var++Use the original variable value for calculations first, then increment variable by 1 after calculation completesOriginal value before increment
--varDecrement variable by 1 first, then participate in calculationsNew value after decrement
var--Use the original variable value for calculations first, then decrement variable by 1 after calculation completesOriginal value before decrement
void main() {
	int a;
	int b;

	// 1. Prefix increment ++a: increment first, then assign
	a = 0;
	b = ++a;
	print(a == b); // a=1, b=1, 1 == 1

	// 2. Postfix increment a++: assign first, then increment
	a = 0;
	b = a++;
	print(a != b); // a=1, b=0, 1 != 0

	// 3. Prefix decrement --a: decrement first, then assign
	a = 0;
	b = --a;
	print(a == b); // a=-1, b=-1, -1 == -1

	// 4. Postfix decrement a--: assign first, then decrement
	a = 0;
	b = a--;
	print(a != b); // a=-1, b=0, -1 != 0
}

All examples above print true

Important note: Prefix and postfix placement of ++ or -- produces different behaviors in certain scenarios.

When used standalone, such as:

int a = 1;
++a;

int b  = 1;
b++;

print(a);
print(b);

Both statements will output 2 in the end

D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
2
2

The same logic applies to --; the target variable will ultimately increase or decrease by 1 either way.

However, results differ when ++ or -- are embedded inside mathematical expressions, for example:

void main() {
  int a = 5;
  // Postfix: Use value 5 for calculation first, then a becomes 6 afterward
  int res1 = a++ * 2;
  print(res1); // 10
  print(a);  // 6

  int b= 5;
  // Prefix: Change b to 6 first, then calculate with value 6
  int res2 = ++b * 2;
  print(res2); // 12
  print(b); // 6
}

Notice that both variables a and b end up incremented by 1, yet res1 and res2 hold different values. This subtle difference solely comes from whether the plus operator is placed before or after the variable name.

For postfix notation a++: the original value of a is used for subsequent operations first. Here, a * 2 = 10 runs while a still equals 5, which gets assigned to res1 to produce a result of 10. Only after this assignment does a increment by 1 to become 6.

For prefix notation, a increments by 1 to become 6 first, then the expression 6 * 2 = 12 executes, assigning the value 12 to res2.

The line int res1 = a++ * 2; breaks down into these two steps:

int res1 = a * 2;
a = a +1;

The line int res2 = ++b * 2; breaks down into these two steps:

b = b+1;
int res2 = b * 2;

Easy mnemonic to remember:

Prefix modifies first, postfix uses original value first;

Prefix returns updated value, postfix returns original value.

Summary

Prefix ++a / --a: Modify the variable itself first, then use the updated value for calculations or assignment

Postfix a++ / a--: Use the variable’s original value for calculations or assignment first, then modify the variable once the operation finishes

Readers may test the following examples:

void main() {
  int a = 0;
  int b = ++a; 
  // Step 1: a = a + 1 → a becomes 1
  // Step 2: Assign updated a (1) to b
  print(a); // 1
  print(b); // 1
}
void main() {
  int a = 0;
  int b = a++;
  // Step 1: Assign original value 0 of a to b
  // Step 2: Execute a = a + 1 → a becomes 1
  print(a); // 1
  print(b); // 0
}
void main() {
  int a = 0;
  int b = --a;
  // Step 1: a = a - 1 → a becomes -1
  // Step 2: Assign updated a (-1) to b
  print(a); // -1
  print(b); // -1
}
void main() {
  int a = 0;
  int b = a--;
  // Step 1: Assign original value 0 of a to b
  // Step 2: Execute a = a - 1 → a becomes -1
  print(a); // -1
  print(b); // 0
}

The above are the simplest demonstration cases for prefix and postfix increment/decrement operators. Readers may compile and run them locally to observe their behavioral differences firsthand.

Previous:

Leave a Reply

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