Courses / Computer Science I
Programming I

Variables, Expressions, and Types

Computer Science I 229 words Free to read

Naming Values and Expressions

A variable is a named box holding a value your program can read and change. Assignment puts a value into a variable, written as x = 5, read as "let x become 5." Assignment is directional: the right side evaluates first, then stores into the left.

An expression is any combination of values, variables, and operators evaluating to a single value. Operator precedence dictates that multiplication binds tighter than addition, so 3+4×2=113 + 4 \times 2 = 11. Parentheses override this: (3+4)×2=14(3 + 4) \times 2 = 14.

Common pitfall: Never read x = x + 1 as a mathematical equation. It is a command: read current x, add 1, and store the result back into x.

Types and Operations

Every value has a type dictating what it holds and which operations are valid. Operations depend heavily on types, especially with overloaded operators like +.

TypeHoldsExample
Integer (int)Whole numbers42, -7
FloatNumbers with decimals3.14
Boolean (bool)true or falsetrue
StringText"hello"

Distinction: The + operator adds two integers (1+1=21 + 1 = 2), but it concatenates two strings ("1" + "1" = "11"). Mixing types carelessly causes subtle bugs.

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

Programming I