TURBO PASCAL 7.0


The following topics are discussed in this section:

GENERAL INFORMATION

Turbo Pascal version 7.0 is a Pascal compiler for the IBM PC by Borland International. This version supports object oriented constructs as well as standard Pascal. To run effectively, you should use a computer with an 80286 or better processor, 1M of memory, and a mouse.

This version of Turbo Pascal comes complete with command line programs as well as an integrated development environment (IDE). The IDE allows you to create, edit, compile, link, run , and debug your programs from within the same application and user interface.

GETTING STARTED

To load the IDE, you must type the command

TURBO7

from a drive on which you are allowed to save files (such as your floppy drive).

USING TURBO PASCAL

Turbo Pascal has a fully integrated environment that allows the user to create, edit, compile, link, run, and debug programs all from within the same environment. Turbo Pascal displays its information in text windows that are configurable by the user. To close a window, click the left mouse button ( [Left Mouse Button] ) on the z in the upper, left corner of the window. The in the upper, right corner can be used to expand the window to its fullest size. Pressing it again will reduce it back to the previous size. A window can be resized by placing the mouse cursor on the lower, right corner of the window, hold down the [Left Mouse Button] , and slide the window borders to the new size. To move a window to a different location on the screen, move the mouse cursor to the top border of the window, hold down the [Left Mouse Button] , and slide the window to the desired position. The name of the file being edited will be displayed in the center of the top row of the window. If the file has been changed since the last time it was saved, a * will appear in the lower, left hand corner of the window. The bottom of the window also contains the current row and column position of the cursor. The scroll bars can be used to display different sections of the file. The upper right corner will also display the window's identification number.

It is possible to have several windows displayed on the screen at the same time. There are several ways to switch between windows. The first method is to hold down the [ALT] and press the identification number of the window. For example, to switch to window number three, press [ALT] [3] . You can also press [ALT] [0] to display a list of all the opened windows. This menu is also displayable by choosing the Window | List All... command. From this list, press the number of the window you wish to make active, or use the arrow keys to highlight the window and press [ENTER].

When selecting commands from the main menu, you must pay attention to the color of the command. Commands which are not available will be displayed in a gray text. When highlighted, these unavailable commands will be displayed as a solid black line. Valid commands will be displayed in black text, and will have a green background when highlighted. Active commands will also have a red letter in the command name. This letter is the shortcut key. For example, the File command has the letter F in red. Instead of choosing the File command with the mouse, you simply need to hold down the [ALT] and press [F] . Some keys will also have a function key combination listed after the command. This key combination can be used to issue the command directly, without having to choose the commands from the menu. For example, the [F2] key will automatically save your file -- just as if you had chosen the File | Save command.

Creating a New File

To create a new editor window, choose the File and then New commands from the main menu (File | New). This will open a new window with the header NONAMExx.PAS where xx is the next sequential unnamed window number.

Opening an Existing File

To open a file that was saved from a previous session, use the File | Open command or press [F3]. Turbo Pascal will then display a window showing all the files with a .PAS extension. To open a file, highlight it and press [ENTER] , double click the [Left Mouse Button] , or type the name of the file in the name box and press [ENTER]. You can change the pattern used for displaying files in the window by changing the name in the Name box. The default is *.PAS which displays all files with a .PAS extension. To see all the include files, enter *.INC or type *.* to see all files. If more files exist then can fit in the window, you can use the scroll bars to display the next page of files.

Using the Editor

Turbo Pascal comes with a powerful editor in the environment. If a mouse is present, you will have two cursors in your editor window. The big, block cursor is the mouse cursor. Pressing the [Left Mouse Button] will move the text (blinking underline cursor) to the mouse's current position. You can move about the file by using the arrow keys or the scroll bars.

To enter text into a file, place the text cursor at the appropriate position and then start typing. If the line is longer than the width of the window, Turbo Pascal will automatically shift the view in the window to display the newly entered text. Pressing [ENTER] will move the text cursor to the next line. Turbo Pascal will automatically indent the next line to the same column as the previous line.

Turbo Pascal will allow you to perform operations on a block of text. A block can be marked in one of two ways. The first is to place the mouse cursor at the start of the block and press [Left Mouse Button] . You can then move the mouse cursor to the end of the block and press [SHIFT] [Left Mouse Button] to mark the block. The same can be done by using [CTRL] [K] [B] to mark the beginning of the block and [CTRL] [K] [K] to mark the end of the block.

To transfer the marked block of text to another window or file, you must first copy the block to a special window called the clipboard. This is done by marking the block and then pressing [SHIFT] [DEL] to remove the text from the original file and placing it on the clipboard. If you want to copy the text from the original file to the clipboard, use [CTRL][Insert]. Pressing [SHIFT][Insert] will copy the block from the clipboard to the location of the text cursor in the active window. Note that the block copy and move commands do not place the blocked text onto the clipboard, only the above mentioned commands will do such.

Sometimes you may want to find a string of text in your file. Turbo Pascal lets you do this by issuing the search command ([CTRL][Q] [F] ). You can also press [CTRL][Q] [A] to perform a search and replace operation. After requesting one of the searches, Turbo Pascal will ask you for the string to find. After pressing [ENTER] , you will be asked for options. The options are as follows:

B  Searches the file backwards from the current cursor position to the beginning
G  Searches the entire file regardless of the current cursor position
N  Replaces the string without asking
U  Matches upper- or lowercase
W  Matches only complete words -- not substrings
n  Finds the nth occurrence of the string

Turbo Pascal has many key sequences that perform operations within the editing window. Following is a list of these commands and a brief description of what they do.

Cursor Commands

------------------------------------------------------------------------------
[Left Arrow]   Left one character          [Right Arrow]  Right one Character
[CTRL][A]      Left one word               [CTRL][F]      Right one word
[Up Arrow]     Up one line                 [Down Arrow]   Down one line
[CTRL][W]      Scroll up                   [CTRL][Z]      Scroll down
[PG UP]        Page up                     [PG DN]        Page down
[Home]         Go to the start of the line [End]          Go to the end of the line
[CTRL][Q] [E]  Go to top of the screen     [CTRL][Q] [X]  Go to bottom of the screen
[CTRL][Q] [R]  Go to top of the file       [CTRL][Q] [C]  Go to bottom of the file
[CTRL][Q] [B]  Go to start of the block    [CTRL][Q] [K]  Go to end of the block
[CTRL][Q] [P]  Go to last cursor position

Insert Commands

------------------------------------------------------------------------------
[Insert]       Toggle insert mode          [ENTER]        Insert a blank line

Delete Commands

--------------------------------------------------------------------------
[CTRL][Y]      Delete a line                 [CTRL][Q] [Y]  Delete to end of line
[Backspace]    Delete character to the left  [DEL]          Delete character at cursor
[CTRL][T]      Delete word to the right

Block Commands

--------------------------------------------------------------------------
[CTRL][K] [B]  Mark beginning of a block   [CTRL][K] [K]  Mark end of a block
[CTRL][K] [T]  Mark a word                 [CTRL][K] [H]  Hide block toggle
[CTRL][K] [C]  Copy a block                [CTRL][K] [Y]  Delete a block
[CTRL][K] [V]  Move a block                [CTRL][K] [P]  Print block to printer
[CTRL][K] [R]  Read a block from disk      [CTRL][K] [W]  Write a block to disk
[CTRL][K] N    Set a place marker, where N is a number 0-3

Search Commands

---------------------------------------------------------------------------
[CTRL][Q] [F]  Search                      [CTRL][A] [Q]  Search and replace
[CTRL][Q] N    Find place marker, where N is 0-3
[CTRL][L]      Repeat find

Pair Matching

--------------------------------------------------------------------------
[CTRL][Q] [    Math pair forward           [CTRL][Q] ]    Match pair reverse

Miscellaneous Commands

--------------------------------------------------------------------------
[CTRL][U]      Abort                       [CTRL][O] [I]  Toggle autoindent mode
[CTRL][P]      Control character prefix    [F10]          Exit editor
[F3]           New file                    [F2]           Save file
[CTRL][O] [T]  Toggle tab mode             [CTRL][Q] [L]  Undo

Saving Your File

There are three save commands under the File menu. The File | Save command (or [F2]) can be used to save the current file. If you have not saved the file before, the editor will ask for a filename under which to save the program. If you wish to save your file under a different name, use the File | Save As... command. To save all the opened files in the environment, use the File | Save All command.

Printing the Program

There are two ways to send your source code to the printer. The first is to have your program loaded in the active window and choose the File | Print command. If you are at a DOS prompt, you can enter the command:

PRT filename.ext

where filename.ext is the name and extension of the source code file.

To get a copy of the output, you should write a copy of the screen output to a file. Do not write programs that send output directly to the network printers. Such programs will not work properly over the network. You can also press [SHIFT][Print Screen] to send a copy of the text on the screen to the printer.

The following code shows how to write all pertinent information to a file.

program demo1;
uses CRT;
var     outfile:text;
begin
   assign(outfile,'OUTPUT.TXT');
   reset(outfile);
   writeln(outfile,'This line goes to the file OUTPUT.TXT.');
   close(outfile);
end.

Accessing the On-line Help

Turbo Pascal has an extensive on-line help facility. The help window can be accessed by using either the [F1]. To get help on the use of a function or keyword of Turbo Pascal, place the cursor on the word and press [CTRL][F1]. You can bring up the index of help by pressing [SHIFT][F1]. Pressing [F1] will bring up help on the use of the active window or currently highlighted menu command.

Compiling the Program

Before you can run your program, it must be compiled successfully. Turbo Pascal allows you to do this in several different ways. Pressing [ALT][F9] will tell Turbo Pascal to compile your program. If an error is found, it will be displayed on the top row of the window.

By default, Turbo Pascal compiles the program to memory. This means that the compiled code is placed into memory instead of saving the object file to your disk. If you wish to have the program compiled to your disk, choose the Compile | Destination command. This will toggle between compiling the program to memory and to disk. When you bring up the Compile menu, the current compilation location will be displayed after the Destination option.

If you have several source files and are using a project, you can press [F9] to make your project. This will cause Turbo Pascal to scan the source files listed in your project file and compile only those that have been modified since the last compilation. If you want to compile all the files in your project regardless of whether they have been modified, press [ALT][C] [B]. This will force each source file to be recompiled and linked into a new executable.

Running the Program

Once your program has been compiled successfully, you will want to run it. If your program requires parameters to be passed on the command line, you can press [ALT][R] [A] to specify them. Pressing [CTRL][F9] will then run your program with any specified parameters. If any of the files in your project have been modified since last compilation, Turbo Pascal will automatically recompile them and build a new executable before executing the program. When the program is done running, you can press [ALT][F5] to see your output screen.

If you are at a DOS prompt, you can run your program by typing the name of the program followed by any command line parameters separated by spaces. Your program will then run just as any other DOS application.

It is best to run your program from within the environment in case of problems. On the PC, the [CTRL][Break] key sequence will only stop execution during I/O. Therefore, if your program is stuck in an infinite loop that does not perform any I/O operations, you will be unable to halt the program. When running the program in the Turbo Pascal environment, pressing [CTRL][break] will halt the program and return you to the editor.

Debugging

Turbo Pascal comes with a powerful debugger built into the environment. This debugger will allow you to step through the execution of your program so you can see the results of each source line. You can inspect the values stored in the program's variables. You can even execute the program to a specific source line.

To evaluate the value of an expression, press [CTRL][F4]. Turbo Pascal will then ask for the expression to be evaluated. The result will be displayed in the result section of the window.

The debugger also comes with several commands to study the current state of the program. Pressing [CTRL][F3] will have Turbo Pascal display the call stack. The call stack is a list of the functions that are currently being executed. This is useful to determine the chain of execution among the functions. You can also inspect the value stored in a variable by pressing [ALT][F4]. All the variables for which you have requested to be inspected will be displayed in a window at the bottom of the screen. This window is updated after each source line is executed. This allows you to constantly watch the values stored in your variables.

You can also control the way the debugger executes your program. Pressing [F7] will cause the debugger to execute the next line in your source code. If the next line is a call to another function for which a source file is present, the debugger will trace into this function. Pressing [F8] will cause the debugger to execute the next source line in its entirety. This means if the next source line is a call to a function, the debugger will execute the entire function and not trace into it. You can also execute to a specific line by placing the cursor on the desired source line and pressing [F4].

Another method of controlling program execution is through the use of breakpoints. You can set breakpoints on any executable line of source code. When the debugger finds a source line that has a breakpoint marked, the debugger will stop execution. This will allow you to examine the status of the program before the line is executed. Breakpoints can be toggled by placing the cursor on the desired source line and pressing [CTRL][F8].

HELPFUL HINTS

Turbo Pascal uses single quotes to denote strings.

Variables of type string are the same as an array of char that starts at index 1. Position 0 of the string contains the size of the string. The length function can be used to determine the size of a string variable.

Placing a $ before a numeral make it a hexadecimal number.

Placing a # before a number tells Turbo Pascal that the number represents an ASCII value and should be of type char.

Turbo Pascal 7.0 short circuits when evaluating conditions. This means that as soon as it is determined that a condition will evaluate to false or true, the rest of the condition is ignored.

Turbo Pascal is similar to C in that loops and conditions (such as if, while, and for) operate on the next command. To execute more than one command with these statements, you must place a begin and end around the block.

REFERENCES/MANUALS

For learning the Turbo Pascal language, it is best to find a book that specifically talks about programming in this environment. Such books can be found at your local bookstore. These books will also include in more detail how to use the IDE and the command line versions of the applications.

EXAMPLES

{ This is a simple comment to describe what this program does}
{ This program simply shows the short circuit ability (1st loop)}
{ and how string variables store information (2nd loop)}
program bri;
var
  fred : string;
  i,j  : integer;
  ar   : array[0..1] of integer;
begin
  fred  := 'Hello there!';
  ar[0] := 12;
  ar[1] := -34;
  i := 0;
  while (i <  2) and not (ar[i] = 0) do
  begin
     writeln('Array is not 0, but ',ar[i]);
     i := i+1;
  end;
  for i:= 0 to length(fred) do
    writeln('fred[',i,']=[',fred[i],']');
end.

(c) Copyright UCF Computer Services I& R Support 08/25/93