Courses / Computer Science I
Programming I

Variables, Expressions, and Types

Computer Science I 328 words Free to read

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:

TypeHoldsExample
Integer (int)Whole numbers42, -7
FloatNumbers with decimals3.14
Boolean (bool)true or falsetrue
StringText"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.

3+4×2=113 + 4 \times 2 = 11

Variables, Expressions, and Types

Practise this lesson

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

10practice questions
2interactive scenes
Start Computer Science I free

Programming I