Hello! Last time, you used string interpolation to put values—and even a small calculation—inside a message. Now you’ll focus on the calculations themselves. By the end of this lesson, you’ll be able to calculate totals, divide quantities, and find what is left over. Allow about 35–40 minutes to watch, read, and run the examples.
Arithmetic produces a value
An expression such as 7 + 2 combines two values with an operator. Dart evaluates the expression to produce a result, which you can print, store in a variable, or place inside an interpolated string.
Watch the arithmetic demonstration in Dart Basics – Arithmetic operations in Dart by Johan Jurrius. The first part shows addition, subtraction, multiplication, and division; the second shows two ways to divide when you need whole groups or a remainder. Stop when the video moves on to compound assignment.
Dart Basics - Arithmetic operations in Dart
Watch the examples to see how Dart writes each operation and what it prints.
Watch basic arithmetic, paying particular attention to the fractional result of division. Then watch groups and remainders, where the examples use ~/ and %. You can stop at 00:04:01.
For a reference you can return to, read the Arithmetic operators subsection of Dart’s official Operators guide. Its examples use assert(...) to check expected results; you do not need to learn assert to understand the calculations.
Read the operator table and its examples to compare ordinary division, integer division, and remainder.
In the “Arithmetic operators” subsection, read the arithmetic examples from the opening sentence through the example that displays a quotient and remainder. Stop before the paragraph beginning “Dart also supports both prefix and postfix increment and decrement operators”; the operators after that point are not needed for this lesson.
Start with the familiar operations. Dart writes multiplication with *, not x. The minus sign can either subtract one value from another or reverse the sign of a single value:
void main() {
int units = 7;
int extra = 2;
print('Combined: ${units + extra}');
print('Difference: ${units - extra}');
print('Twice the units: ${units * 2}');
print('Opposite sign: ${-units}');
}
The four results are 9, 5, 14, and -7. Notice the braces around each calculation: as in the previous lesson, ${...} tells Dart to evaluate an expression before inserting its result into a string. None of these expressions changes units or extra.
Divide exactly, count whole groups, or find the leftover
Dart has three operators worth distinguishing when dividing numbers:
| Expression | Result | Meaning |
|---|---|---|
14 / 4 |
3.5 |
Divide; the result is a double |
14 ~/ 4 |
3 |
Count whole groups; the result is an int |
14 % 4 |
2 |
Find the remainder |
The / operator returns a double even when the answer is a whole number: 8 / 2 produces 4.0. That matters when you declare a variable to hold the result. Use double for /, rather than assuming an int will work.
Imagine packing 14 items into packs of four. Three complete packs use 12 items, leaving two unpacked:
void main() {
int items = 14;
int itemsPerPack = 4;
double exactRatio = items / itemsPerPack;
int completePacks = items ~/ itemsPerPack;
int unpackedItems = items % itemsPerPack;
print('Exact ratio: $exactRatio');
print('Complete packs: $completePacks');
print('Unpacked items: $unpackedItems');
}
This prints 3.5, 3, and 2, respectively. For these positive numbers, ~/ keeps the whole-number quotient rather than rounding 3.5 up to 4. The remainder tells you why three packs are not enough to hold everything:
Use / when you need the numerical ratio, ~/ when you need a count of complete groups, and % when you need the amount left over.
Combine calculations into a useful total
When an expression contains several operators, Dart performs multiplication and division (including ~/ and %) before addition and subtraction. Parentheses let you specify a different grouping—and often make your intention clearer.
For example, 20 - 4 * 3 is 8, because Dart multiplies first. (20 - 4) * 3 is 48, because the parentheses make the subtraction happen first. Operators at the same level work from left to right: 20 - 6 - 2 is 12, while 20 - (6 - 2) is 16.
Here is a complete purchase calculation. Save it as arithmetic.dart and run it with dart run arithmetic.dart:
void main() {
int pricePerItem = 12;
int quantity = 3;
int deliveryFee = 5;
int amountPaid = 50;
int subtotal = pricePerItem * quantity;
int total = subtotal + deliveryFee;
int change = amountPaid - total;
double halfOfTotal = total / 2;
print('Subtotal: $subtotal');
print('Total: $total');
print('Change: $change');
print('Half of total: $halfOfTotal');
}
You should see:
Subtotal: 36
Total: 41
Change: 9
Half of total: 20.5
Here, subtotal holds the result of multiplication, and total uses that result in another expression. If the delivery fee is charged once per purchase, the calculation is pricePerItem * quantity + deliveryFee, not (pricePerItem + deliveryFee) * quantity: the latter would charge delivery for every item.
Calculating a value does not automatically update an existing variable. If you later want quantity to increase, you must assign a new result, such as quantity = quantity + 1;. For now, the important distinction is between evaluating an expression and storing its result.
Key takeaways
Dart uses +, -, and * for addition, subtraction, and multiplication; a minus sign before one value reverses its sign. Division with / produces a double, while ~/ gives a whole-number quotient and % gives the remainder. Parentheses help you control and communicate how a longer calculation is grouped.
You can now calculate values and display them in meaningful messages. Next, you’ll use comparison and logical operators to construct conditions that a program can evaluate as true or false.