Operators

Math. Comparisons. Logic.

Every program does all three. Dart uses one consistent set of symbols for all of them: operators.

Arithmetic operators

Arithmetic operators work on numbers:

OperatorWhat it doesExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 22.5
~/Integer division5 ~/ 22
%Remainder7 % 31
void main() {
  var sum = 5 + 3;       // 8
  var product = 5 * 3;   // 15
  var quotient = 5 / 2;  // 2.5
  var intDiv = 5 ~/ 2;   // 2
  var remainder = 7 % 3; // 1
  print(sum); // 8
}

Most of these are familiar. But / is not like the others.

5 / 2 gives 2.5, not 2. Division in Dart always produces a double, even when both numbers are whole.

What if you need the whole number part? Use ~/:

void main() {
  var intDiv = 10 ~/ 3; // 3, the decimal is dropped
  print(intDiv); // 3
}

And % gives the remainder after division. 7 % 3 is 1. Seven divided by 3 goes in twice, with 1 left over.

Comparison operators

Comparison operators compare two values. They always return a bool:

OperatorMeaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
void main() {
  var age = 20;
  var isAdult = age >= 18; // true
  var isTeen = age < 20;   // false
  print(isAdult); // true
}

The result is always true or false. Nothing else. You will use these constantly once you reach Lesson 6 on control flow.

Logical operators

Logical operators combine booleans:

OperatorMeaning
&&And (both must be true)
`
!Not (flips true to false, and vice versa)
void main() {
  var isAdult = true;
  var hasLicense = true;
  var canDrive = isAdult && hasLicense; // true, both are true
  var cannotDrive = !canDrive;          // false, flipped
  print(canDrive); // true
}

&& requires both sides to be true. || only needs one side to be true. ! flips whatever follows it.

Compound assignment

These are shortcuts for updating a variable in place:

void main() {
  var x = 10;
  x += 5;  // x is now 15, same as x = x + 5
  x -= 3;  // x is now 12, same as x = x - 3
  x *= 2;  // x is now 24, same as x = x * 2
  x++;     // x is now 25, same as x = x + 1
  x--;     // x is now 24, same as x = x - 1
  print(x); // 24
}

x++ and x-- are the shortest form. They add or subtract exactly one.

Summary

Operator familySymbolsResult
Arithmetic+ - * / ~/ %A number
Comparison== != < > <= >=bool
Logical&& `
Compound assignment+= -= *= ++ --Updates a variable in place
Previous

Strings and Interpolation

Learn how to build and format strings in Dart using interpolation, multi-line strings, and raw strings.

Start Previous Day
Next Up

Control Flow

Learn how to make decisions in Dart using if, else, ternary expressions, and switch statements.

Start Next Day