Lecture 1

To review the material from this lecture, read the following sections of our Pascal Made Simple textbook:

Here are a few more notes on various topics.

Programs and algorithms

Compiling a program

I recommend Geany for editing Pascal programs. Save your program to a filename with extension '.pas'. In Geany, use the Build/Compile command (F8) to compile your program. Use the Run command (F5) to run it.

You can also use any other text editor. To compile from the command line:

$ fpc hello.pas

To run a program on UNIX or Mac OS:

$ ./hello

On Windows, omit the "./":

C:\> hello

Adding a series of numbers

program add;

var
  sum: integer;
  n: integer;

begin
  sum := 0;
  
  while not eof do
    begin
      readln(n);
      sum := sum + n;
    end;
  
  writeln('the sum is ', sum);
end.

The built-in eof (= “end of file”) function returns true when there is nothing more to read from standard input.

To run:

$ ./add
4
5
6
^D
the sum is 15

Use ^D to terminate the input on UNIX and Mac OS. On Windows, type ^Z.

Redirecting input from a file

If the file nums contains

5
6
7

then you can compute the sum like this:

$ ./add < nums
the sum is 18

Generating random numbers

Built-in functions:

randomize()                   // seed the random number generator
random(n: integer): integer   // return a random number r, with 0 <= r < n

A simple game

program guessIt;

var
  secret: integer;
  guess: integer;

begin
  secret := random(1000) + 1;
  writeln('I''m thinking of a number from 1 to 1000');
  repeat
    write('Your guess? ');
    readln(guess);
    if guess < secret then
      writeln('too low')
    else if guess > secret then
      writeln('too high');
  until guess = secret;
  writeln('you got it');
end.

A program that overflows

program overflow;

var
  a, b, c: integer;
 
begin
  a := 20000;
  b := 20000;
  c := a + b;
  writeln(c);
end.

Surprisingly, this program prints -25536!

It has overflowed: by default integers are 2 bytes (16 bits) in size, and can hold only numbers up to 32,767.

Delphi mode

{$mode delphi} enables Delphi mode (a dialect of Pascal):

Recommended for all programs.

Binary numbers

decimal = base 10, binary = base 2.

A bit is a binary digit, either 0 or 1.

An unsigned value is always 0 or positive. An unsigned n-bit value has the range 0 <= x < 2n.

A signed value can be negative, 0, or positive. Its sign is either -, 0 or +. A signed n-bit value has the range - 2n <= x < 2n.

A byte is an 8-bit value. An unsigned byte can range from 0 to 255. A signed byte can range from -128 to 127.

signed integer types

type

size (bytes)

range

shortint

1

-128 ... 127

smallint

2

-32,768 ... 32,767

integer*, longint

4

-2,147,483,648 .. 2,147,483,647

int64

8

-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807

* - integer is longint assuming you have remembered to enable Delphi mode.

unsigned integer types

type

size (bytes)

range

byte

1

0 ... 255

word

2

0 ... 65,535

longword

4

0 ... 4,294,967,295

qword

8

0 .. 18,446,744,073,709,551,615

Converting between characters and integers

Use the ord and chr functions to convert a character to an integer, or vice versa:

var
  c: char = 'x';
  i: integer;

begin
  i := ord(c);
  c := chr(i);
end;

This conversion yields an ASCII code. Be warned: integer('0') is not 0!

Alternatively, you can use a type cast to perform these conversions:

begin
  i := integer(c);
  c := char(i);
end;

floating-point types

type

size (bytes)

significant digits

range

single

4

7-8

1.5E-45 ... 3.4E38

real*, double

8

15-16

5.0E-324 .. 1.7E308

* - real may be equal to single on some platforms.

A program with floating-point numbers

program diminish;

var
  r: real;
 
begin
  r := 1.0;
  while r > 0 do
    begin
      r := r / 2;
      writeln('r = ', r);
    end;
end.

div and mod

As described in the Pascal Made Simple book, the div operator divides two integers, truncating the result into an integer. The mod operator returns the remainder after a division.

For all x and y,

x = (x div y) * y + x mod y

Reading characters from strings

The length function returns the length of a string:

length('xyzzy') => 5

You can retrieve the i-th character of a string s using the syntax s[i]. The first character is numbered 1.

var
  s: string = 'abcde';

begin
  writeln(s[3]);  // writes 'c'
end

Writing to strings

Strings are mutable. You can change them by assigning to s[i]:

var
  s: string;
  
begin
  s := 'bike';
  s[1] := 'p';
  writeln(s);   // will write 'pike'
end

Accessing command-line parameters

Built-in functions in the system unit:

paramCount: integer            // the number of command-line parameters
paramStr(n: integer): string   // retrieves parameter n (starting with 1)

Converting between integers and strings

Functions in the sysutils unit:

intToStr(integer): string
strToInt(string): integer

Adding two numbers specified on the command line

program add;

uses
  sysutils;

var
  m, n: integer;

begin
  if paramCount <> 2 then
    writeln('usage: add  ')
  else
    begin
      m := strToInt(paramStr(1));
      n := strToInt(paramStr(2));
      writeln('the sum is ', m + n);
    end
end.

To run:

$ ./add 4 5
the sum is 9