Learn Pascal
1G - Punctuation and Indentation

Since Pascal ignores end-of-lines and spaces, punctuation is needed to tell the compiler when a statement ends.

You MUST have a semicolon following:

  1. the program heading
  2. each constant definition
  3. each variable declaration
  4. each type definition (to be discussed later)
  5. almost all statements
The last statement in the program, the one immediately preceding the END, does not require a semicolon. However, it's harmless to add one, and it saves you from having to add a semicolon if suddenly you had to move the statement higher up.

Indenting is not required. However, it is of great use for the programmer, since it helps to make the program clearer. If you wanted to, you could have a program look like this:
     program Stupid; const a=5; b=385.3; var alpha,beta:real; begin alpha := a + b; beta:= b / a end.
But it's much better for it to look like this:

     program Stupid;

     const
        a = 5;
        b = 385.3;

     var
        alpha,
        beta : real;

     begin     (* main *)
        alpha := a + b;
        beta := b / a
     end.      (* main *)

In general, indent two to four spaces for each block. Skip a line between blocks (such as between the const and var blocks).

Most importantly, use comments liberally! If you ever return to a program that you wrote ten years ago, you probably wouldn't remember the logic unless you documented it. It is a good idea to comment the main executable part of the program, to distinguish it from subprograms.


Previous lesson

To assignment

Contents

Index

e-mail me


taoyue@mit.edu
Copyright © 1997-2001 Tao Yue. All rights reserved.