Sei sulla pagina 1di 3

Introduction to Programming

Lesson 2: Introduction to Pascal Programming



Pascal is a high-level language named after a French philosopher, Blaise Pascal, who invented the first adding machine.

The Programming Phases

A Pascal program typically goes through four phases:

running (executing).

editing, compiling, linking/loading, and

In the first phase, the program’s source code (a series of statements used to instruct the computer


perform a particular task) is created. The following line of source code instructs the computer


print Hello World! on the screen:


Writeln(‘Hello World!’);

The source code is typed and manipulated in a built-in program provided with Pascal, referred to

as an editor (displayed below). In the second phase, the source code is compiled (by
an editor (displayed below).
the second phase, the source code is compiled (by a program called a compiler), resulting in

the source code being converted to machine language (object code).

In the third phase (linking), the object code is converted to an executable image.

performed by a program called the linker/loader.

This task is

In the final phase, the computer executes the program one instruction at a time.

Interpreter versus Compiler An interpreter is a program that reads a high-level language program statement, determines the

operation to be performed, and executes the operation immediately. An interpreter, though easier to work with (than a compiler), must be loaded on your computer in order for the program

to run.

Introduction to Programming

Lesson 2: Introduction to Pascal Programming

A compiler translates the source code into a form that you can run at a later date. Programs that

are produced by a compiler run very fast. The compiler does not have to be loaded on your computer in order for the executable program to run.

Variables and Data Declarations

A variable represents a data storage location in the computer's memory in which a value can be

stored. Every reference to that name is actually a reference to the contents of that memory location. All variables being used in a program must be declared. A variable must be declared as a specific data type. A variable declaration is the process of setting aside memory for the variable. The data types of concern to us, with sample declarations are:

INTEGER for whole numbers, e.g., age : INTEGER; REAL for floating-point numbers numbers with an integer portion and a fractional portion, e.g., salary : REAL; CHAR for a single character, such as M or F for Gender, e.g., gender : CHAR; STRING for text, e.g., name : STRING [20]; (indicating that name is a string variable capable of storing 20 characters).

A variable name can contain letters of the alphabet, decimal digits, and the underscore character

(_). However, the variable name must begin with a letter. Pascal is not case-sensitive, therefore the variable names Gender and gender are seen as the same. Pascal has a number of Reserved words (words that are reserved for, or are a part, of the language), which cannot be used as variable names. For example, BEGIN, PROGRAM, or CHAR. It is useful to use variable names which give an indication of the data they represent. In a Pascal program, variables are declared as follows:


age : INTEGER; salary : REAL; gender : CHAR;

Variables with fixed values are called constants. Examples of values that remain constant throughout a program are Income Tax (15%), or pi (3.14159). Constants are declared as follows:


IncTax = 0.15;

pi = 3.14159;

Program Layout Below is the general format for a Pascal program:

PROGRAM name(INPUT, OUTPUT); CONST initialization of variables;


declaration of variables;

Introduction to Programming



statement 1;

statement 2;


Lesson 2: Introduction to Pascal Programming

INPUT indicates that data will be input via the keyboard, and OUTPUT indicates that data will be output to the monitor.

Data Input and Output Output Statements The two (2) output statements used in Pascal are:

Writeln(output list);

Write(output list);

outputs data to screen and then sends the cursor to a new line so that any output following will begin on a new line

does not cause the cursor to advance to a new line

For example, to display This is my first program! on screen, you code:

Writeln(‘This is my first program!’);

Input Statements There are two (2) input statements used in Pascal:

Readln(input list);

reads in the variables and then sends the cursor to a new line

Read(input list);

does not cause the cursor to advance to a new line

Let us examine the following syntax:

Write(‘Enter a number: ’); Readln(x);

The statement Readln(x); is storing the number that was entered in the variable named x. If the user enters 6, that value would then be stored in x.

Commenting or Documenting a Program Comments in a program are ignored by the compiler and they have no effect on how a program works. A comment can span a line or multiple lines, or be appended at the end of a line of code. A comment is enclosed between { and } or begins with (* and ends with *). Documentation may be used to indicate what a variable represents or what a segment of code does. The following are examples of comments using both methods:


price : REAL; qty : INTEGER; disc : REAL;

{price of item} {quantity purchased} (* discount allowed *)