Chapter 4 Key Takeaways
The Big Picture
Programs exist to serve people. A program that computes the correct answer but cannot communicate it clearly has failed. Input and output are not afterthoughts — they are the interface between your logic and the human who depends on it.
Core Concepts
1. Write vs. WriteLn
WriteLnoutputs text and moves the cursor to the next line. Use it for most output.Writeoutputs text and keeps the cursor on the same line. Use it for prompts.WriteLnwith no arguments outputs a blank line.- Both accept multiple arguments of any type, separated by commas. No spaces are inserted between arguments — you must include them yourself.
2. Formatting with Field Widths
:widthright-justifies any value in a field of at leastwidthcharacters. Example:WriteLn(42:10)produces42.:width:decimalsdisplays Real numbers in fixed-point notation. Example:WriteLn(3.14:8:2)produces3.14.- Field width is a minimum, never a maximum. Values wider than the field are never truncated.
- Without formatting, Real numbers display in scientific notation. Always format them explicitly.
3. Read vs. ReadLn
ReadLnreads input and discards the rest of the line (including the newline). Always prefer this for interactive programs.Readreads input but leaves the remaining characters in the buffer. This causes the "skipped prompt" bug when the leftover newline is consumed by the next read.- The prompt pattern:
Write('Prompt: '); ReadLn(variable);
4. The Input Buffer
- User input goes into a buffer.
ReadandReadLnconsume characters from this buffer. Readleaves the newline in the buffer.ReadLnclears it.- When a prompt seems to be "skipped," the cause is almost always a leftover newline from a previous
Read.
5. Input Validation with Val
- Never trust user input. Reading a non-numeric string into a numeric variable crashes the program (runtime error 106).
- The Val pattern: Read as a string, convert with
Val(str, num, code), checkcode = 0for success. - Validate ranges after type validation: a valid number might still be out of range.
6. Text-Based UI Design
- Use headers, separators, and blank lines for visual structure.
- Use consistent field widths for aligned columns in tables.
- The CRT unit provides colors and screen control (Free Pascal extension, not standard Pascal).
Key Syntax
| Operation | Syntax | Example |
|---|---|---|
| Print with newline | WriteLn(args); |
WriteLn('Total: ', total:0:2); |
| Print without newline | Write(args); |
Write('Enter name: '); |
| Field width | value:width |
WriteLn(n:10); |
| Decimal places | value:width:decimals |
WriteLn(price:8:2); |
| Read entire line | ReadLn(variable); |
ReadLn(name); |
| Blank line | WriteLn; |
WriteLn; |
| String-to-number | Val(str, num, code); |
Val(input, amount, code); |
Common Mistakes to Avoid
- Forgetting to format Real numbers. Unformatted reals display in scientific notation — unreadable for users. Always use
:width:decimals. - Using
WriteLnfor prompts. The cursor drops to the next line, separating the prompt from where the user types. UseWritefor prompts. - Using
Readinstead ofReadLnin interactive programs. The leftover newline causes the next input to be skipped. - Reading numeric input directly without validation. If the user types non-numeric text, the program crashes. Use the string-then-Val pattern.
- Assuming
Writeinserts spaces between arguments. It does not.WriteLn(10, 20)produces1020, not10 20.
Connections
- From Chapter 3: Type declarations determine what constitutes valid input.
ReadLnrelies on declared types to parse user input correctly. - To Chapter 5: Input and decisions combine to create interactive programs. You will use validated input to drive
if-then-elseandcaselogic. - To Chapter 6: Loops will replace the repetitive copy-paste code in PennyWise v0.3, letting us read any number of expenses.
- To Chapter 7: Procedures will encapsulate the prompt-validate-read pattern into reusable building blocks.
- To Chapter 13: File I/O extends these concepts from the keyboard to disk files, where
Read(notReadLn) becomes more appropriate.