Pascal and Delphi Tutorial
Learn Pascal and Delphi step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Are Pascal and Delphi? Meet Pascal, Object Pascal, Free Pascal, Lazarus and Delphi: where they came from, how they differ, what people build with them, and why they are still worth learning.
- Setting Up Free Pascal, Lazarus and Delphi Choose a Pascal toolchain and set it up: install Free Pascal, Lazarus or Delphi Community Edition, compile from the command line, pick a mode and fix common first-run problems.
- Your First Pascal Program Write, read and understand your first Pascal programs: Hello World, Write versus WriteLn, escaping quotes, formatting numbers and reading input with ReadLn.
- Program Structure, Comments and Style Learn how a Pascal program is organized: the program header, declaration sections, the main block, comment styles, identifiers, case rules, and clean formatting conventions.
Basics and Types
- Variables, Constants and Assignment Declare variables and constants in Pascal, understand typed constants, initial values, scope, assignment with :=, type compatibility and the difference between global and local variables.
- Integer and Real Types in Pascal Learn Pascal's numeric types: Integer, Byte, Word, Int64, Cardinal, Single, Double and Extended, their ranges, overflow, div and mod, conversions and formatting.
- Booleans, Characters and Strings in Pascal Work with Pascal's Boolean, Char and string types: logical values, character codes, short and long strings, indexing, concatenation, Length, Copy, Pos, UpCase and conversions.
- Operators and Expressions in Pascal A complete guide to Pascal operators: arithmetic, relational, logical, bitwise shl and shr, string and set operators, precedence levels, type promotion and common expression mistakes.
- Enumerations, Subranges and Sets in Pascal Use Pascal's ordinal types: enumerations, subrange types, Ord, Succ, Pred, High and Low, set types with in, union and difference, and how sets map to bit flags.
Control Flow and Procedures
- If, Else and Case in Pascal Make decisions in Pascal with if..then..else, compound statements, nested and chained conditions, the dangling else, the case statement with ranges and lists, and common semicolon mistakes.
- For, While and Repeat Loops in Pascal Repeat work in Pascal with for..to, for..downto, while..do and repeat..until, choose the right loop, use Break, Continue and Exit, avoid infinite loops, and iterate with for..in.
- Procedures and Functions in Pascal Learn Pascal subprograms: procedures that do something, functions that return values, the Result variable, forward declarations, local variables, overloading and default parameters.
- Parameters in Pascal: Value, var, const and out Understand how Pascal passes arguments: by value, by reference with var, read-only const, output-only out, open array parameters, untyped parameters and procedural types.
- Recursion and Nested Routines in Pascal Learn recursion in Pascal with factorial, Fibonacci and Towers of Hanoi, how the call stack works, base cases, nested procedures that share variables, and recursion versus loops.
Records, Arrays and Pointers
- Arrays in Pascal: Static, Dynamic and Open Master Pascal arrays: static arrays with any index range, multidimensional arrays, dynamic arrays with SetLength, copying, sorting basics, open array parameters and range checking.
- Records and Variant Records in Pascal Group related data with Pascal records: declare fields, copy and compare records, use with statements, arrays of records, packed and variant records, and advanced records with methods.
- Pointers and Dynamic Memory in Pascal Understand Pascal pointers: typed pointers, the ^ and @ operators, New and Dispose, nil and Assigned, GetMem and FreeMem, memory leaks, dangling pointers and pointer arithmetic.
- Linked Lists and Trees with Pointers in Pascal Build linked data structures in Pascal: singly linked lists, push and pop stacks, insertion and deletion, a binary search tree with recursion, traversal and freeing nodes safely.
Units and Object Pascal
- Units and the uses Clause Split a Pascal or Delphi program into units, learn how the uses clause finds code, how name clashes are resolved, and how compiled units are built and linked.
- Interface and Implementation Sections Learn how a Pascal unit hides its details, why private state belongs in the implementation section, and how to break circular unit dependencies safely.
- Initialization and Finalization See how Delphi and Free Pascal run unit setup and cleanup code, in which order units start and stop, and which pitfalls to avoid in initialization sections.
- Generics and Anonymous Methods Write type-safe generic classes like TList of T, add constraints, and use anonymous methods and closures in modern Delphi and Free Pascal, with a runnable C stand-in.
Classes and Properties
- Classes and Objects Declare Object Pascal classes, create and free objects safely with try..finally, understand object references versus records, and see the idea in a runnable C stand-in.
- Properties and Visibility Use private, protected, public and published sections, write properties with read and write accessors, validate values, and add indexed and default properties in Object Pascal.
- Inheritance and Virtual Methods Build class hierarchies in Object Pascal with inherited, virtual and override, abstract classes, is and as type tests, and see how a virtual method table works in C.
- Interfaces and Exceptions Define Object Pascal interfaces with reference counting, then handle errors with try..except, try..finally, custom exception classes and raise, with a runnable C model.
Files and Databases
- Text Files and TStringList Read and write text files in Pascal and Delphi with AssignFile, Reset, Rewrite and ReadLn, handle I/O errors, and use TStringList and TFile for modern, shorter code.
- Typed Files and Streams Store records in Pascal typed files with Seek and FileSize, copy raw bytes with BlockRead and BlockWrite, and use TFileStream and TMemoryStream, with a runnable C record-file model.
- Databases with FireDAC Connect Delphi to SQLite or any SQL database with FireDAC, run queries with parameters, walk datasets with First, Next and Eof, and use transactions safely.
Forms and Tooling
- VCL Forms and Components Understand how Delphi builds windows from forms and components, how Owner and Parent differ, how .dfm files and the Object Inspector work, and how to create controls in code.
- Events and the IDE Learn how Delphi events work as method pointers, how to write and assign handlers, how the Windows message loop drives forms, and how to keep an interface responsive.
- Debugging and Compiler Tools Debug Pascal with breakpoints and watches, catch range and overflow errors with compiler directives, report memory leaks, and compare Delphi, Free Pascal and Lazarus.
Projects and Practice
- Project: Contact Book Build a contact book in Pascal that stores records, searches, sorts and saves to a typed file, then run the same design as a complete C program in your browser.
- Project: Bank Accounts Design a bank account system in Object Pascal with a base class, savings and checking descendants, custom exceptions and safe transfers, plus a runnable C model with a virtual table.
- Project: Text Adventure Engine Build a small text adventure in Pascal using enumerations, sets, records, case statements and a command parser, plus a runnable C version driven by a scripted game.
- Pascal Practice Set Twelve graded Pascal and Delphi exercises on units, classes, files and databases, with hints, worked solutions for three of them and a runnable C check of the logic.