Courses / Mathematics I
Programming Elements

Variables, Types, and Expressions

Mathematics I 234 words Free to read

Storing Values and Types

A program computes by manipulating values held in variables. A variable is a named container where assignment stores a value, and using its name retrieves it.

Assignment is a command, not a math equation. Writing x = x + 1 means compute x + 1 and store the result back into x, updating the variable.

Every value has a data type dictating valid operations. The symbol + adds numbers but concatenates strings.

TypeDescriptionExample
IntegerWhole numbers, exact42
FloatFractional numbers, approx3.14
BooleanTruth valuestrue
StringText characters"hello"
Assignment reads the old value, computes, and overwrites it

Expressions and Pitfalls

Expressions combine values and variables with operators. They follow precedence rules where multiplication happens before addition, overridden by parentheses.

For example, 2+3×42 + 3 \times 4 evaluates to 14, because the operator * binds tighter than +.

Floating-point numbers use finite precision and are approximate. Due to rounding, 0.1+0.20.1 + 0.2 does not exactly equal 0.30.3.

FeatureMathematical ViewProgramming Reality
Assignmentx = x + 1 is falsex becomes old x + 1
Float Equality0.1+0.2==0.30.1 + 0.2 == 0.3False due to rounding

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

Programming Elements