Welcome to this comprehensive introductory course on the **Pascal** programming language. Pascal was deliberately designed as a teaching language, and it remai…
For University and College Students (First-Year Computer Science / Programming)
Author: Prepared for educational use
Last Updated: 2026
Dialect Focus: ISO Pascal with Free Pascal / Object Pascal extensions (widely used in modern education)
Table of Contents
- Introduction and Learning Objectives
- History and Origins of Pascal
- Setting Up Your Development Environment
- Basic Program Structure
- Fundamental Data Types
- Variables, Constants, and Literals
- Operators and Expressions
- Input and Output
- Control Structures
- Procedures and Functions
- Arrays
- Records (Structured Types)
- Sets
- Files (Text and Binary)
- Pointers and Dynamic Memory Management
- Units and Modular Programming
- Object-Oriented Extensions in Modern Pascal
- Exception Handling
- Standard Libraries and Common Units
- Debugging Practices and Common Errors
- Best Practices and Coding Style
- Comprehensive Exercises and Mini-Projects
- Appendix: Quick Reference
- Further Reading and Resources
1. Introduction and Learning Objectives
Welcome to this comprehensive introductory course on the Pascal programming language. Pascal was deliberately designed as a teaching language, and it remains one of the clearest and most structured languages for learning fundamental programming concepts.
Learning Objectives
By the end of this course you will be able to:
- Write, compile, and run complete Pascal programs.
- Understand and correctly use all major data types and control structures.
- Design modular programs using procedures, functions, and units.
- Work with structured data (arrays, records, sets, files).
- Manage dynamic memory with pointers.
- Apply basic object-oriented concepts in modern Pascal dialects.
- Debug programs systematically and follow professional coding standards.
- Compare Pascal’s design philosophy with languages such as C, Python, and Java.
Why Pascal Still Matters in 2026
Although Pascal is no longer a mainstream industry language, it remains extremely valuable for education because:
- It enforces strong typing and structured programming.
- Its syntax is highly readable and self-documenting.
- Many modern languages (Ada, Modula-2, and even aspects of C# and Go) inherited ideas from Pascal.
- Free Pascal and Lazarus provide a complete, free, cross-platform development environment still used in universities worldwide.
Tip: Approach this course as you would a mathematics textbook — read carefully, type every example yourself, and complete the exercises.
2. History and Origins of Pascal
2.1 The Birth of Pascal
Pascal was designed in 1968–1970 by Professor Niklaus Wirth at ETH Zurich, Switzerland. Wirth wanted a language that:
- Encouraged good programming habits (structured programming).
- Was simple enough for teaching.
- Could be efficiently implemented on the computers of the time.
The language was named after the French mathematician and philosopher Blaise Pascal (1623–1662).
The first Pascal compiler was completed in 1970. The language was formally standardized by ISO in 1983 (ISO 7185) and later extended (ISO 10206 Extended Pascal).
2.2 Major Dialects and Evolution
| Dialect |
Year |
Key Characteristics |
Still Used? |
| Original Pascal |
1970 |
Wirth’s original design |
Historical |
| UCSD Pascal |
1970s |
P-code virtual machine |
Rare |
| Turbo Pascal |
1983 |
Extremely fast compiler, IDE, units |
Legacy |
| Object Pascal |
1986+ |
Object-oriented extensions (Borland, Apple) |
Yes |
| Free Pascal |
1993– |
Open-source, highly compatible with Turbo/Delphi |
Yes |
| Delphi |
1995– |
Commercial RAD environment based on Object Pascal |
Yes |
Today, Free Pascal (with the Lazarus IDE) is the recommended free environment for students.
2.3 Design Philosophy
Pascal follows these core principles:
- Strong static typing — many errors are caught at compile time.
- Structured programming — no unrestricted
goto (though a limited form exists).
- Orthogonality — few special cases.
- Readability over brevity.
Comparison:
- Versus C: Pascal is safer and more readable; C is lower-level and more flexible.
- Versus Python: Pascal requires explicit declarations and has a rigid structure; Python is dynamic and concise.
- Versus Java: Pascal is lighter and closer to the machine; Java has a much larger standard library and garbage collection.
3. Setting Up Your Development Environment
3.1 Recommended Tools
- Free Pascal Compiler (fpc) – the compiler.
- Lazarus – a free Delphi-like IDE (highly recommended for beginners).
- Alternatives: VS Code + Pascal extension, or online compilers (e.g., OnlineGDB, Replit).
3.2 Installation (Brief)
3.3 Your First Program
Create a file named hello.pas:
program HelloWorld;
begin
writeln('Hello, University Student!');
writeln('Welcome to Pascal.');
end.
Compile and run:
fpc hello.pas
./hello
Note: The period (.) after end is mandatory — it marks the end of the program.
4. Basic Program Structure
A minimal Pascal program has this skeleton:
program ProgramName;
{ Optional uses clause }
uses
SomeUnit;
{ Constant declarations }
const
...
{ Type declarations }
type
...
{ Variable declarations }
var
...
{ Procedure and function declarations }
procedure ...;
function ...;
{ Main program block }
begin
{ Statements }
end.
Key Points
- Everything is case-insensitive (
Begin, BEGIN, and begin are identical).
- Comments:
{ this is a comment } or (* this is also a comment *). Free Pascal also supports // single-line comments.
- The
program heading is optional in Free Pascal but recommended for clarity.
- Declarations must appear before use (no forward references except with
forward).
Why this matters: Pascal’s strict declaration-before-use rule forces you to think about data before algorithms — a valuable discipline.
5. Fundamental Data Types
Pascal has a rich and well-organized type system.
5.1 Simple (Primitive) Types
| Type |
Description |
Typical Range (Free Pascal) |
Example |
Integer |
Signed whole numbers |
−2 147 483 648 .. 2 147 483 647 |
42 |
LongInt |
Same as Integer on 32-bit |
Same |
|
Int64 |
64-bit integer |
±9×10¹⁸ |
|
Real |
Floating-point |
≈ 1.5×10⁻⁴⁵ .. 3.4×10³⁸ |
3.14159 |
Double |
Double-precision float |
Higher precision |
|
Boolean |
Logical values |
True / False |
True |
Char |
Single character |
ASCII / Unicode (depending) |
'A' |
String |
Sequence of characters (special) |
Up to 255 (short) or unlimited |
'Hello' |
Warning: In classic Pascal, String was not a standard type. Modern Free Pascal and Delphi treat String as a dynamic type.
5.2 Ordinal Types
Types that have a defined order and can be used in for loops, case statements, and as array indices:
- All integer types
Boolean
Char
- Enumerated types
- Subrange types
5.3 Enumerated Types
type
TDayOfWeek = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
var
Today: TDayOfWeek;
begin
Today := Wednesday;
writeln(Ord(Today)); { Outputs 2 (zero-based) }
end.
5.4 Subrange Types
type
TDigit = 0..9;
TUpperLetter = 'A'..'Z';
Why this matters: Subranges catch many logical errors at compile time (e.g., assigning 10 to a TDigit).
6. Variables, Constants, and Literals
6.1 Variable Declaration
var
Age: Integer;
Name: String;
IsStudent: Boolean;
Height: Real;
Multiple variables of the same type:
var
x, y, z: Real;
Count1, Count2: Integer;
6.2 Constants
const
Pi = 3.1415926535;
MaxStudents = 100;
UniversityName = 'Swiftener University';
IsDebug = True;
Typed constants (can be changed in some dialects — avoid this):
const
Counter: Integer = 0;
6.3 Literals
- Integer:
42, −17, $FF (hex), %1010 (binary in Free Pascal)
- Real:
3.14, 2.5E3 (= 2500)
- Character:
'A', #65 (ASCII code)
- String:
'Hello', 'It''s a test' (escaped apostrophe)
- Boolean:
True, False
Common Pitfall: Using = instead of := for assignment.
x = 5; { This is a comparison, not assignment! }
x := 5; { Correct assignment }
7. Operators and Expressions
7.1 Arithmetic Operators
| Operator |
Meaning |
Example |
Result Type |
+ |
Addition |
3 + 4 |
Integer/Real |
- |
Subtraction |
10 - 3 |
|
* |
Multiplication |
2 * 5 |
|
/ |
Real division |
7 / 2 |
Real |
div |
Integer division |
7 div 2 |
Integer (3) |
mod |
Modulo |
7 mod 2 |
Integer (1) |
7.2 Relational Operators
=, <>, <, >, <=, >=
7.3 Logical Operators
and, or, not, xor
Important: Pascal uses short-circuit evaluation for and and or in modern compilers, but classic Pascal did not guarantee it.
7.4 Operator Precedence (highest to lowest)
not
*, /, div, mod, and
+, -, or, xor
- Relational operators
Always use parentheses for clarity.
Comparison with other languages:
- Pascal uses
:= for assignment and = for equality (opposite of many languages).
div and mod are keywords, not symbols (/ and % in C/Java).
8. Input and Output
8.1 Standard Output
write('Hello'); { No newline }
writeln('Hello'); { With newline }
writeln('Value = ', x:6:2); { Formatted: width 6, 2 decimal places }
8.2 Standard Input
var
Name: String;
Age: Integer;
begin
write('Enter your name: ');
readln(Name);
write('Enter your age: ');
readln(Age);
end.
read vs readln:
read leaves the newline in the buffer.
readln consumes the entire line.
Common Pitfall: Mixing read and readln often causes the next input to be skipped.
9. Control Structures
9.1 Conditional Statements
if-then-else
if Score >= 50 then
writeln('Pass')
else
writeln('Fail');
Compound statements require begin…end:
if Age >= 18 then
begin
writeln('Adult');
CanVote := True;
end
else
begin
writeln('Minor');
CanVote := False;
end;
case Statement
case Grade of
'A': writeln('Excellent');
'B': writeln('Good');
'C': writeln('Average');
'D', 'F': writeln('Needs improvement');
else
writeln('Invalid grade');
end;
The case selector must be an ordinal type.
9.2 Loops
while Loop
while Count < 10 do
begin
writeln(Count);
Count := Count + 1;
end;
repeat-until Loop
repeat
write('Enter a positive number: ');
readln(Number);
until Number > 0;
(Note: the body always executes at least once.)
for Loop
for i := 1 to 10 do
writeln(i);
for i := 10 downto 1 do
writeln(i);
The control variable must be an ordinal type and should not be modified inside the loop.
Why this matters: Pascal’s loops are clean and discourage the “off-by-one” errors common in C-style for loops.
10. Procedures and Functions
10.1 Procedures
procedure Greet(Name: String);
begin
writeln('Hello, ', Name, '!');
end;
10.2 Functions
function Add(a, b: Integer): Integer;
begin
Add := a + b; { Classic style }
{ or: Result := a + b; } { Free Pascal / Delphi style }
end;
10.3 Parameter Passing
| Mode |
Keyword |
Behavior |
Use Case |
| Value |
(none) |
Copy of the value |
Input only |
| Variable |
var |
Reference (can modify original) |
Output / in-out |
| Constant |
const |
Read-only reference |
Large structures |
| Out |
out |
Output only (Free Pascal) |
Pure output |
Example:
procedure Swap(var x, y: Integer);
var
Temp: Integer;
begin
Temp := x;
x := y;
y := Temp;
end;
10.4 Recursion
function Factorial(n: Integer): Integer;
begin
if n <= 1 then
Factorial := 1
else
Factorial := n * Factorial(n - 1);
end;
10.5 Forward Declarations
When two procedures call each other:
procedure ProcA(x: Integer); forward;
procedure ProcB(y: Integer);
begin
if y > 0 then ProcA(y - 1);
end;
procedure ProcA(x: Integer);
begin
if x > 0 then ProcB(x - 1);
end;
11. Arrays
11.1 One-Dimensional Arrays
var
Scores: array[1..100] of Integer;
Letters: array['A'..'Z'] of Boolean;
Access: Scores[5] := 87;
11.2 Multi-Dimensional Arrays
var
Matrix: array[1..3, 1..4] of Real;
begin
Matrix[2, 3] := 4.5;
end.
11.3 Dynamic Arrays (Free Pascal / Delphi)
var
Data: array of Integer;
begin
SetLength(Data, 10);
Data[0] := 42; { Zero-based! }
end.
Pitfall: Classic Pascal arrays are static and fixed at compile time. Dynamic arrays are a modern extension.
12. Records (Structured Types)
Records group related data of different types:
type
TStudent = record
Name: String;
Age: Integer;
GPA: Real;
IsActive: Boolean;
end;
var
S: TStudent;
begin
S.Name := 'Alice';
S.Age := 20;
S.GPA := 3.8;
S.IsActive := True;
end.
With with statement (use sparingly):
with S do
begin
Name := 'Bob';
Age := 21;
end;
Records can be nested and can contain arrays.
13. Sets
Sets are one of Pascal’s most elegant features.
type
TDigitSet = set of 0..9;
var
EvenDigits, Digits: TDigitSet;
begin
EvenDigits := [0, 2, 4, 6, 8];
Digits := [1, 3, 5] + EvenDigits; { Union }
if 4 in EvenDigits then
writeln('4 is even');
end.
Operators: + (union), * (intersection), - (difference), = , <= (subset), in.
Maximum set size is limited (usually 256 elements for base types).
14. Files (Text and Binary)
14.1 Text Files
var
f: Text;
begin
Assign(f, 'data.txt');
Rewrite(f); { Create new file }
writeln(f, 'Hello file');
Close(f);
Reset(f); { Open for reading }
{ read data }
Close(f);
end.
14.2 Binary / Typed Files
type
TStudentFile = file of TStudent;
var
sf: TStudentFile;
begin
Assign(sf, 'students.dat');
Rewrite(sf);
Write(sf, S); { Write a record }
Close(sf);
end.
15. Pointers and Dynamic Memory Management
type
PInteger = ^Integer;
var
p: PInteger;
begin
New(p); { Allocate }
p^ := 42; { Dereference }
writeln(p^);
Dispose(p); { Free }
end.
Linked lists are a classic application of pointers in Pascal courses.
Warning: Always match every New with a Dispose. Memory leaks are easy to create.
16. Units and Modular Programming
Units allow separate compilation and modular design.
Example unit (mathutils.pas):
unit MathUtils;
interface
function Square(x: Real): Real;
function Cube(x: Real): Real;
implementation
function Square(x: Real): Real;
begin
Square := x * x;
end;
function Cube(x: Real): Real;
begin
Cube := x * x * x;
end;
end.
Main program:
program Test;
uses MathUtils;
begin
writeln(Square(5.0));
end.
17. Object-Oriented Extensions in Modern Pascal
Free Pascal and Delphi support full object-oriented programming:
type
TPerson = class
private
FName: String;
FAge: Integer;
public
constructor Create(const AName: String; AAge: Integer);
procedure Introduce;
property Name: String read FName write FName;
property Age: Integer read FAge write FAge;
end;
constructor TPerson.Create(const AName: String; AAge: Integer);
begin
FName := AName;
FAge := AAge;
end;
procedure TPerson.Introduce;
begin
writeln('I am ', FName, ', ', FAge, ' years old.');
end;
Inheritance, virtual methods, interfaces, and generics are all supported in modern dialects.
18. Exception Handling
Free Pascal / Delphi style:
try
{ risky code }
x := y div z;
except
on E: EDivByZero do
writeln('Division by zero!');
on E: Exception do
writeln('Error: ', E.Message);
end;
Also available: try…finally for cleanup.
19. Standard Libraries and Common Units
Important units in Free Pascal:
System — always available
SysUtils — string handling, file utilities, exceptions
Classes — TList, streams, etc.
Math — advanced mathematical functions
DateUtils — date and time
StrUtils — additional string functions
20. Debugging Practices and Common Errors
Common Compile-Time Errors
- Missing period after final
end
- Using
= instead of :=
- Undeclared identifiers
- Type mismatches
Runtime Errors
- Division by zero
- Array index out of bounds
- Accessing disposed pointers
Debugging Tips
- Read the compiler error messages carefully — they are usually precise.
- Use
writeln statements liberally during development.
- In Lazarus, use the integrated debugger (breakpoints, watches).
- Compile with range and I/O checking enabled (
{$R+}, {$I+}).
21. Best Practices and Coding Style
- Use meaningful names (
StudentCount not sc).
- Indent consistently (2 or 4 spaces).
- Declare variables as locally as possible.
- Prefer functions over procedures when a value is returned.
- Keep procedures short and focused.
- Comment the why, not the what.
- Always close files and free memory.
- Avoid deep nesting — extract procedures instead.
22. Comprehensive Exercises and Mini-Projects
Exercise Set A (Basics)
- Write a program that reads three numbers and prints them in ascending order.
- Calculate the factorial of a number using both a loop and recursion.
Exercise Set B (Structures)
- Create a record for a bank account and write procedures to deposit, withdraw, and display balance.
- Implement a set-based program that finds common elements between two lists of numbers.
Exercise Set C (Files & Modules)
- Write a unit that manages a simple student database stored in a binary file.
- Create a text-file based log system.
Mini-Project
Build a simple console-based library management system that uses records, files, units, and (optionally) classes.
Solutions are deliberately not fully provided here so you practice independent problem-solving. Discuss solutions with classmates or instructors.
23. Appendix: Quick Reference
Reserved Words (partial)
and, array, begin, case, const, div, do, downto, else, end, file, for, function, goto, if, in, label, mod, nil, not, of, or, packed, procedure, program, record, repeat, set, then, to, type, until, var, while, with
Useful Compiler Directives (Free Pascal)
{$mode objfpc} { Object Pascal mode }
{$R+} { Range checking }
{$I+} { I/O checking }
{$H+} { AnsiStrings }
24. Further Reading and Resources
- Pascal User Manual and Report — Kathleen Jensen & Niklaus Wirth
- Free Pascal Documentation: https://www.freepascal.org/docs.html
- Lazarus IDE Tutorials
- Classic textbook: Oh! Pascal! by Doug Cooper
- Modern reference: Free Pascal Reference Guide
Congratulations!
You now have a solid foundation in Pascal. The discipline you gain from its structured approach will make you a better programmer in any language.
#pascal #programming #computerscience #tutorial #universitycourse #freepascal #objectpascal #structuredprogramming #codingeducation #swiftener