Course S2-01 - Pascal
Course S2-01 - Pascal
2024-2025
1
A. GHALI
Computer Science 2024/2025
Pascal - Overview
Pascal is a general-purpose, high-level language that was originally developed by Niklaus Wirth
in the early 1970s. It was developed for teaching programming as a systematic discipline and to
develop reliable and efficient programs. 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.
Niklaus Wirth
Niklaus Wirth in 1984 with Lilith, one of the world’s first computer workstations to have a high-
resolution graphic display and use a mouse, which Dr. Wirth invented.
2
A. GHALI
Computer Science 2024/2025
• Program name
• Uses command
• Type declarations
• Constant declarations
• Variables declarations
• Functions declarations
• Procedures declarations
• Main program block
• Statements and Expressions within each block
• Comments
Every pascal program generally has a heading statement, a declaration and an execution part
strictly in that order. Following format shows the basic syntax for a Pascal program –
3
A. GHALI
Computer Science 2024/2025
Following is a simple pascal code that would print the words "Hello, World!" –
program HelloWorld;
(* Here the main program block starts *)
begin
writeln('Hello, World!');
end.
This will produce following result −
Hello, World!
• The first line of the program program HelloWorld; indicates the name of the program.
• 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 statement.
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 last statement end. ends your program.
Variables
A variable definition is put in a block beginning with a var keyword, followed by definitions of the
variables as follows:
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
4
A. GHALI
Computer Science 2024/2025
procedure/function and before the begin keyword. For global variables, they are defined after
the program header.
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.
Comments
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 sensitive language, which means you cannot write your variables, functions and
procedure in either case. Like variables A_Variable, a_variable and A_VARIABLE have not the
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 −
The statements in Pascal are designed with some specific Pascal words, which are called the
reserved words. For example, the words, program, input, output, var, real, begin, readline,
writeline and end are all reserved words.
Following is a list of reserved words available in Pascal.
5
A. GHALI
Computer Science 2024/2025
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 −
6
A. GHALI
Computer Science 2024/2025
Integer Types
Following table gives you details about standard integer types with its storage sizes and value
ranges used in Object Pascal −
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.
VELOCITY_LIGHT = 3.0E=10;
PIE = 3.141592;
NAME = 'Stuart Little';
CHOICE = yes;
OPERATOR = '+';
7
A. GHALI
Computer Science 2024/2025
Pascal - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. Pascal allows the following types of operators −
• 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. We will discuss
the set operators and string operations later.
Arithmetic Operators
Following table shows all the arithmetic operators supported by Pascal. Assume variable A holds
10 and variable B holds 20, then −
Show Examples
8
A. GHALI
Computer Science 2024/2025
Relational Operators
Following table shows all the relational operators supported by Pascal. Assume variable A holds
10 and variable B holds 20, then −
Show Examples
9
A. GHALI
Computer Science 2024/2025
Boolean Operators
Following table shows all the Boolean operators supported by Pascal language. All these
operators work on Boolean operands and produce Boolean results. Assume variable A holds
true and variable B holds false, then −
Show Examples
10
A. GHALI
Computer Science 2024/2025
Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.
Following is the general form of a typical decision-making structure found in most of the
programming languages −
11
A. GHALI
Computer Science 2024/2025
Pascal programming language provides the following types of decision-making statements. Click
the following links to check their detail.
12
A. GHALI
Computer Science 2024/2025
You have observed that if-then-else statements enable us to implement multiple decisions in a
program. This can also be achieved using the case statement in simpler way.
Syntax
case (expression) of
L1 : S1;
L2: S2;
...
...
Ln: Sn;
end;
Where, L1, L2... are case labels or input values, which could be integers, characters, boolean or
enumerated data items. S1, S2, ... are Pascal statements, each of these statements may have one
or more than one case label associated with it. The expression is called the case selector or the
case index. The case index may assume values that correspond to the case labels.
The case statement must always have an end statement associated with it.
• The expression used in a case statement must have an integral or enumerated type or be
of a class type in which the class has a single conversion function to an integral or
enumerated type.
• You can have any number of case statements within a case. Each case is followed by the
value to be compared to and a colon.
• The case label for a case must be the same data type as the expression in the case
statement, and it must be a constant or a literal.
• The compiler will evaluate the case expression. If one of the case label's value matches
the value of the expression, the statement that follows this label is executed. After that,
the program continues after the final end.
• If none of the case label matches the expression value, the statement list after the else or
otherwise keyword is executed. This can be an empty statement list. If no else part is
present and no case constant matches the expression value, program flow continues after
the final end.
• The case statements can be compound statements (i.e., a Begin ... End block).
13
A. GHALI
Computer Science 2024/2025
Flow Diagram
program checkCase;
var
grade: char;
begin
grade := 'A';
case (grade) of
'A' : writeln('Excellent!' );
'B', 'C': writeln('Well done' );
'D' : writeln('You passed' );
'F' : writeln('Better try again' );
end;
Excellent!
Your grade is A
14
A. GHALI
Computer Science 2024/2025
Pascal - Loops
There may be a situation, when you need to execute a block of code several number of times. In
general, statements are executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and
following is the general form of a loop statement in most of the programming languages –
15
A. GHALI
Computer Science 2024/2025
Pascal programming language provides the following types of loop constructs to handle looping
requirements. Click the following links to check their details.
Loop control statements change execution from its normal sequence. When execution leaves a
scope, all automatic objects that were created in that scope are destroyed.
Pascal supports the following control statements. Click the following links to check their details.
16
A. GHALI