var allows reassignment, final is assigned once at runtime, and const is a compile-time constant.

Program

Play the program to change count, then capture a final total.

variables.dart
Replay: real traced execution (multi-file project)
void main() {
  var count = 1;
  count = count + 2;
  final total = count * 10;
  const tax = 5;
  print(total + tax);
}
  1. count ← 1

    1void main() {2  var count = 1;3  count = count + 2;
    values this step1count
  2. count ← 3

    2var count = 1;3count = count + 2;4final total = count * 10;
    values this step1 3count
  3. total ← 30

    3count = count + 2;4final total = count * 10;5const tax = 5;
    values this step30total3count
  4. tax ← 5

    4final total = count * 10;5const tax = 5;6print(total + tax);
    values this step5tax
  5. print(total + tax);

    5  const tax = 5;6  print(total + tax);7}
    output35
    values this step30total5tax

Follow the Values

  1. count starts at 1.
  2. count + 2 changes count to 3.
  3. The final total is 30.
  4. tax is the constant 5.
  5. The program prints 35. | name | value | | --- | --- | | starting count | 1 | | updated count | 3 | | total | 30 | | tax | 5 | | stdout | 35 |
var Reassignable runtime binding.
final Assigned once; the value can come from any expression.
const Compile-time constant; value must be known at compile time.

Exercise: variables.dart

Reproduce the output 35, then identify the updated count, total, and tax values that lead to it.