You are on page 1of 22

Justin Ramsepaul

Pascal Crash Course

Chapter 0: Getting Started


The Pascal programming language was named in honour of the French mathematician Blaise Pascal. It is
a teaching language since its syntax is easily understood and programming methodologies can be easily
expressed using the language. Pascal is a compiler language that is of the 3rd generation of programming
languages making it easily understood and somewhat powerful.

Since it is a compiler language, we will need a compiler in order to compile and link the source code to
libraries to create binaries.

Pascal Compilers
There are many pascal compilers. These are some honourable mentions:

Bloodshed Dev-Pascal: A very simple (compared to others), minimalistic but powerful Pascal compiler.
May encounter errors on newer operating systems due to the projects development being halted.

Lazarus IDE: Lazarus is an IDE which is a little complicated but can get the job done with almost no
trouble.

Coding Grounds Pascal Compiler: An online compiler that allows you to write and compiler Pascal code
online without any pre-installed software. Handy when downloading an IDE is too much trouble.

Chapter 1: Introduction to Pascal


Reserved words
These are the words that make up the syntax of the Pascal language

Program Defines the program


Var Defines any variables to be used
Integer Defines a variable to be a whole number
Real Defines the variable to be a number with a
decimal
Char Defines the variable to be one character
String Defines the variable to be text
Array Defines the variable to be an array of data
Const Defines a value that will not be changed
throughout the program
Begin Begin a section of code
End; Ends a section of code
End. Ends a program.
If Then Else Not And Or Boolean commands
While Do Loop statement
For Do Loop statement
Writeln Prints a line on the screen
Readln Reads an input
:= Assigns a value to a variable

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

= Equals to
> More than
< Less than
<> Not equal to
:
; Denotes an executable line
DIV Divides two numbers returning an integer instead
of a real value.
Array Defines an array.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 2: Basic Structure of a Pascal Program


All Pascal programs follow this basic structure. The first line must be a statement that declares the name
of the program. This is the identifier for the program. All identifiers (a sequence of characters used to
identify or refer to a program or an element) cannot have spaces within them and therefore must use
underscores for spaces or no space.

1. Program MyProgram;

Or

2. Program My_Program;

The actual executable code (code that will be run within the program) must be between a Begin and
End. This is where the main code or any procedures that must be executed are written.

1. Program Pascal;
2.
3. Begin
4. {EXECUTABLE CODE}
5. End.

There must be a begin and end. in the program which denotes the main program. The last end must
have a period at the end of it : end. This tells the compiler that the program is finished and there are
no more executable lines beyond it. All begin statements must be paired with an end statement. There
can be multiple begin-end statements within a program denoting different sections of executable code.
Each begin-end that is within the main program must have its end followed by a semi-colon instead of
a full stop : end; This semicolon indicates that the program has not ended and there are more
executable lines of code.

Correct use of Begin-End

1. Program Pascal;
2.
3. Begin
4. {EXECUTABLE CODE}
5. Begin
6. {EXECUTABLE CODE}
7. End;
8. {EXECUTABLE CODE}
9. End.

Incorrect use of Begin-End

1. Program Pascal;
2.
3. Begin
4. {EXECUTABLE CODE}
5. Begin
6. {EXECUTABLE CODE}

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course
7. End.
8. {EXECUTABLE CODE}
9. End.

Chapter 3: Variables and Constants


In Pascal we can assign variables and constants. Variables are identifiers that can have their values
changed while constants are identifiers that cannot have their values changed. We refer to the stating of
these variables with their types as declaring. Any variable must have a unique name along with its data
type (the type of data that is to be stored within that variable example: numbers, characters etc.). The
declaring of these variables and constants must be done after the declaration of the program on the first
line and before the begin of the main program. When declaring the programs name, keep in mind that
that name cannot be used as a variable anymore. Spaces cannot be used in variable or constant
identfiers.

1. Program MyProgram;
2. Var
3. (DATA)
4. Const
5. (DATA)
6. Begin
7. *EXECUTABLE CODE*
8. End.

Var is used to declare a list of variables and Const is used to declare a list of constants. This formatting
can also be used safely:

1. Program MyProgram;
2. Const
3. (DATA)
4. Var
5. (DATA)
6. Begin
7. *EXECUTABLE CODE*
8. End.

The process of declaration refers to the defining of constants and variables within the program.
Variables are declared by specifying the name of the variable and the datatype while constants are
declared by specifying the name of the constant and the value that is assigned to the constant.

Use of Constants.
A constant is a set value that does not change in the program. A constant can be set in the code of the
program but cannot be changed when the program is compiled and run.

1. Const
2. X = 21;

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course
3. Y = A;
4. Z = John;

Const is the reserve word used to specify that the following code declares constants within the program.
The identifier of the constant followed by an equal sign and the value ended by a semicolon is used to
define a constant.

1. CONSTANT_IDENTIFIER = CONSTANT_VALUE;

Use of Variables.
Variables are values within a program that can be changed after the program is compiled and run. Var is
the reserve word used to specify that the following code declares variables.

2. Var
3. X : integer;
4. Y : char;
5. Z : real;

The variables identifier needs to be followed by a colon and the type of data to be used in the variable
ended by a semicolon.

1. VARIABLE_IDENTIFIER : DATATYPE;

Local and Global Variables and Constants


Global variables and constants are those that are accessible throughout the entire program without
restriction. These must be declared after the declaration of the program and can be accessed by
procedures in the program.

Example:

1. Program MyProgram;
2. Const
3. (DATA)
4. Var
5. (DATA)
6. Begin
7. *EXECUTABLE CODE*
8. End.

The highlighted code declares globally accessible variables and constants.

Local variables and constants are those that are accessible only within the subroutine of the program
that they are declared in. These cannot be accessed by other subroutines or anywhere else in the
program.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Example:

1. Program test;
2.
3.
4. Procedure addone;
5. Var //Declaring variable within the subroutine
6. i:integer;
7. Const //Declaring constant within the subroutine
8. a=1;
9. Begin
10. i:= i+a;
11. End;
12. Begin //Main Program
13. addone; //Calling procedure addone
14. end.

These variables and constants are only accessible within the procedure.

Data Types and Sizes

Integer

- Contains integer values (whole numbers with no decimal values) from -32768 to 32767
- - 16bit
- Initialization:
:integer;
Example

1. X :integer;

Longint

- -Long Integer
- -Contains integer values (whole numbers with no decimal values) from -2,147,483,648 to
2,147,487,647
- - 32bit
- Initialization
:longint;
Example

1. Y:longint;

Real

- -Holds decimal numbers in exponential form

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

- - Range from 2.9 x 10 E-39 to 1.7 x 10 E+38


- -To print a real value in non-exponential form:
1 Writeln(X:5:2)
X is the variable
:5:2 denotes that the value should be displayed 00000.00
5 spaced before the decimal and 2 spaces after.
- -Initialization
1 :real;
2 Z :real;

Character

- Holds a single character


- :char;
1 Gender:char;

Boolean

- Holds either true or false

:boolean;

1. car:boolean;

Chapter 4: Calculations In Pascal


Pascal can do basic to moderately complex calculations. All rules of maths apply to these calculations
but calculations need to be in the simplest form possible.

Examples:

1. x:=1+1;

x is the variable (integer) and the assignment statement is used before the equation.

A semicolon must be present after the equation.

The variable must be of the datatype that the equations answer is. If the equation returns a decimal
value, then x needs to be declared as real in the variables section of the program.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Example:

1. x:=(1+1)/0.75;

Operations can be done on variables within a calculation.

Examples:

1. Program MyProgram;
2. Var
3. X,Y : integer;
4. Z : real;
5. Begin
6. Y:=45;
7. X:=23 + Y;
8. Z:=Y / X;
9. End.

Calculations can also be done within Writeln statements.

Example:

1. Program MyProgram;
2. Var
3. X,Y : integer;
4. Z : real;
5. Begin
6. Y:=45;
7. X:=23 + Y;
8. Writeln("Z is equal to", Y / X);
9. End.

The Writeln statement can output what Z would have been but will not save the value to Z.

DIV Calculations
Sometimes we may need to divide two numbers that may give a real value but we may want an integer.
This is where DIV comes in. DIV replaces / in a calculation to return an integer rather than a real value.

Using the regular divide sign:

1. Z:= X / Y;

The result will be a real value with a decimal point.

1. Z:= X DIV Y;

The result will be an integer rounded off based on a real value.

This is especially helpful if we want a rounded off value instead of a real value. This can be used to solve
division calculations where the result is stored in a variable declared as an integer.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 5: Writeln and Readln usage


Writeln and Readln are used to output and input data into the program.

1. Program MyProgram;
2. Var
3. X,Y : integer;
4. Begin
5. Writeln("INPUT X");
6. Readln(X);
7. End.

Writeln writes a line to the screen. Text between the can be replaced. Any text between the
quotations are not executed within the program. Writeln is short for write line.

Anything that isnt a designated variable/constant needs to be in quotation marks. If a variable/constant


needs to be printed, this syntax is used

1. Writeln("Value X is", X);

, (an unquote then a comma) is used to end the text and print a variable/constant.

1. Writeln("Value X is", X, "Now input Y");

If there is more text to be printed the above syntax can be used. , (Comma and Quote) can be used to
begin another set of text.

Calculations can be done within the Writeln command.

1. Writeln("Z is equal to", X + Y);

That prints the added values of X and Y.

1. Readln(X);

Readln reads anything that is typed and stores that data in a variable. The variable does not need to be
in quotes. If the input does not correspond with the datatype of that variable (For example: Inputting a
string into a variable that is declared as a number) the program will instantly crash. Readln is short for
read line.

More than one variable can be input at the same time.

1. readln(a,b);

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Readln is used and within the parentheses the variables are separated by a comma. What happens is the
program prompts for an input and reads the first input, then prompts for the second input on another
line and reads that input.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 6: IF-THEN-ELSE Usage

1. Program MyProgram;
2. Var
3. X,Y : integer;
4. Begin
5. Writeln("INPUT X");
6. Readln(X);
7. Writeln("Input Y");
8. Readln(Y);
9. If X > Y
10. Then Writeln("X is more")
11. Else Writeln("Y is more");
12. End.

Line 9: The If statement to make the decision of TRUE or FALSE does not require a semicolon.

Line 10: Then is executed when the statement is true. Only one line of code can be executed without a
BEGIN and END; If multiple lines of code needs to be executed then the syntax is changed to this.

9. If X > Y
10. Then
11. Begin
12. Writeln("X is more");
13. Writeln("Y is less");
14. End;

Line 11: Else is executed when the statement is false. Same rule of single lines and multiple lines of code
that need to be executed apply.

11. Else
12. Begin
13. Writeln(X is Less);
14. Writeln(Y is more);
15. End;

Using Criteria
Before we can make decisions in Pascal, we need to learn how criteria is stated. For a single criterion,
this is used:

1. if a < b

The variables are compared without any brackets.

There are three comparisons that can be done.

More than - >

1. if a > b

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Less than - <

1. if a < b

Equals to - =

1. if a = b

Not equals to - <>

1. if a <> b

Comparing to numbers and strings

Using Multiple Criteria


Sometimes we may need to check multiple criteria in order to make a proper decision. This is especially
useful in validation. Example: To determine if a student has received a B grade, we may need to check
if that student has both above 80% but less than 90%.

The operators AND as well as OR can be used to make check multiple criteria before Pascal makes a
decision.

1. If (X > Y) AND (Y > X)

The conditions are put into brackets and the operator is put in the middle of both.

If the conditions need a string or a character the syntax, the character must be in single quotes while the
string must be in double quotes.

1. If (X = 'M') OR (Y = "YES")

Truth Tables
Pascal uses truth tables to validate multiple criteria.

OR Truth Table
OR T F

T T T

F T F

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

AND Truth Table


AND T F

T T F

F F T

NOR Truth Table


NOR T F

T F F

F F T

Nested If statements

1. If Y > X
2. Then
3. If X > Y
4. Then Writeln("Infinite loop");

The same rules of IF THEN ELSE apply to Nested Ifs

1. If Y > X
2. Then
3. Y := Y + X
4. Else
5. If X > Z
6. Then
7. Writeln("X is more than Z");

Nested IF statements are statements that nest different levels of criteria using IF statements within IF
statements. The example above shows two levels of nesting where another criteria is checked( X > Z) on
line 5, is checked only if (Y > X) on line 1 is false. There is no limit to the amount of nesting that can be
done but it will get very complicated past 3 levels.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 7: While Do Loop


While Do loops are usually used to do validation. The criteria is checked before any code is executed.
Basic syntax:

1. Program MyProgram;
2. Var
3. X,Y : integer;
4. Begin
5. Writeln('INPUT X');
6. Readln(X);
7. While X > Y do
8. Begin
9. Writeln('Hello');
10. Writeln('World');
11. end;

While do keeps checking the condition to see if it is true or false and will only continue if the condition is
false. If the condition is true it will repeat the section of code between the begin and end.

Line 7: Does not require a semicolon at the end of the line

Must have a Begin and End; to denote the section of code to be executed when the condition is true or
false.

Same rules as IF THEN ELSE applies to WHILE DO

For example the multiple conditions

1. While (X = 'M') OR (Y = 'YES') do

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 8: For Loop


The For loop is used to loop the same section of code multiple times.

Basic syntax

1. Program MyProgram;
2. Var
3. i,X,Y : integer;
4. Begin
5. For i := 1 to 10 do
6. Begin
7. Writeln('Are we there yet?');
8. End;
9. End.

i must be initialized as an integer in variables. i in this case is called the loop counter. The loop counter
can be any variable of your choice. The loop counter usually starts at 1 and then the counter number is
specified after the to. Do must be placed after the counter number.

Same rules apply as the IF THEN ELSE and WHILE DO

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 9: Repeat Until Loops


Repeat until loops repeat a section of code until a criterion is met. This is the only control loop that
checks the criteria after the section of code is executed. This means that it may not be very suitable for
validation since it executes the code before the criteria is checked.

Basic syntax:

1. Program MyProgram;
2. Var
3. x:integer;
4. Begin
5. Writeln('Repeat loop');
6. repeat
7. x:=x+1;
8. Writeln('Inside loop');
9. until x > 10;
10. end.

The section of code that is being repeated does not need a begin and end container. The REPEAT and
UNTIL acts as the container. The text in yellow will repeat since it is between the REPEAT and UNTIL.

Same rules of multiple criteria apply here.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Chapter 10: Arrays


An array in Pascal can be defined a series of objects all of which are the same size and type.

How arrays in Pascal are declared:

1. Program My_Program;
2. Var
3. myarray:array[1..5] of integer;

Lets break it down:

4. myarray:array[1..5] of integer;

myarray is the arrays identifier.

array[1..5] is the way we specify that the variable is an array. The square brackets determines the size
of the array. Arrays usually start at 1 hence the 1 in the declaration. Two periods are places after the
starting number of the array then the last number of the arrays size is places at the end. Therefore
array[1..5] will yield an array the size of 5 elements.

of integer Is the way we declare what type of data is stored in the array. Arrays can be of only a single
datatype and therefore datatypes cannot mix and match in the array.

Array terminology:

Element: An element in an array is an object in an array.

Index: In arrays, we give each element a unique index number with allows the easy locating of an object.

Example:

1. myarray:array[1..5] of char;

myarray

1 2 3 4 5
E J A M S

Interacting with Arrays


In order to interact with elements within an array, we need the arrays identifier and the index of the
element we want to interact with.

Array indexes are exclusively used to interact with elements.

Example:

Store the character B in the 3rd element in the array.

3rd element in this case applies the index where the data value needs to be stored is 3.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course
1. Myarray[3]:=b;

Myarray is the identifier of the array we want to interact with.

[3] is the index of the element in the array we want to interact with.

:= an assignment statement is used since we want to assign a value to this space in the array.

This would effectively change the array to this:

1 2 3 4 5
E J b M S
Where the object in index 3 has changed.

Addressing elements in arrays:

Identifier[index]

Where the identifier is the arrays name and index is the location in the array.

Filling Data into Arrays


To fill data into an array, a FOR LOOP is used provided that the number of inputs are known in order to
declare the arrays size. The arrays size and the FOR LOOPs counter must be the same.

Example:

1. Program Test;
2. Var
3. Myarray:array[1..10] of integer;
4. i:=integer;
5. begin
6. For i:=1 to 10 do
7. begin
8. Writeln('Please input', i);
9. Readln(Myarray[i]);
10. end;
11. end.

In this example, an array identified as Myarray was declared to be an array of 10 containing integers
(Line 3) and i was declared to be an integer (Line 4) since it is used in the FOR LOOP. The FOR LOOPs
counter was set to 10(Line 6), the same size as the declared array. On line 9, the index number of the
array was set to i. This allows the arrays index to change according to the position in the FOR LOOP;
when the loop is on its 3rd loop, the index of the array would be 3 and so on. This method is used to fill
an array when the number of inputs are known.

A good programming practice would be to use a constant to control the loop counter when multiple
loops are being used to control one array. This has the advantage of allowing the number of times the
loop executes to be changed accordingly to how big the array is by changing the value of the constant.

Example:

1. Program Test;
2. Var

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course
3. Myarray:array[1..10] of integer;
4. i:=integer;
5. Const
6. N=10;
7. begin
8. For i:=1 to N do
9. begin
10. Writeln('Please input', i);
11. Readln(Myarray[i]);
12. end;
13. For i:=1 to N do
14. begin
15. Writeln('Please change the value of', i);
16. Readln(Myarray[i]);
17. end;
18.
19. end.

In the above example the constant N was set to 10 and used as the loop counter for each loop within
the program. This has the advantage of allowing the programmer to easily change the number of times
the loop executes by changing the value of the constant.

Chapter 11: Procedures


Procedures are subprograms within a program that allow for a section of code to be executed by calling
the procedure anywhere in the program. Procedures are used to reduce repetition of code by assigning
a name to that section of code and calling the code in the main program. Procedures can be used
numerous times without limit throughout the main program. Procedures are declared before the main
begin and end of a program in the same code space where variables and constants are declared.

Declaring a Procedure

1. Program [PROGRAM_IDENTIFIER;
2. Procedure [PROCEDURE_IDENTFIER];
3. begin
4. [CODE TO BE EXECUTED IN PROCEDURE]
5. end;
6. begin
7. [CODE FOR MAIN PROGRAM]
8.
9. end.

Procedures are declared by Procedure followed by the procedures identifier. A begin and end; are used
to encapsulate the code to be executed when the procedure is called.

1. Procedure [PROCEDURE_IDENTFIER];
2. begin
3. [CODE TO BE EXECUTED IN PROCEDURE]
4. end;

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

Calling a Procedure.
Procedures can be called in the main program by specifying the identifier of the procedure followed by a
semicolon.

Example:

1. Program test;
2. Var
3. i:integer;
4.
5. Procedure addone;
6. begin
7. i:=i+1;
8. end;
9. begin
10. addone;
11. end.

The begin and end on lines 9 and 11 respectively denote the beginning and ending of the main program
or section of code. Within this section of code, procedures can be called. This is shown in line 10 as the
procedure addone was called in the program. When addone is called, the code that is encapsulated in
the addone procedure is executed.

Chapter 11: Programming Concepts


Programming concepts are predefined solutions for small problems that can be used in a program.

1. Accepting input
For an input to be accepted in a program, the user must first be prompted as to what input needs to be
entered.

Example: Prompting the user to input a number that is less than 5

1. Program five;
2. Var
3. x: integer;
4. Begin
5. Writeln("Please enter a number less than five."); //Prompts user for input
6. Readln(x); //Accepts input
7. End.

For all inputs, the user must be prompted to enter the input with instructions on what input is required.

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course

2. Validating Input
Input validation is used to ensure that the input that is entered is correctly and meets the
requirements of the program. For example, if an input of a students mark which ranges from 1 to 100
was prompted but the user accidently enters the mark as 156, then the input is wrong therefore the
output would also be wrong. Validation are those software checks that ensure that the data entered is
correct and free from any errors.

Validation can be done by prompting the user to first enter the input, then checking the input to
ensure it meets the requirements. If the check fails, then the user is prompted to re-enter the input and
the input is rechecked to ensure it meets requirements.

1. Program studentmarks;
2. Var
3. stdmark:integer; //Stores the mark of the student
4. Begin
5. Writeln("Please enter the mark of the student."); //Prompts the user for input
6. Readln(stdmark); //Accepts the input
7. While stdmark => 100 do // Checks the mark to ensure it is not over 100
8. Begin //Begins the section of code to be executed if the mark is over 100
9. Writeln("Please re-
enter the mark of the student"); // Prompts the user to re-enter the mark
10. Readln(stdmark);
11. End; //When the section of code is executed, the mark is re-checked at line
7 to ensure it is correct
12. Writeln("The student's percentage is", stdmark/100);
13. End.

The validation starts in a WHILE DO loop which checks the variable to be validated to determine if it is
out of the acceptable range for that input. If the check determines that the input is not valid, it executes
the code within the WHILE DO statement. This code prompts the user to re-enter the input and the
program replaces the input that was previously entered. After the section of code is executed, the
WHILE DO statements checks the input again to ensure it is valid. If the input is valid, then the program
continues. When the input is invalid, the validation code is executed again and the process repeats until
the input is valid.

3. Inputting Data into an Array


As mentioned in the chapter relating to arrays, loops are used to input data into arrays. It is done by
allowing the variable that controls the number of time the loop is executed to control the position of the
element in the array that is being accessed. Usually a FOR loop is used to input data into an array when
the array size is known.

Example: Save the marks of 10 students into an array called studentmark.

1. Program Test;
2. Var
3. studentmark:array[1..10] of integer; //Declares the array
4. i:integer; //Declares the loop counter
5. Begin
6. For i:= 1 to 10 do //FOR loop set to execute for 10 loops. i is used to store the
position in the loop

DRAFT 34.9 26TH MAY 2016 9:00AM


Justin Ramsepaul
Pascal Crash Course
7. Begin
8. Writeln('Please enter the mark of student ',i); //Prompts the user to input
the mark of the student
9. Readln(studentmark[i]); //Reads the mark of the student and inputs it into
the studentmark array at the "i" position
10. End;
11. End.

In the above example, studentmark was declared as the array to store the students mark and i was
declared as the loop counter. On line 6, a FOR loop was used and set to execute the same number of
times as the length of the array. On line 8, the user was prompted to input the mark of the student in
the ith position in the array. On line 9, the input was read into the ith position within the array. This loop
would execute until all positions in the array was filled.

DRAFT 34.9 26TH MAY 2016 9:00AM

You might also like