Professional Documents
Culture Documents
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.
= 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.
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.
1. Program Pascal;
2.
3. Begin
4. {EXECUTABLE CODE}
5. Begin
6. {EXECUTABLE CODE}
7. End;
8. {EXECUTABLE CODE}
9. End.
1. Program Pascal;
2.
3. Begin
4. {EXECUTABLE CODE}
5. Begin
6. {EXECUTABLE CODE}
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;
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;
Example:
1. Program MyProgram;
2. Const
3. (DATA)
4. Var
5. (DATA)
6. Begin
7. *EXECUTABLE CODE*
8. End.
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.
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.
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
Character
Boolean
:boolean;
1. car:boolean;
Examples:
1. x:=1+1;
x is the variable (integer) and the assignment statement is used before 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.
Example:
1. x:=(1+1)/0.75;
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.
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.
1. Z:= X / Y;
1. Z:= X DIV Y;
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.
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.
, (an unquote then a comma) is used to end the text and print a variable/constant.
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.
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.
1. readln(a,b);
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.
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
1. if a > b
1. if a < b
Equals to - =
1. if a = b
1. if a <> b
The operators AND as well as OR can be used to make check multiple criteria before Pascal makes a
decision.
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
T T F
F F T
T F F
F F T
Nested If statements
1. If Y > X
2. Then
3. If X > Y
4. Then Writeln("Infinite loop");
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.
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.
Must have a Begin and End; to denote the section of code to be executed when the condition is true or
false.
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.
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.
1. Program My_Program;
2. Var
3. myarray:array[1..5] of integer;
4. myarray:array[1..5] of integer;
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:
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
Example:
3rd element in this case applies the index where the data value needs to be stored is 3.
[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.
1 2 3 4 5
E J b M S
Where the object in index 3 has changed.
Identifier[index]
Where the identifier is the arrays name and index is the location in the array.
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
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.
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;
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.
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.
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.
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.
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
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.