Key Takeaways: Variables, Types, and Expressions
The Big Ideas
1. Variables Are Named Memory Locations
A variable is a labeled box in memory that holds a value. Pascal requires you to declare every variable before using it, specifying both its name and its type. This declaration-before-use rule catches typos at compile time and forces you to think about your data before writing code.
2. Every Variable Has a Type
A type determines three things: what values the variable can hold, how much memory it occupies, and what operations are legal. The Big Five types — Integer, Real, Char, String, Boolean — cover the vast majority of programming needs.
3. Strong Typing Is a Safety Net (Threshold Concept)
Pascal's strong type system prevents you from accidentally mixing incompatible types. When the compiler reports a type mismatch, it is catching a potential bug before your program ever runs. This is not a limitation — it is a feature that has prevented real-world disasters costing hundreds of millions of dollars.
4. Use Constants for Fixed Values
The const section lets you give meaningful names to values that do not change. Named constants eliminate "magic numbers," make code self-documenting, and allow you to change a value in one place rather than hunting through the entire program.
5. Assignment (:=) and Comparison (=) Are Distinct
Pascal uses := for "becomes" (assignment) and = for "is equal to" (comparison). This prevents the entire category of bugs where assignment is confused with comparison — a common error in C and its descendants.
6. Integer Division (div) and Real Division (/) Are Different Operations
The / operator always returns a Real result. The div operator performs integer division (truncating the decimal part). Choosing the wrong one is a common source of bugs.
7. Type Conversions Must Be Explicit
Pascal does not silently convert between incompatible types. To go from Real to Integer, you must call Trunc or Round. To go from String to Integer, you must call StrToInt. Each explicit conversion documents your intent.
Quick Reference
| Concept | Syntax/Example |
|---|---|
| Declare a variable | var age: Integer; |
| Assign a value | age := 25; |
| Declare a constant | const PI = 3.14159; |
| Real division | 7 / 3 gives 2.333... |
| Integer division | 7 div 3 gives 2 |
| Modulus (remainder) | 7 mod 3 gives 1 |
| Comparison | if age = 25 then ... |
| Not equal | if age <> 25 then ... |
| Boolean operators | and, or, not, xor |
| Real to Integer | Trunc(3.7) = 3, Round(3.7) = 4 |
| Char to code | Ord('A') = 65 |
| Code to Char | Chr(65) = 'A' |
| String to Integer | StrToInt('42') = 42 (needs SysUtils) |
| Integer to String | IntToStr(42) = '42' (needs SysUtils) |
| String length | Length('Hello') = 5 |
| String indexing | s[1] = first character (1-based!) |
| Format Real output | WriteLn(x:0:2) = 2 decimal places |
| Range checking | {$R+}` enables, `{$R-} disables |
Common Mistakes to Avoid
- Using
=for assignment — use:=instead - Storing a
Realin anInteger— useTruncorRound - Using
/when you wantdiv— remember/always returnsReal - Forgetting parentheses in Boolean expressions —
(x > 5) and (x < 10), notx > 5 and x < 10 - Accessing
s[0]for the first character — Pascal strings are 1-indexed; uses[1] - Using uninitialized variables — always assign before you read
- Assuming
Realarithmetic is exact —0.1 + 0.2does not equal0.3exactly
Connections
- Chapter 2 (Your First Program): We used
WriteLnto display output. Now we know how to store data in variables and display computed results. - Chapter 4 (Conditionals and Input): The Boolean type and comparison operators from this chapter are the foundation of
if..then..elsedecisions. We will also learn to read user input into our declared variables. - Chapter 5 (Loops): Variables will serve as loop counters and accumulators.
- Chapter 6 (Arrays): Arrays solve Rosa's scalability problem — instead of
amount1,amount2,amount3, we will use a singleamountsarray. - Chapter 8 (Records and Enumerations): Records group related variables (amount, category, date) into a single unit. Enumerated types replace category strings with compile-time-checked values.