Home Random Page



The Elements of a Program

Elementary Pascal Programs

This chapter covers:

  • The parts of a program
  • Pascal variables
  • Meaningful identifiers
  • Field width specifiers
  • Precedence of operators
  • Reading and writing variables.

The parts of a program

The simplest Pascal programs that do anything at all useful, contain three important parts.

  1. The title of the program.
  2. The VAR list.
  3. The body of the program.

The Program Title

The program title, sometimes called the program heading follows the form:


First is the reserved word PROGRAM, a reserved word is a word belonging to the programming language. All programming languages have them. After PROGRAM comes the program's name, this can be almost anything chosen by the programmer except a reserved word. You will also find that the first character of the program name, like all identifiers must be a letter.

The VAR list

The VAR list contains a number of data items called variables. Each variable has a name, called its identifier. These identifiers are names chosen by the programmer to refer to these variables. These names can be almost anything, but they must begin with a letter. More on this later. These variables, are said to be declared when they are put into the VAR list. The word VAR is short for variable, and a variable is merely a storage device for values referred to, and manipulated by their identifier. In programs each of these variables must have a type. The elementary types in Pascal include

  • INTEGER, the whole numbers.
  • REAL, the decimal numbers.
  • CHAR, single letters, numbers, or other printing ASCII characters.
  • STRING, groups of ASCII characters.

All variable declarations follow the form:

VAR <anyname> : <variable type>;

The body of a program

The body of a Pascal program is all the text between the first BEGIN and the last END. - of course there can only be one END with a full-stop after it, and it must be the last one. This is the minimum body for a Pascal program:


The Elementary Pascal Types

The type INTEGER

Integers are the set of whole numbers, and include both negative and positive. In Pascal the size of the INTEGER value is limited. Usually, there is something along the line of the reserved words MAXINT (maximum integer) and MININT (minimum integer). The size of these are constrained by the components of each particular computer. This is what is called being machine dependent, on the Hewlett Packard workstation I use, they are equal to 2,147,483,647, and -2,147,483,648. To declare a variable of this type requires a piece of code similar to

VAR student_date_of_birth : INTEGER;

These two lines of code declares the identifier student_date_of_birth to be a variable of the type INTEGER. Because Sandra, a friend of mine, said the draft I showed her got too technical, too soon, I have rewritten the first chapter. She felt that programs should appear further forward in the book than I did so I have moved this program as far forward as I felt it possible. Hopefully by now you understand enough about integers, variables, and the VAR list so that this program will not be too far above you. If it is, blame her not me. On the other hand, if you think that this program appears at an appropriate place in the book, then of course I wrote the book and take full credit for it. This relatively simple program asks the user for two integers, and then it adds these integers together, and writes out the result on the screen.

Program 1

PROGRAM add(INPUT,OUTPUT); { This program asks for two numbers then adds them together } VAR first_integer, second_integer, answer : INTEGER; BEGIN WRITE('Enter an integer : '); READLN(first_integer); WRITE('Enter another integer : '); READLN(second_integer); answer := first_integer + second_integer; WRITELN('The answer = ',answer:1); END. { program add }

In the VAR list you can see that there are three variables which are declared, these are first_integer, second_integer, and answer. It is the line


which puts the first number into the variable first_integer, and the line


which puts the second number into second_integer. These numbers are then added together in the line

answer := first_integer + second_integer;

and assigned to the variable answer before it's value is written to the screen in the line

WRITELN('The answer = ',answer:1);

When you run the program you will get a better feel for how it all works. You will see however that it is the value contained in the variable answer which is written out in the last line of the program, rather than the word itself. Now we will have a quick look at the other elementary types, before looking at some more programs.

The type REAL

Reals, are the floating point numbers (decimals) and scientific notation numbers (exponents). The numbers 3.14, -21.2, 15, 12.24321, -0.22311, 0 are all REAL numbers. The maximum and minimum REAL values are much larger than the integers, because of the exponential nature of REAL numbers, but there is a machine dependent limit on their size. It is similarly declared with a piece of code similar to:

VAR student_account_bank_balance : REAL;

In this declaration the variable identifier student_account_bank_balance is declared to be a REAL number.

The type CHAR

This type CHAR, is short for character, and the set of printable ASCII characters may be found in the ASCII table in Appendix I. These printable characters range from the character <space> to the character <~> (tilde) (the <space> has an ASCII value of 32, and the <~> (tilde) a value of 126). To use the type CHAR requires the lines

VAR the_answer : CHAR;

It may seem strange that the variable the_answer a ten character identifier has been declared in order to hold a single character of input, but this is in fact the case.

The type STRING

This type is a list of characters of up to the specified maximum size, and is declared with a piece of code similar to:

VAR anyname : STRING[12];

The number twelve being the maximum number of characters that can be contained in the STRING called anyname . This number is chosen by the programmer, but it is limited to 255 characters, but this is large enough for any sensible applications. It is possible to create your own storage devices which will hold as many characters as you like, but that will come much later on in this book.

The Elements of a Program

Date: 2016-03-03; view: 520

<== previous page | next page ==>
Strategic priorities | Comments in a program
doclecture.net - lectures - 2014-2019 year. Copyright infringement or personal data (0.001 sec.)