Pascal
Pascal was designed by Niklaus Wirth in 1970 to teach good programming. Its rule is that a program is built from clearly nested blocks (
begin ... end), with every variable declared and typed before use. It is a compact demonstration of structured programming.Paradigm: Structured Programming
What Makes Pascal Special
- Declarations before code. Constants, types, variables and subprograms are declared in fixed sections above the
beginblock, so a reader sees the data before the logic. - Strong static typing. The compiler rejects mixing types by accident. Subranges (
1..12), enumerations and sets are built into the language. - Functions and procedures. A function returns a value and a procedure does not, and they can be nested inside each other.
- Readable control flow.
if/then/else,while,repeat/until,forandcasecover all three structured shapes. - Its descendants are everywhere. Turbo Pascal, Delphi and Free Pascal/Lazarus made it a practical language, and it strongly influenced Modula-2, Oberon and Ada.
Example: Prime numbers up to 30
program Primes;
function IsPrime(n: Integer): Boolean;
var
d: Integer;
ok: Boolean;
begin
ok := n >= 2;
d := 2;
while ok and (d * d <= n) do
begin
if n mod d = 0 then
ok := False;
d := d + 1;
end;
IsPrime := ok;
end;
var
i: Integer;
begin
for i := 1 to 30 do
if IsPrime(i) then
Write(i, ' ');
WriteLn;
end.
How It Works
- The function
IsPrimereturns aBooleanby assigning to its own name, the classic Pascal way to return a value. - The
whileloop is the repetition shape; theifinside it is the decision shape. - The main block loops with
forand prints each prime, ending with2 3 5 7 11 13 17 19 23 29. - Note that
begin/endgroup statements: the block is a single unit.
History and Where It Is Used
Pascal is still a good first language for learning structure. Free Pascal and Lazarus produce native programs for Windows, Linux and macOS, and Delphi remains in business applications.
Learn More
- Free Pascal documentation
- Pascal (Wikipedia)
- Back to Structured Programming or the PGP roadmap.