Variables store values that later expressions can reuse.

assignment An assignment stores the value on the right side of `=` in the variable on the left.

Variables

unitPrice
Variables.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static void Main()
    {
        int unitPrice = 12;
        int quantity = 3;
        int total = unitPrice * quantity;

        Console.WriteLine($"unit={unitPrice}");
        Console.WriteLine($"total={total}");
    }
}
using System;

class Program
{
    static void Main()
    {
        int unitPrice = 8;
        int quantity = 3;
        int total = unitPrice * quantity;

        Console.WriteLine($"unit={unitPrice}");
        Console.WriteLine($"total={total}");
    }
}
using System;

class Program
{
    static void Main()
    {
        int unitPrice = 20;
        int quantity = 3;
        int total = unitPrice * quantity;

        Console.WriteLine($"unit={unitPrice}");
        Console.WriteLine($"total={total}");
    }
}
  1. unitPrice ← 12, quantity ← 3, total ← 36

    4{5    static void Main()6    {7        int unitPrice→ 12 = 12; //@unitPrice=8, 208        int quantity→ 3 = 3;9        int total→ 36 = unitPrice12 * quantity3;1011        Console.WriteLine($"unit={unitPrice12}");12        Console.WriteLine($"total={total36}");13    }
    outputunit=12
    total=36
  1. unitPrice ← 8, quantity ← 3, total ← 24

    4{5    static void Main()6    {7        int unitPrice→ 8 = 8;8        int quantity→ 3 = 3;9        int total→ 24 = unitPrice8 * quantity3;1011        Console.WriteLine($"unit={unitPrice8}");12        Console.WriteLine($"total={total24}");13    }
    outputunit=8
    total=24
  1. unitPrice ← 20, quantity ← 3, total ← 60

    4{5    static void Main()6    {7        int unitPrice→ 20 = 20;8        int quantity→ 3 = 3;9        int total→ 60 = unitPrice20 * quantity3;1011        Console.WriteLine($"unit={unitPrice20}");12        Console.WriteLine($"total={total60}");13    }
    outputunit=20
    total=60

Follow the Total

  1. unitPrice starts at 12.
  2. quantity starts at 3.
  3. total = unitPrice * quantity multiplies 12 * 3.
  4. total becomes 36.
  5. The program prints unit=12 and total=36. | unitPrice | quantity | total | | --- | --- | --- | | 8 | 3 | 24 | | 12 | 3 | 36 | | 20 | 3 | 60 |

Exercise: Variables.cs

Reproduce unit=12 and total=36, then use the pinned unitPrice values 8 and 20 to predict each total.