Questions or a demo for your school?Student? Sit a test
BrainBit Infotech TeachingStart free

Dart notes · Topic 3 of 19 · 6 min read

Dart operators and numbers

Dart operators notes with examples: arithmetic, ~/ and %, comparison, logical and assignment operators, ++ and --, precedence, ternary and dart:math.

What is an operator?

An operator is a symbol that does something with values, called operands. In 7 + 3, the + is the operator and 7 and 3 are the operands. Dart has the operators you know from maths, plus a few of its own.

Arithmetic operators

OperatorMeaningExampleResult
+add7 + 310
-subtract7 - 34
*multiply7 * 321
/divide (always a double)7 / 23.5
~/divide, keep the whole part7 ~/ 23
%remainder7 % 21

The big difference from Java and C: in Dart, / always gives a double, even for two ints. When you want a whole-number answer, use ~/.

void main() {
  int eggs = 47;
  print(eggs / 12);     // 3.9166666666666665
  print(eggs ~/ 12);    // full trays
  print(eggs % 12);     // eggs left over
}
3.9166666666666665
3
11

~/ and % together split a quantity: 47 eggs make 3 full trays of 12 with 11 left over. The same idea turns seconds into minutes and seconds, or paise into rupees and paise.

Comparison operators

Comparisons give a bool: true or false.

OperatorMeaning
==equal to
!=not equal to
> and <greater than, less than
>= and <=greater or equal, less or equal
void main() {
  int marks = 72;
  print(marks >= 33);
  print(marks == 100);
  print(marks != 0);
}
true
false
true

Remember: = assigns a value; == compares two values.

Logical operators

Logical operators join conditions:

  • && (and): true only when both sides are true.
  • || (or): true when at least one side is true.
  • ! (not): flips true and false.
void main() {
  int age = 19;
  bool hasVoterId = true;
  print(age >= 18 && hasVoterId);   // can vote
  print(age < 5 || age > 60);       // gets a fare discount
  print(!hasVoterId);
}
true
false
false

Dart short-circuits: in a && b, if a is false Dart never looks at b. In a || b, if a is true it skips b.

Assignment shortcuts, ++ and --

void main() {
  int stock = 50;
  stock += 20;   // stock = stock + 20 -> 70
  stock -= 5;    // 65
  stock *= 2;    // 130
  stock ~/= 4;   // 32
  stock++;       // 33
  stock--;       // 32
  print(stock);
}
32

x++ uses the value then adds 1; ++x adds 1 then uses it. The difference shows only when the expression's value is used:

void main() {
  int a = 5;
  int b = a++;   // b gets 5, then a becomes 6
  int c = ++a;   // a becomes 7, then c gets 7
  print('$a $b $c');
}
7 5 7

Operator precedence

Dart follows the usual maths order: *, /, ~/ and % before + and -; comparisons after arithmetic; && before ||. Use brackets to make the order clear.

void main() {
  print(2 + 3 * 4);       // 14
  print((2 + 3) * 4);     // 20
  print(10 - 4 - 3);      // 3 (left to right)
}
14
20
3

The ternary operator ?:

condition ? valueIfTrue : valueIfFalse picks one of two values in a single expression:

void main() {
  int marks = 41;
  String result = marks >= 33 ? 'Pass' : 'Fail';
  print(result);
}
Pass

dart:math

import 'dart:math'; gives maths functions and constants:

import 'dart:math';

void main() {
  print(sqrt(144));          // 12.0
  print(pow(2, 10));         // 1024
  print(max(17, 42));        // 42
  print(pi.toStringAsFixed(4));
  print((7.456).round());    // 7
  print(-8.abs());           // careful: this is -(8.abs())
  print((-8).abs());         // 8
}
12.0
1024
42
3.1416
7
-8
8

Useful number methods: abs(), round(), floor(), ceil(), toStringAsFixed(n), clamp(lo, hi).

Example: discount and GST bill

void main() {
  const price = 2499.0;
  const discountPercent = 10;
  const gstPercent = 18;
  final afterDiscount = price - price * discountPercent / 100;
  final gst = afterDiscount * gstPercent / 100;
  final total = afterDiscount + gst;
  print('After discount: ${afterDiscount.toStringAsFixed(2)}');
  print('GST           : ${gst.toStringAsFixed(2)}');
  print('Total         : ${total.toStringAsFixed(2)}');
}
After discount: 2249.10
GST           : 404.84
Total         : 2653.94

Common mistakes

  • Expecting 7 / 2 to be 3. In Dart it is 3.5; use 7 ~/ 2 for 3.
  • Assigning int half = 10 / 2;. / gives a double, so this is an error even though the answer is 5.
  • Using = where you meant == inside a condition.
  • Writing -8.abs() and expecting 8: the method runs first, then the minus is applied.
  • Comparing doubles with == after calculations: 0.1 + 0.2 == 0.3 is false.

Key points

  • / always returns a double; ~/ returns the whole-number part; % gives the remainder.
  • Comparisons return bool; &&, || and ! combine them, with short-circuiting.
  • +=, -=, *=, ~/= and ++/-- are shortcuts for updating a variable.
  • Brackets beat precedence rules; use them for clarity.
  • cond ? a : b chooses a value; dart:math supplies sqrt, pow, max, min, pi and Random.

Practice questions

1. What do 17 ~/ 5, 17 % 5 and 17 / 5 give?

3, 2 and 3.4.

2. Write a program that converts 3725 seconds into hours, minutes and seconds.
void main() {
  int total = 3725;
  int h = total ~/ 3600;
  int m = total % 3600 ~/ 60;
  int s = total % 60;
  print('$h h $m m $s s');   // 1 h 2 m 5 s
}
3. What is printed? int a = 3; print(a++ + ++a);

8. a++ gives 3 and makes a 4; ++a makes a 5 and gives 5; 3 + 5 = 8.

4. Use the ternary operator to print Even or Odd for a number n.
void main() {
  int n = 27;
  print(n % 2 == 0 ? 'Even' : 'Odd');   // Odd
}
5. Why does int half = 10 / 2; fail, and how do you fix it?

/ always returns a double (here 5.0), which cannot be stored in an int. Use 10 ~/ 2, or declare double half.