Pascal Topic 1 Notes
Pascal Topic 1 Notes
CHAPTER
Pascal Overview
This chapter describes the basic definition and concepts of Pascal.
P ascal is a general purpose high level language that was originally developed by
Nicklaus Wirth in the early 1970s. It was developed for teaching programming as a
systematic discipline and to develop reliable and efficient programs.
Pascal is Algol based language and includes many constructs of Algol. Algol-60 is a subset
of Pascal. Pascal offers several data types and programming structures. It is easy to
understand and maintain the Pascal programs.
Pascal has grown in popularity in the teaching and academics arena for various reasons:
Easy to learn.
Structured language.
It produces transparent, efficient and reliable programs.
It can be compiled on a variety of computer platforms.
TUTORIALSPOINT
Simply Easy Learning Page 1
2
CHAPTER
Environment
This section describes the environment setup for running Pascal
T here are several Pascal compilers and interpreters available for general use.
Turbo Pascal: provides an IDE and compiler for running Pascal programs on
CP/M, CP/M-86, DOS, Windows and Macintosh.
Delphi: provides compilers for running Object Pascal and generates native code
for 32- and 64-bit Windows operating systems, as well as 32-bit Mac OS X and
iOS. Embarcadero is planning to build support for the Linux and Android operating
system.
Free Pascal: it is a free compiler for running Pascal and Object Pascal programs.
Free Pascal compiler is a 32 and 64 bit Turbo Pascal and Delphi compatible Pascal
compiler for Linux, Windows, OS/2, FreeBSD, Mac OS X, DOS and several other
platforms.
Turbo51: it is a free Pascal compiler for the 8051 family of microcontrollers, with
Turbo Pascal 7 syntax.
Oxygene: it is an Object Pascal compiler for the .NET and Mono platforms.
GNU Pascal (GPC): it is a Pascal compiler composed of a frontend to GNU
Compiler Collection.
We will be using Free Pascal in these tutorials. You can download Free Pascal for your
operating system from the link: Download Free Pascal
TUTORIALS POINT
Simply Easy Learning Page 3
3
CHAPTER
Program Structure
This section describes basic Pascal program structure so that we can take it as a reference
in upcoming chapters.
A
Pascal program basically consists of the following parts:
Program name
Uses command
Type declarations
Constant declarations
Variables declarations
Functions declarations
Procedures declarations
Main program block
Statements and Expressions within each blocks
Comments
Every Pascal program generally have a heading statement, a declaration and an execution
part strictly in that order. Following format shows the basic syntax for a Pascal program:
TUTORIALS POINT
Simply Easy Learning Page 6
Pascal Hello World Example
Following is a simple Pascal code that would print the words "Hello, World!":
program HelloWorld;
uses crt;
The first line of the program program HelloWorld; indicates the name of the
program.
The second line of the program uses crt; is a preprocessor command which tells the
compiler to include the crt unit before going to actual compilation.
The next lines enclosed within begin and end statements are the main program block.
Every block in Pascal is enclosed within a begin statement and an end statements.
However, the end statement indicating the end of the main program is followed by a
full stop (.) instead of semicolon (;).
The begin statement of the main program block is where the program execution
begins.
The lines within (*...*) will be ignored by the compiler and it has been put to add a
comment in the program.
The statement writeln('Hello, World!'); uses the writeln function available in Pascal
which causes the message "Hello, World!" to be displayed on the screen.
The statement readkey; allows the display to pause until the user presses a key. It is
part of the crt unit. A unit is like a library in Pascal.
TUTORIALS POINT
Simply Easy Learning Page 7
4
CHAPTER
Basic Syntax
You have seen a basic structure of Pascal program, so it will be easy to understand other
basic building blocks of the Pascal programming language.
This section shows the basic syntax of Pascal program.
Variables
A variable definitions are put in a block beginning with a var keyword, followed by
var
A_Variable, B_Variable ... : Variable_Type;
Pascal variables are declared outside the code-body of the function which means they are
not declared within the begin and end pairs, but they are declared after the definition of
the procedure/function and before the begin keyword. For global variables, they are
defined after the program header.
Functions/Procedures
In pascal a procedure is set of instructions to be executed, with no return value and a
function is a procedure with a return value. The definition of function/procedures will be
as follows:
Comments
TUTORIALS POINT
Simply Easy Learning Page 9
The multiline comments are enclosed within curly brackets and asterisks as {* ... *}.
Pascal allows single line comment enclosed within curly brackets { ... }.
Case Sensitivity
Pascal is a case non-sensitive language which mean you can write your variables,
functions and procedure in either case. Like variables A_Variable, a_variable and
A_VARIABLE have same meaning in Pascal.
Pascal Statements
Pascal programs are made of statements. Each statement specifies a definite job of the
program. These jobs could be declaration, assignment, reading data, writing data, taking
logical decisions, transferring program flow control, etc.
For example:
TUTORIALS POINT
Simply Easy Learning Page 10
The Pascal character set consists of:
The entities in a Pascal program like variables and constants, types, functions, procedures
and records etc. has a name or identifier. An identifier is a sequence of letter and digits,
beginning with a letter. Special symbols and blanks must not be used in an identifier.
TUTORIALS POINT
Simply Easy Learning Page 11
5
CHAPTER
Data Types
This section shows the data types used in a Pascal program.
Integer, real, Boolean and character types are referred as standard data types. Data types
can be categorized as scalar, pointer and structured data types. Examples of scalar data
types are integer, real, Boolean, character, subrange and enumerated. Structured data
types are made of the scalar types, for example, arrays, records, files and sets. We will
discuss the pointer data types later.
TUTORIALS POINT
Simply Easy Learning Page 12
Type Declarations:
The type declaration is used to declare the data type of an identifier. Syntax of type declaration
is:
For example, the following declaration defines the variables days and age as integer type, yes
and true as Boolean type, name and city as string type, fees and expenses as real type.
type
days, age = integer;
yes, true = boolean;
name, city = string;
fees, expenses = real;
Integer Types
Following table gives you detail about standard integer types with its storage sizes and value
ranges used in Object Pascal:
TUTORIALS POINT
Simply Easy Learning Page 13
Constants
Use of constants makes a program more readable and helps to keep special quantities at
one place in the beginning of the program. Pascal allows numerical, logical, string and
character constants. Constants can be declared in the declaration part of the program by
specifying the const declaration.
const
Identifier = contant_value;
VELOCITY_LIGHT = 3.0E=10;
PIE = 3.141592;
NAME = 'Stuart Little';
CHOICE = yes;
OPERATOR = '+';
Enumerated types
Enumerated data types are user-defined data types. They allow values to be specified in a
list. Only assignment operators and relational operators are permitted on enumerated
data type. Enumerated data types can be declared as follows:
type
enum-identifier = (item1, item2, item3, ... )
type
SUMMER = (April, May, June, July, September);
COLORS = (Red, Green, Blue, Yellow, Magenta, Cyan, Black, White);
TRANSPORT = (Bus, Train, Airplane, Ship);
The order in which the items are listed in the domain of an enumerated type, defines the
order of the items. For example, in the enumerated type SUMMER, April comes before
May; May comes before June, and so on. The domain of enumerated type identifiers
cannot consist of numeric or character constants.
TUTORIALS POINT
Simply Easy Learning Page 14
Subrange Types
Subrange types allow a variable to assume values that lie within a certain range. For
example, if the age of voters should lie between 18 to 100 years, a variable named age
could be declared as:
var
age: 18 ... 1000;
We will look at variable declaration in details in the next section. You can also define a
subrange type using the type declaration. Syntax for declaring a subrange type is as
follows:
type
subrange-identifier = lower-limit ... upper-limit;
const
P = 18;
Q = 90;
type
Number = 1 ... 100;
Value = P ... Q;
Subrange types can be created from a subset of an already defined enumerated type,
For example:
type
months = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
Summer = Apr ... Aug;
Winter = Oct ... Dec;
TUTORIALS POINT
Simply Easy Learning Page 15
6
CHAPTER
Variable Types
This section shows the variable types used in a Pascal program.
A variable is nothing but a name given to a storage area that our programs can
manipulate. Each variable in Pascal has a specific type, which determines the size and
layout of the variable's memory; the range of values that can be stored within that
memory; and the set of operations that can be applied to the variable.
The name of a variable can be composed of letters, digits, and the underscore character.
It must begin with either a letter or an underscore. Pascal is not case-sensitive, so
uppercase and lowercase letters mean same here. Based on the basic types explained in
previous chapter, there will be following basic variable types:
TUTORIALS POINT
Simply Easy Learning Page 16
Pascal programming language also allows defining various other types of variables which
we will cover in subsequent chapters like Pointer, Array, Records, Sets, and Files etc. For
this chapter, let us study only basic variable types.
var
variable_list : type;
Here, type must be a valid Pascal data type including character, integer, real, boolean, or
any user defined data type etc., and variable_list may consist of one or more identifier
names separated by commas. Some valid variable declarations are shown here:
var
age, weekdays : integer;
taxrate, net_income: real;
choice, isready: boolean;
initials, grade: char;
name, surname : string;
In the previous tutorial, we have discussed that Pascal allows declaring a type. A type can
be identified by a name or identifier. This type can be used to define variables of that
type. For example:
type
days, age = integer;
yes, true = boolean;
fees, expenses = real;
var
weekdays, holidays : days;
choice: yes;
student_name, emp_name : name;
capital: city;
cost: expenses;
Please note the difference between type declaration and var declaration. Type declaration
indicates the category or class of the types such as integer, real etc., whereas the variable
specification indicates the type of values a variable may take. You can compare type
declaration in Pascal with typedef in C. Most importantly, the variable name refers to the
memory location where the value of the variable is going to be stored. This is not so with
the type declaration.
TUTORIALS POINT
Simply Easy Learning Page 17
Variable Initialization in Pascal
Variables are assigned a value with a colon and the equal sign, followed by a constant
expression. The general form of assigning a value is:
variable_name := value;
By default, variables in Pascal are not initialized with zero. They may contain rubbish
values. So it is a better practice to initialize variables in a program. Variables can be
initialized (assigned an initial value) in their declaration. The initialization is followed by
the var keyword and the syntax of initialization is as follows:
var
variable_name : type = value;
Let us look at an example which makes use of various types of variables discussed so far:
program Greetings;
const
message = ' Welcome to the world of Pascal ';
type
name = string;
var
firstname, surname: name;
begin
writeln('Please enter your first name: ');
readln(firstname);
writeln('Please enter your surname: ');
readln(surname);
writeln;
writeln(message, ' ', firstname, ' ', surname);
end.
When the above code is compiled and executed, it produces following result:
TUTORIALS POINT
Simply Easy Learning Page 18
Enumerated Variables
You have seen how to use simple variable types like integer, real and boolean. Now let's see
variables of enumerated type which can be defined as:
var
var1, var2, ... : enum-identifier;
When you have declared an enumerated type, you can declare variables of that type. For
example:
type
months = (January, February, March, April, May, June, July, August,
September, October, November, December);
Var
m: months;
...
M := January;
program exEnumeration;
type
beverage = (coffee, tea, milk, water, coke, limejuice);
var
drink:beverage;
begin
writeln('Which drink do you want?');
writeln('You have ', sizeof(drink), ' choices');
end.
When the above code is compiled and executed, it produces following result:
Subrange Variables
Subrange variables are declared as:
var
subrange-name : lowerlim ... uperlim;
var
marks: 1 ... 100;
grade:
The 'A' ...
following 'E';
program illustrates the concept:
age: 1 ... 25;
TUTORIALS POINT
Simply Easy Learning Page 19
The following program illustrates the concept:
program exSubrange;
var
marks: 1 .. 100;
grade: 'A' .. 'E';
begin
writeln( 'Enter your marks(1 - 100): ');
readln(marks);
writeln( 'Enter your grade(A - E): ');
readln(grade);
writeln('Marks: ' , marks, ' Grade: ', grade);
end.
When the above code is compiled and executed, it produces following result:
var
weekdays, holidays : days;
choice: yes;
student_name, emp_name : name;
capital: city;
cost: expenses;
TUTORIALS POINT
Simply Easy Learning Page 20
7
CHAPTER
Constants
This section shows the constants used in a Pascal program.
Ordinal types
Set types
Pointer types (but the only allowed value is Nil).
Real types
Char
String
Declaring Constants
Syntax for declaring constants is as follows:
const
identifier = constant_value;
TUTORIALS POINT
Simply Easy Learning Page 21
String type constant president = 'Johnny Depp';
When the above code is compiled and executed, it produces following result:
Observe the formatting in the output statement of the program. The variable c is to be
formatted with total number of digits 7 and 2 digits after the decimal sign. Pascal allows
such output formatting with the numerical variables.
TUTORIALS POINT
Simply Easy Learning Page 22
8
CHAPTER
Operators
This section shows the operators used in a Pascal program.
Arithmetic operators
Relational operators
Boolean operators
Bit operators
Set operators
String operators
Let us discuss the arithmetic, relational, Boolean and bit operators one by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by Pascal. Assume variable A
holds 10 and variable B holds 20 then:
TUTORIALS POINT
Simply Easy Learning Page 23
The following example illustrates the arithmetic operators:
program calculator;
var
a,b,c : integer;
d: real;
begin
a:=21;
b:=10;
c := a + b;
writeln(' Line 1 - Value of c is ', c );
c := a - b;
writeln('Line 2 - Value of c is ', c );
c := a * b;
writeln('Line 3 - Value of c is ', c );
d := a / b;
writeln('Line 4 - Value of d is ', d:3:2 );
c := a mod b;
writeln('Line 5 - Value of c is ' , c );
c := a div b;
writeln('Line 6 - Value of c is ', c );
end.
Please note that Pascal is very strongly typed programming language, so it would give an
error if you try to store the results of a division in an integer type variable. When the
above code is compiled and executed, it produces following result:
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of d is 2.10
Line 5 - Value of c is 1
Line 6 - Value of c is 2
Relational Operators
Following table shows all the relational operators supported by Pascal. Assume variable A holds
10 and variable B holds 20 then:
TUTORIALS POINT
Simply Easy Learning Page 24
< Checks if the value of left operand is less (A < B) is true.
than the value of right operand, if yes then
condition becomes true.
Try following example to understand all the relational operators available in Pascal
programming language:
program showRelations;
var
a, b: integer;
begin
a := 21;
b := 10;
if a = b then
writeln('Line 1 - a is equal to b' )
else
writeln('Line 1 - a is not equal to b' );
if a < b then
writeln('Line 2 - a is less than b' )
else
writeln('Line 2 - a is not less than b' );
if a > b then
writeln('Line 3 - a is greater than b' )
else
writeln('Line 3 - a is greater than b' );
if a <= b then
writeln('Line 4 - a is either less than or equal to b' );
if ( b >= a ) then
writeln('Line 5 - b is either greater than or equal to ' );
end.
When the above code is compiled and executed, it produces following result:
TUTORIALS POINT
Simply Easy Learning Page 25
Boolean Operators
Following table shows all the Boolean operators supported by Pascal language. All these
operators work on Boolean operands and produces Boolean results. Assume variable A
holds true and variable B holds false then:
and then It is similar to the AND operator, however, (A and then B) is false.
it guarantees the order in which the
compiler evaluates the logical expression.
Left to right and the right operands are
evaluated only when necessary
program beLogical;
var
a, b: boolean;
begin
a := true;
b := false;
if (a and b) then
writeln('Line 1 - Condition is true' )
else
writeln('Line 1 - Condition is not true');
if (a or b) then
writeln('Line 2 - Condition is true' );
Bit Operators
Bitwise operators work on bits and perform bit by bit operation. All these operators work
on integer operands and produces integer results. The truth tables for bitwise and (&),
bitwise or (|), and bitwise not (~) are as follows:
p q p&q p!q ~p ~q
0 0 0 0 1 1
0 1 0 1 1 0
1 0 1 1 0 0
1 1 0 1 0 1
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
The Bitwise operators supported by Pascal are listed in the following table. Assume
variable A holds 60 and variable B holds 13 then:
& Binary AND Operator copies a bit to the (A & B) will give 12 which is
result if it exists in both operands. 0000 1100
TUTORIALS POINT
Simply Easy Learning Page 27
! Binary OR Operator copies a bit if it exists A ! B) will give 61 which is
in either operand. 0011 1101
<< Binary Left Shift Operator. The left A << 2 will give 240 which is
operands value is moved left by the 1111 0000
number of bits specified by the right
operand.
>> Binary Right Shift Operator. The left A >> 2 will give 15 which is
operands value is moved right by the 0000 1111
number of bits specified by the right
operand.
Please note that different implementations of Pascal differ in bitwise operators. Free
Pascal, the compiler we used here, however, supports the following bitwise operators:
Operators Operations
or Bitwise OR
TUTORIALS POINT
Simply Easy Learning Page 28
program beBitwise;
var
a, b, c: integer;
begin
a := 60; (* 60 = 0011 1100 *)
b := 13; (* 13 = 0000 1101 *)
c := 0;
c := a or b; (* 61 = 0011 1101 *)
writeln('Line 2 - Value of c is ', c );
When the above code is compiled and executed, it produces following result:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is -61
Line 4 - Value of c is 240
Line 5 - Value of c is 15
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher
precedence than + so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the table, those with the
lowest appear at the bottom. Within an expression, higher precedence operators will be
evaluated first.
Operators Precedence
~, not, Highest
|, !, +, -, or,
TUTORIALS POINT
Simply Easy Learning Page 29
=, <>, <, <=, >, >=, in
Try the following example to understand the operator precedence available in Pascal:
program opPrecedence;
var
a, b, c, d : integer;
e: real;
begin
a := 20;
b := 10;
c := 15;
d := 5;
e := (a + b) * c / d; (* ( 30 * 15 ) / 5 *)
writeln('Value of (a + b) * c / d is : ', e:3:1 );
e := ((a + b) * c) / d; (* (30 * 15 ) / 5 *)
writeln('Value of ((a + b) * c) / d is : ' , e:3:1 );
e := a + (b * c) / d; (* 20 + (150/5) *)
writeln('Value of a + (b * c) / d is : ' , e:3:1 );
end.
When the above code is compiled and executed, it produces following result:
Value of (a + b) * c / d is : 90.0
Value of ((a + b) * c) / d is : 90.0
Value of (a + b) * (c / d) is : 90.0
Value of a + (b * c) / d is : 50.0
TUTORIALS POINT
Simply Easy Learning Page 30