Learn Pascal
1A - Program Structure


The basic structure of a Pascal program is:

     PROGRAM ProgramName (FileList);

     CONST
        (* Constant declarations *)

     TYPE
        (* Type declarations *)

     VAR
        (* Variable declarations *)

     (* Subprogram definitions *)

     BEGIN
        (* Executable statements *)
     END.

The elements of a program must be in the correct order, though some may be omitted if not needed. Here's a program that does nothing, but has all the REQUIRED elements:

     program DoNothing;
     begin
     end.

Pascal comments start with a (* and end with a *). You can't nest comments:
     (* (* *) *)
will yield an error because the compiler matches the first (* with the first *), ignoring everything inbetween. The second *) is left without its matching (*.

In Turbo Pascal, {Comment} is an alternative to (* Comment *). The opening brace signifies the beginning of a block of comments, and the ending brace signifies the end of a block of comments.

Commenting has two purposes: first, it makes your code easier to understand. If you write your code without comments, you may come back to it a year later and have a lot of difficulty figuring out what you've done or why you did it that way. Another use of commenting is to figure out errors in your program. When you don't know what is causing an error in your code, you can comment out any suspect code segments. Remember the earlier restriction on nesting comments? It just so happens that braces {} supersede parenthesis-stars (* *). You will NOT get an error if you do this:

     { (* Comment *) }

So, if you have Turbo Pascal, I suggest using standard comments (* *), leaving the braces for debugging.

All spaces and end-of-lines are ignored by the Pascal compiler unless they are inside a string. However, to make your program readable by human beings, you should indent your statements and put separate statements on separate lines.


Previous lesson

Next lesson

Contents

Index

e-mail me


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