This post is more than two years old and may contain outdated information.

Pascal as bite-sized executable programs

February 20, 2020

A collection of short Pascal programs to learn syntax and language features. Copy, paste, and execute here.

In this post:


Introduction #

Hello pascal #

program HelloPascal;

begin
    writeln('hello pascal');
end.

Bubble sort #

// https://en.wikipedia.org/wiki/Bubble_sort
program BubbleSort;

var
    i, j, tmp: integer;
    // array of unsorted integers
    numbers: array[0..4] of integer = (14, 33, 27, 35, 10);
begin
    for i := length(numbers) - 1 DownTo 0 do
        for j := length(numbers) - 2 DownTo 0 do
            if (numbers[j] > numbers[j + 1]) then
                begin
                    tmp := numbers[j];
                    // swap positions
                    numbers[j] := numbers[j + 1];
                    numbers[j + 1] := tmp;
                end;
            // end if
        // end for
    // end for

    // sorted
    for i := 0 to length(numbers) - 1 do write(numbers[i], ' ');
    // 10 14 27 33 35
END.

Insertion sort #

// https://en.wikipedia.org/wiki/Insertion_sort
program InsertionSort;

var
    i, j, tmp: integer;
    // array of unsorted integers
    numbers: array[0..4] of integer = (14, 33, 27, 35, 10);
begin
    for i := length(numbers) - 1 DownTo 0 do
        begin
            tmp := numbers[i];
            j := i;
            while ((j > 0) and (numbers[j - 1] > tmp)) do
                begin
                    numbers[j] := numbers[j - 1];
                    j -= 1;
                end;
            // end while
            numbers[j] := tmp;
        end;
    // end for

    // sorted
    for i := 0 to length(numbers) - 1 do write(numbers[i], ' ');
    // 10 14 27 33 35
end.

Values #

Numbers #

program Numbers;

begin
    // expression
    writeln(12 + 2 * (40 / 4) + 10);
    // 4.2...

    // floating point conversion
    writeln(12.0 + (10 + 20));
    // 4.2...

    // integer division
    writeln(30 div 20);
    // 1

    // integer modulo (remainder)
    writeln(42 mod 10);
    // 2
end.

Variables #

program Variables;

type
    // define enumerated type
    position = (Left, Right, Up, Down);
    // define range
    range = 1 .. 20;
var
    number: integer = 42;
    text: string = 'this is some text';
    float: real = 12.5;
    ch: char = 'A';

    // declare enumerated variable
    pos: position;

    // declare range variable
    rng: range;
begin
    writeln(number);
    // 42
    writeln(text);
    // this is some text
    writeln(float);
    // 12.5...
    writeln(ch);
    // A

    // enumerated
    pos := Right;
    writeln(pos);
    // Right

    // valid range
    rng := 12;
    writeln(rng);
    // 12

    // invalid range
    rng := 25;
    writeln(rng);
    // Warning: range check error while evaluating
    // constants (25 must be between 1 and 20)
end.

Consts #

program Consts;

const
    A = 100;
    B = 200;
begin
    writeln(A * B);
    // 20000

    // consts are immutable
    // A := 42;
    // Error: Variable identifier expected
end.

Data structures #

Records #

program Records;

type
    // define record type
    Element = record
        text: string;
        numbers: array[0..4] of integer;
        val: real;
    end;
var
    First, Second: Element;
begin
    First.text := 'this is my first element';
    First.numbers[0] := 1;
    First.numbers[1] := 2;
    First.val := 3.14;

    writeln(First.text);
    // this is my first element
    writeln(First.numbers[1]);
    // 2
    writeln(First.val);
    // 3.14...

    // with
    with Second do
    begin
        text := 'this is my second element';
        numbers[0] := 6;
        numbers[1] := 7;
        numbers[2] := 8;
        numbers[3] := 9;
        val := -12.5;
    end;

    writeln(Second.text);
    // this is my second element
    writeln(Second.numbers[3]);
    // 9
    writeln(Second.val);
    // -12.5...
end.

Sets #

program Sets;

type
    // sets are collections of same type
    days_of_week = set of (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
    letters = set of char;
    alphabet = set of 'A' .. 'Z';
var
    // declare sets
    A: alphabet;
    B: alphabet;
    C: alphabet;

    days: days_of_week;

    // declare iterator variables
    i: char;
begin
    A := ['A', 'B', 'C', 'D'];
    for i in A do write(i, ' '); writeln();
    // A B C D

    B := ['C', 'D', 'E', 'F'];
    for i in B do write(i, ' '); writeln();
    // C D E F

    // union
    for i in (A + B) do write(i, ' '); writeln();
    // A B C D E F 

    // intersection
    for i in (A * B) do write(i, ' '); writeln();
    // C D 

    // difference
    for i in (A - B) do write(i, ' '); writeln();
    // A B

    // symmetric difference
    for i in (A >< B) do write(i, ' '); writeln();
    // A B E F 

    // subset
    C := ['A', 'B', 'C'];
    if (C <= A) then writeln(true);
    // TRUE

    // include
    include(A, 'E');
    for i in A do write(i, ' '); writeln();
    // A B C D E

    // exclude
    exclude(A, 'E');
    for i in A do write(i, ' '); writeln();
    // A B C D

    // in
    if 'A' in A then writeln(true);
    // TRUE

    days := [Mon, Wed, Fri];
    if Wed in days then writeln(true);
    // TRUE
end.

Arrays #

program Arrays;

type
    // define type for array[20] of real
    default_array = array[0..19] of real;
var
    // declare variable using default array type
    arr: default_array;
    // declare array without default type
    vector: array[0..4] of integer;
    // nd-array
    matrix: array[0..9, 0..9] of integer;
    // declare and populate
    numbers: array[0..4] of integer = (1, 2, 3, 4, 5);
    // declare iterators
    i, j: integer;
begin
    // populate array
    for i := 0 to 19 do arr[i] := i;
    // access element
    writeln(arr[0]);
    // 0.0...
    writeln(arr[8]);
    // 8.0...

    // iterate array
    for i := 0 to length(numbers) - 1 do writeln(numbers[i]);
    // 1
    // 2
    // 3
    // 4
    // 5

    // populate nd-array
    for i := 0 to 9 do
        for j := 0 to 9 do
            matrix[i, j] := i + j;

    // access element in nd-array
    writeln(matrix[0, 0]);
    // 0
    writeln(matrix[3, 3]);
    // 6

    // length of first dimension
    writeln(length(matrix));
    // 10
end.

Control flow #

Conditionals #

program Conditionals;

const
    ANSWER = 42;
var
    number: integer = 42;
begin
    // single statement in block
    if number = ANSWER then writeln('yes')
    else writeln('no');
    // yes

    // more than one statements in block
    if number = ANSWER then
        begin
            if ANSWER = 42 then
                begin
                    writeln('42 is the answer');
                end
            else
                writeln('42 is not the answer..?');
            // end if
        end
    else
        writeln('no');
    // end if
    // 42 is the answer
end.

Loops #

program Loops;

var
    x: integer;
    i, j: integer;
    // for readln input
    input: string;
begin
    for i := 1 to 5 do write(i, ' ');
    // 1 2 3 4 5

    // nested
    for i := 0 to 1 do
        for j := 0 to 2 do
            write('(', i, ',', j, ') ');
        // end for
    // end for

    // (0,0) (0,1) (0,2) (1,0) (1,1) (1,2)

    // iterate down
    for i := 5 DownTo 0 do writeln(i);
    // 5
    // 4
    // 3
    // 2
    // 1
    // 0

    // while
    x := 0;
    while x < 5 do x += 1;
    writeln(x);
    // 5

    // repeat-until
    i := 0;
    repeat
        begin
            write(i, ' ');
            i += 1;
        end
    until i >= 5;
    // 0 1 2 3 4 

    // repeat until input is equal to value
    repeat readln(input) until input = 'exit';
    // > hello pascal
    // > ...
    // exit
    // ...Program finished with exit code 0
end.

Switch #

program Switch;

var
    numbers: array[0..4] of integer = (1, 2, 3, 42, 5);
    i: integer;
begin
    for i := 0 to length(numbers) - 1 do
        case numbers[i] of
            1: writeln('one');
            2: writeln('two');
            3: writeln('three');
            4: writeln('four');
            5: writeln('five');
        else
            writeln('what?');
        end;
    // end for

    // one
    // two
    // three
    // what?
    // five
end.

Functions and procedures #

Functions #

program Functions;

// define function
function power(base: integer; x: integer) : integer;
var
    // local variables
    result: integer = 1;
    i: integer = 0;
begin
    while i < x do
    begin
        result := result * base;
        i := i + 1;
    end;
    power := result;
end;

begin
    writeln(power(2, 8));
    // 256
end.

Procedures #

program Procedures;

// define procedure (sub-routine)
procedure MyProcedure;
var
    // variables are local
    number: integer = 0;
begin
    writeln('number: ', number);
    number += 1;
end;

begin
    MyProcedure;
    // number: 0

    MyProcedure;
    // number: 0
end.

I/O #

Inputs #

program Input;

var
    text: string;
begin
    writeln('> ');
    readln(text);
    // > hello pascal
    writeln(text);
    // hello pascal
end.

Files #

program Files;

var
    // variable for file
    f: file of string;
    // variable for text
    text: string;
begin
    // write to file
    assign(f, '_file.txt');
    rewrite(f);
    write(f, 'hello pascal');
    close(f);

    // read from file
    assign(f, '_file.txt');
    reset(f);
    while not eof(f) do
        begin
            read(f, text);
            writeln(text);
        end;
    // end while
    close(f);
    // hello pascal
end.

Misc #

Strings #

program Strings;
uses sysutils, Character;

var
    str: string = 'hello pascal';
begin
    writeln(str);
    // hello pascal
    writeln(str[1]);
    // h
    writeln(str[1..5]);
    // hello

    // length of string
    writeln(length(str));
    // 12

    // built-ins
    writeln(uppercase(str));
    // HELLO PASCAL
    writeln(lowercase(str));
    // hello pascal
    writeln(isnumber(str[1]));
    // FALSE
    writeln(isletter(str[1]));
    // TRUE
    writeln(isupper(str[1]));
    // FALSE
    writeln(islower(str[1]));
    // TRUE
end.

Logicals #

program Logicals;

const
    YES: boolean = true;
    NO: boolean = false;
begin
    writeln(YES or NO);
    // TRUE
    writeln(YES and (YES and NO));
    // FALSE
    writeln(not YES);
    // FALSE
    writeln(not (not YES));
    // TRUE

    writeln(1 = 1);
    // TRUE
    writeln(1 <> 1);
    // FALSE
    writeln(1 > 0);
    // TRUE
    writeln(1 < 0);
    // FALSE
    writeln(1 >= 1);
    // TRUE
    writeln(1 <= 0);
    // FALSE
end.

Date and time #

program DateAndTime;
uses sysutils;

begin
    // time
    writeln(TimeToStr(Now));
    // 00:15:17

    // date
    writeln(DateToStr(Now));
    // 26-5-21

    // increment month
    writeln(DateToStr(IncMonth(Now)));
    // 26-6-21
end.