# Variables, Types, and Expressions

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/variables-types-and-expressions

## 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.

| Type | Description | Example |
| --- | --- | --- |
| **Integer** | Whole numbers, exact | `42` |
| **Float** | Fractional numbers, approx | `3.14` |
| **Boolean** | Truth values | `true` |
| **String** | Text characters | `"hello"` |

## 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 \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.2$ does not exactly equal $0.3$.

| Feature | Mathematical View | Programming Reality |
| --- | --- | --- |
| **Assignment** | `x = x + 1` is false | `x` becomes old `x` + 1 |
| **Float Equality** | $0.1 + 0.2 == 0.3$ | False due to rounding |

## Practice questions

7 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. In most languages `0.1 + 0.2 == 0.3` evaluates to **false**. Why do experienced programmers compare floats with a tolerance instead?

A. Because 0.1 has no exact binary representation
B. Because addition is performed in a different order internally
C. Because 0.3 is stored exactly while 0.1 and 0.2 are not
D. Because the equality operator is unreliable for all numeric types

**Answer:** A. Because 0.1 has no exact binary representation

**Why:** The error exists before any arithmetic: 0.1 in binary is 0.000110011… repeating, exactly as 1/3 is in decimal. Adding two slightly-wrong numbers gives a slightly-wrong sum that misses the slightly-wrong stored 0.3, which is why money is stored in integer pence and floats are compared within an epsilon.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/in-most-languages-0-1-0-2-0-3-evaluates-to-false-why-do

### 2. Match each data type to its defining property.

**Answer:**

- Integer → Whole numbers, stored exactly
- Float → Numbers with a fractional part, stored approximately
- Boolean → One of exactly two values, true or false
- String → Text, which + joins rather than adds

**Why:** Exactness is what separates integers from floats, and it is why counting is safe in integers but accumulating fractions is not. Booleans carry the verdicts that conditions produce, and strings are the type on which + changes meaning.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/match-each-data-type-to-its-defining-property

### 3. Order the evaluation steps of the expression 2 + 3 * (8 - 6) / 2.

**Answer:**

1. Evaluate the parentheses: 8 - 6 = 2
2. Multiply: 3 * 2 = 6
3. Divide: 6 / 2 = 3
4. Add: 2 + 3 = 5

**Why:** The value is 5. Because * and / bind equally, the multiplication is done before the division simply by position; had you divided first the answer would be unchanged here, but with integer division it would not be.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/order-the-evaluation-steps-of-the-expression-2-3-8-6-2

### 4. Select every statement that is TRUE.

A. Two floats should be compared by checking their difference is smaller than a small tolerance
B. Assignment and mathematical equality are the same relation
C. Multiplication binds more tightly than addition
D. The symbol + can mean addition or text-joining depending on the operand types
E. 0.1 + 0.2 tests exactly equal to 0.3 in standard floating-point arithmetic

**Answer:** A. Two floats should be compared by checking their difference is smaller than a small tolerance; C. Multiplication binds more tightly than addition; D. The symbol + can mean addition or text-joining depending on the operand types

**Why:** Assignment is a command, not an equality claim, and floats are approximate, so options 1 and 3 are false. The type-dependence of +, the precedence of *, and tolerance-based comparison are all exactly as taught.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/select-every-statement-that-is-true

### 5. Because a float stores only finitely many digits, two computations that are mathematically equal can produce values that differ slightly.

**Answer:** True

**Why:** True. Every stored float is rounded, and two different routes to the same mathematical value round at different places, so the tiny errors differ. That is precisely why exact equality tests on floats are unreliable.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/because-a-float-stores-only-finitely-many-digits-two-computations

### 6. In mathematics x = x + 1 is a contradiction with no solution. In most programming languages it is a routine instruction. Why do the same symbols mean such different things?

A. Because the equals sign in programming is a typo inherited from mathematics and should be read as ≈.
B. Because programming languages use approximate arithmetic, so the two sides need not be exactly equal.
C. Because the programming version is an instruction, not an assertion.
D. Because x is a constant in mathematics and a variable in programming.

**Answer:** C. Because the programming version is an instruction, not an assertion.

**Why:** The line is a command with an order of operations: evaluate the right side using the current x, then store the result. Nothing is asserted, so nothing can be contradicted, and the two x's are different moments the notation conceals. A mathematical variable stands for one fixed value throughout; a program variable is a container whose contents change over time.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/in-mathematics-x-x-1-is-a-contradiction-with-no-solution-in-most

### 7. The symbol + adds numbers but joins text. Sort each expression by what + actually does.

**Answer:**

- Adds numbers: 3 + 4, 1.5 + 2
- Joins text: the quoted text 3 plus the quoted text 4, the quoted word cat plus the quoted word fish, the quoted phrase total: plus the quoted text 7

**Why:** The numeric cases give 7 and 3.5; the quoted cases give 34, catfish and total: 7. The same symbol means two different operations, which is why knowing a value's type is not optional.

Page: https://tryals.app/practice/mathematics-i/variables-types-and-expressions/the-symbol-adds-numbers-but-joins-text-sort-each-expression-by
