Вы находитесь на странице: 1из 25

Chapter 4

Introduction to Pascal
Programming Language

Learning Outcomes
Upon completion of this lecture, learners will
be able to:
Identify basic Pascal program structure
Recognise important Pascal tokens
Grasp the basis concept of identifiers, constants,
datatypes and variables

Page 2 of 23

Outline

What is Pascal Programming Language?


Pascal Program Structure
Comments
Reserved words
Identifiers
Constants
Data types
variables
Page 3 of 23

What is Pascal Programming


Language?
A high level procedural programming
language
Developed by Nicklaus Wirth in 1970
named in honor of the French mathematician
and philosopher Blaise Pascal
NOT a case sensitive programming
language
Page 4 of 23

Pascal Program Structure


Head section

program <program_name>;

Declaration section

var
(* variable declaration
section *)

Body section

Begin
(* program statements *)
End.

Page 5 of 23

Sample Pascal Program

Page 6 of 23

Pascal Program Structure

Errors in program
Syntax error

violation of syntactic rules of the language, detect during


compile time

Run time error


program is syntactically correct, but causes abnormal
program termination during run time, detect during run time
eg: use a variable before assigning a value,
division by 0

Logic error
incorrect translation of either the problem statement or the
algorithm which produce incorrect result
Page 7 of 23

Comments
For the programmer to explain pieces of
code
Compiler will ignore the comments
Comments are discarded by the compiler during
compilation process
Comments are not translated into machine code

Page 8 of 23

..Comments

Page 9 of 23

..Comments
Syntax

(* <comments> *)
{ <comments> }
// <comments>

Example

(* my comments *)
{ my comments }
// my comments
(*********************************
input : radius
process : peri = 2 * PI * radius
output : peri
**********************************)
Page 10 of 23

..Reserved Words

Page 11 of 23

Reserved Words
Belong to Pascal programming language
Mean something to the compiler
Cannot be redefined by programmer

Page 12 of 23

..Reserved Words
Reserved words in free Pascal

Page 13 of 23

Identifiers
Identifiers are all programmer defined names
in program i.e
Constants
Variables
Programs
Functions
Procedures etc.

Reserved words are not identifiers

Page 14 of 23

..Identifiers

Page 15 of 23

..Identifiers
Naming rules
Must begin with a letter (a..z or A..Z) or an
underscore ( _ )
Can contain letters, digits or underscore
Can be up to 127 characters

Not case sensitive

Page 16 of 23

..Identifiers
Valid identifiers
num
num1
number_one
_NumberOne
nUmBeRoNe_

Invalid identifiers
1num
Num1&2
number one
Number:One
nUmBeRoNe_$

Page 17 of 23

Constants

Page 18 of 23

..Constants
Unlike variable, the value assigned to a constant
cannot be changed
Advisable to write constants identifier in
UPPERCASE
Syntax

const <constant name> = <value>;

Example

const PI = 3.142;

Page 19 of 23

Data Types
Each variable has a data type
Data Type
ShortInt

Size
Whole number -128 to 127

Integer

Whole number -32768 to 32767

LongInt

Whole number -2,147,483,648 to


2,147,487,647

Real

Floating point number 1E-38 to


1E+38 platform dependent,7 digit. 4 byte

Double

Floating point number up to 15


Digits. 8 byte

Boolean

True / False

Character

Holds a single character

String

Holds text

Page 20 of 23

Variables

Name of a memory locations


Each variable has a data type
A variable can hold only a value at one time
A new value assigned to a variable will
replace the existing value

Page 21 of 23

..Variables

Page 22 of 23

Variable Declaration
Each variable must be declared before it can
be used
Syntax

var <variable name> : <data type>;

Example

var my_input : integer;


var
width : real;
height : real;
hour : integer;

Page 23 of 23

..Variable Declaration
Declare more than one variable of the same
data type
Syntax

var <variable name1>,


<variable name2> : <data type>;

Example

var
width,
height : real;
hour
: integer;

Use comma to separate variables of the


same data type
Page 24 of 23

Summary
Basic Pascal program structure comprises
head, declaration and body sections
Important tokens in Pascal include
identifiers, constants, variables and reserved
words
Programmer must follow the syntax rules of
Pascal programming language

Page 25 of 23

Вам также может понравиться