Pascal Programming/Appendix

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Jumps[edit | edit source]

Usage of jumps are deemed bad practice. This topic has deliberately not been covered in any of the previous chapters, but for the sake of completeness it is explained in the appendix.

Using labels[edit | edit source]

Standard Pascal includes the infamous "goto" statement. It redirects the computer to a labeled statement somewhere else in the program. Labels are unsigned integers, although some compilers allow them to be words.

1: write('Enter an even number please: '); (*Try to get an even number*)
   readln(number);
   even := (number mod 2) = 0;
   if even then goto 2; (*Skip ahead*)
   writeln('That was an odd number.');
   goto 1; (*Try again*)
2: writeln('Thanks!'); (*Finished!*)

Declaring labels[edit | edit source]

If you use any labels, you are required to declare them ahead of time just as you would variables or constants.

program GetEvenNumber;
label 1, 2;
var
  number: integer;
  even: Boolean;

Avoiding labels[edit | edit source]

In some early programming languages, labeled jumps were the primary way of controlling the flow of execution. Too many labels and "goto" statements will read to unreadable code. Today, we would generally be expected to rewrite such code with the more specific control flow structures, like so:

repeat
  write('Enter an even number, please: ');
  readln(number);
  even := (number mod 2) = 0;
  if not even then writeln('That was an odd number.')
until even;
writeln('Thanks!');

Noteworthy types[edit | edit source]

type definition size (in bytes) availability
AnsiChar one ANSI-standard textual character 1
AnsiString an array of ANSI-standard characters of indefinite length with an optional size constraint 1 * number of characters
Boolean true or false 1 standard
Byte whole number ranging from 0 to 255 1
Cardinal synonym depending on processor type (16 bit=Word, 32 bit=LongWord, 64 bit=QWord) varies (2, 4, 8)
Char one textual character (likely ASCII) 1 standard
Comp a floating point number ranging 19-20 digits that is effectively a 64-bit integer 8
Currency a floating point number ranging 19-20 digits that is a fixed point data type 8
Double a floating point number ranging 15-16 digits 8
DWord whole number ranging from 0 to 4,294,967,295 4
Extended a floating point number ranging 19-20 digits 10
Int64 whole number ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 8
Integer synonym depending on processor type (16 bit=SmallInt, 32 bit=LongInt, 64 bit=Int64) varies (2, 4, 8) standard
LongInt whole number ranging from -2,147,483,640 to 2,147,483,647 4
LongWord whole number ranging from 0 to 4,294,967,295 4
Pointer an untyped pointer holding an address 32 bit=4, 64 bit=8 standard
PtrUInt a pointer type implicitly convertable to an unsigned integer 32 bit=4, 64 bit=8
QWord whole number ranging from 0 to 18,446,744,073,709,551,615 8 Free Pascal
Real a floating point number whose range is platform dependent 4 or 8 standard
ShortInt whole number ranging from -128 to 127 1
ShortString an array of textual characters of up to 255 elements (likely ASCII) with an optional size constraint 1 * number of characters (max 255) standard
Single a floating point number ranging 7-8 digits 4
SmallInt whole number ranging from -32,768 to 32,767 4
String synonym for ShortString (or AnsiString with the $H preprocessor directive turned on) 1 * number of characters (max 255) standard
UInt64 whole number ranging from 0 to 18,446,744,073,709,551,615 8 Free Pascal, Delphi 8 or later
WideChar one UTF-8 textual character 2
WideString an array of UTF-8 characters of indefinite length with an optional size constraint 2 * number of characters
Word whole number ranging from 0 to 65,535 2

Noteworthy preprocessor directives[edit | edit source]

directive description value(s) example availability
$COPERATORS allows use of C-style operators OFF or ON
{$COPERATORS ON}
i += 5;
i -= 5;
i *= 5;
i /= 5;
Free Pascal
$DEFINE defines a symbol for the preprocessor (if '$macro on', can have a value assigned) symbol name (:= value if '$macro on')
{$DEFINE Foo}
{$DEFINE Bar := 5}
standard
$H implies whether the String type is a ShortString or AnsiString - or + Delphi, Free Pascal
$I inserts a file's contents into the current source code filename
{$I hello.txt}
standard
$IF begins a preprocessor conditional statement compile-time boolean expression
{$IF DEFINED(DELPHI) OR DECLARED(Foo)}
standard
$IFDEF begins a preprocessor conditional statement depending if a preprocessor symbol is defined preprocessor symbol
{$IFDEF MSWINDOWS}
standard
$IFNDEF begins a preprocessor conditional statement depending if a preprocessor symbol is not defined preprocessor symbol
{$IFNDEF UNIX}
standard
$IFOPT begins a preprocessor conditional statement depending on the status of a preprocessor switch compiler option
{$IFOPT D+}
standard
$INCLUDE inserts a file's contents into the current source code filename
{$INCLUDE hello.txt}
standard
$INLINE allows inline functions and procedures OFF or ON
unit Foo;
{$INLINE ON}
interface
  function Give5: integer; inline;
implementation
  function Give5: integer;
  begin
    Give5 := 5;
  end;
end.
Free Pascal
$MACRO allows defined symbols to hold values OFF or ON
{$MACRO ON}
{$DEFINE Foo := 7}
Free Pascal
$MODE sets the Pascal dialect DELPHI, FPC, MACPAS, OBJFPC, TP Free Pascal
$R embeds a resource file into the code a file name
{$R *.dfm}
Delphi, Free Pascal
$STATIC allows use of the 'static' keyword OFF or ON
unit Foo;
{$STATIC ON}
{$MODE OBJFPC}
interface
  type
    Bar = class
      function Baz: string; static;
    end;
implementation
  function Bar.Baz: string;
  begin
    Result := 'This function is not part of a Bar instance.';
  end;
end.
Free Pascal