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

  • WriteLn outputs text and moves the cursor to the next line. Use it for most output.
  • Write outputs text and keeps the cursor on the same line. Use it for prompts.
  • WriteLn with 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

  • :width right-justifies any value in a field of at least width characters. Example: WriteLn(42:10) produces 42.
  • :width:decimals displays Real numbers in fixed-point notation. Example: WriteLn(3.14:8:2) produces 3.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

  • ReadLn reads input and discards the rest of the line (including the newline). Always prefer this for interactive programs.
  • Read reads 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. Read and ReadLn consume characters from this buffer.
  • Read leaves the newline in the buffer. ReadLn clears 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), check code = 0 for 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

  1. Forgetting to format Real numbers. Unformatted reals display in scientific notation — unreadable for users. Always use :width:decimals.
  2. Using WriteLn for prompts. The cursor drops to the next line, separating the prompt from where the user types. Use Write for prompts.
  3. Using Read instead of ReadLn in interactive programs. The leftover newline causes the next input to be skipped.
  4. Reading numeric input directly without validation. If the user types non-numeric text, the program crashes. Use the string-then-Val pattern.
  5. Assuming Write inserts spaces between arguments. It does not. WriteLn(10, 20) produces 1020, not 10 20.

Connections

  • From Chapter 3: Type declarations determine what constitutes valid input. ReadLn relies 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-else and case logic.
  • 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 (not ReadLn) becomes more appropriate.