Courses / Mathematics I
Programming Elements

Variables, Types, and Expressions

Mathematics I 322 words Free to read

Storing and Combining Values

A program computes by manipulating values held in variables. A variable is a named container: assignment stores a value into it, and using the name later retrieves the current value. The crucial mental model is that assignment is not mathematical equality. In most languages x = x + 1 means "compute x + 1, then store the result back into x" — updating the variable. This is a command (do this), not an assertion (this is true), which is why it would be nonsense as an equation.

Every value has a data type that determines what it can hold and what operations apply:

The type controls how operators behave. The symbol + adds two numbers but concatenates (joins) two strings — same symbol, different operation depending on type. Mixing types can cause errors or surprising conversions, so knowing a value's type is essential.

Expressions combine values and variables with operators to produce a new value, evaluated using precedence rules (multiplication before addition, as in ordinary mathematics, with parentheses overriding). 2 + 3 * 4 evaluates to 14, not 20, because * binds tighter than +.

A subtle numerical caution for mathematicians: floating-point numbers are stored with finite precision, so they are approximate. 0.1 + 0.2 does not exactly equal 0.3 in most languages — a tiny rounding error remains. Never test floats for exact equality; compare within a small tolerance instead.

Common pitfall: reading assignment x = x + 1 as a mathematical equation (which would be a contradiction), and testing floating-point values for exact equality. Assignment is a command that updates the variable, not an equality claim. And floats are approximate — 0.1 + 0.2 != 0.3 exactly — so compare them within a tolerance, never with strict ==.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

11practice questions
2interactive scenes
Start Mathematics I free

Programming Elements