[Robelle] [SmugBook] [Index] [Prev] [Next]

Pascal Programming Tips

If you are programming in Pascal for the first time, here are a few things to remember:

Pascal identifiers may contain an underline, as in CUST_REC, but that is non-standard. Pascal comments are (* xxxxx *) or { xxxxx }. Strings are in single quotes ('xxx') not double ("). Array indexing starts at 1, not at 0 (the most insidious difference if you are used to 0-based strings or arrays). Constants are written in the normal way (Pascal does not require 53D for a double INTEGER). Use CONST to equate names to constants. INTEGER is 32 bits by default. To use both size integers, define two TYPEs: INT = -32768 .. 32767 and DBL = INTEGER. For logical-type variables, use BOOLEAN. For byte-type, use CHAR. Arithmetic is normal, except that "/" means REAL divide and DIV means integer divide. Pointer notation differs from some other languages, but you will get used to it. Pascal uses brackets "[]" for indexing arrays and parentheses "()" for passing parameters. Parameters are by value, unless you specify VAR, and actual parameters must match declared ones in both type and size (languages such as C allow you to pass almost anything); this keeps you from making many stupid errors but can be frustrating when writing general system routine.

Pascal does AND/OR before =, <, or >, so you need parentheses in a test like this: IF (COUNT<MAX) AND (PRINT=1) THEN. To get in the habit, put redundant parentheses around all compares: IF (INDEX = MAX) THEN. In Pascal, you should assume full evaluation of logical expressions, not partial evaluation. Some languages branch out of IF A AND B THEN immediately if A is FALSE. Some Pascal compilers use full and some use partial. Rewriting code that assumes partial for a compiler that uses full is difficult.

Pascal has some features that may not be familiar to the SPL user: record, type, set, new variable, scalar type, subrange. Programmers should learn these powerful features.

Pascal has a REPEAT UNTIL loop and WHILE DO. CASE has labels on each statement (they need not be in order) and stops on an END, but has no BEGIN after the OF. The FOR loop uses TO, not UNTIL, as in FOR I:=1 TO 9 DO. In Pascal, FOR loops are well-defined and you need them to step properly through subranges (e.g., CONST N=9; VAR X : 1..N; BUF : ARRAY 1..N OF CHAR; FOR X := 1 TO N DO BUF[X] := ' '). Try to rewrite this example with a REPEAT or WHILE loop, without getting a subrange violation.

If you are writing Pascal for MPE/iX, consult Stan Sieler's paper "How to Code: Pascal."


[Robelle] [SmugBook] [Index] [Quality] [Prev] [Next]