Naming Values
A variable is a named box that holds a value the program can read and change. Assignment puts a value into a variable, written in most languages as x = 5 — read as "let x become 5," not as a claim that x equals 5 forever. Crucially, assignment is directional: the right side is evaluated first, then stored into the name on the left. So x = x + 1 reads the current x, adds one, and stores the result back — it is not a contradiction.
An expression is any combination of values, variables, and operators that evaluates to a single value: 3 + 4 * 2 evaluates to 11 because multiplication binds tighter than addition (operator precedence), just as in ordinary arithmetic. Parentheses override precedence: (3 + 4) * 2 is 14.
Every value has a type that determines what it can hold and what operations make sense:
| Type | Holds | Example |
|---|---|---|
| Integer (int) | Whole numbers | 42, -7 |
| Float | Numbers with decimals | 3.14 |
| Boolean (bool) | true or false | true |
| String | Text | "hello" |
Types matter because operations depend on them: + adds two integers but concatenates (joins) two strings, so "1" + "1" is "11", not 2. Mixing types carelessly is a frequent source of bugs.
Common pitfall: reading the assignment x = x + 1 as a false equation. It is not an equation — it is a command: evaluate the right-hand side using the current value of x, then store the result back into x. The old value is read before the new one is written.
An expression tree for 3 + 4 * 2 drawn in beats: the multiply node (4, 2) evaluates first to 8, then the add node (3, 8) evaluates to 11 — precedence made visible as bottom-up evaluation.