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, evaluates to 14, because the operator * binds tighter than +.
Floating-point numbers use finite precision and are approximate. Due to rounding, does not exactly equal .
| Feature | Mathematical View | Programming Reality |
|---|---|---|
| Assignment | x = x + 1 is false | x becomes old x + 1 |
| Float Equality | False due to rounding |