Supplement to Turbo Pascal 7.0
Samuel L. Marateck
2.1 RUNNING TURBO PASCAL We begin with some definitions. A set of information, be it a program or data, is called a file. A Pascal file name consists of the name, with up to 8 characters, followed by a period, and ending with an extension PAS. The file name is not case sensitive, so PROG1.PAS and prog1.pas are equivalent.
2.2 OPENING (LOADING) A NEW PROGRAM Once you open TURBO PASCAL, the following display will appear on the screen.
This is called the Integrated Development Environment, abbreviated IDE.; Position the mouse cursor on the the File menu and then click it. A pull-down menu will appear.
Keeping the mouse button depressed, move the mouse cursor to the Open...option (this is called dragging the cursor) and release the button. A box requesting information will appear on the screen. This type of box is called a dialog box. This dialog box is entitled Open a File. In a small rectangle at the top of this box, *.PAS appears with the cursor blinking beneath the asterisk. To create your own file, say prog1.pas, type prog1. This will appear in this rectangle. You do not have to type the pas extension. Then either press the Enter key or click the Open button. The dialog box will then disappear and PROG1.PAS will appear at the top of the screen. After you write your program, as shown here,
you should save it on disk. To do this, either depress the F2 key or select Save on the File menu. If you want to save your file on drive A:, when you name your program, type a:prog1. If you have already have opened the file as prog1.pas, you can still save it on drive A: by selecting Save As on the File menu and typing a:prog1 and then pressing Enter.
Another definition: A directory ; is a grouping of files on a disk. The directory name is preceded by a slash (\). A directory can be divided into subdirectories. Thus, \TP\BIN refers to the subdirectory BIN found in the directory TP..
2.3 OPENING (LOADING) AN OLD PROGRAM; Select the Open option on the Files menu. Then, place the cursor in the Files rectangle appearing in the Open a File dialog box, on the file name corresponding to the file name that you wish to open, here JOHNNY.PAS.
When you click the mouse button, the file name will appear in the name rectangle. Then click the Open option (its also called a button) on the upper right of the dialog box. The dialog box will disappear and the program you chose appears on the screen.
2.4 RUNNING THE PROGRAM Place the mouse cursor on the Run menu and click it. A menu will open with the first option being .i. Run ;(Ctrl-F9).
Drag the mouse cursor to this option and release the button. If there is an error, correct it as described in section 3.12 (page 68) of the textbook. If there are no errors, your program will execute and the results will appear on the output screen. When execution is complete, the output screen disappears and your program will reappear on the screen. If you wish to see your output, either hit Alt and F5 or select the Debug menu and drag to the .i.User screen option, as shown in
When you release the button, the IDE will dissappear and the output screen with your output will appear on the screen.
To return to the IDE, hit any key or the mouse button.
In order to see your output appear together with your program in the IDE, select the Debug menu and drag to the Output option. A window will appear at the bottom of your screen with your output in it. You may position that window anywhere on the screen by placing the mouse cursor on the border of the output window, depressing the mouse button and dragging the cursor to the desired spot on the screen and releasing the button. The output window will then appear there.
If all of your output does not appear in the window, place the mouse cursor on the down arrow on the right-hand border of the Output window and then click it. This will scroll the screen downward. To scroll upward, place the mouse cursor on the up arrow on the right-hand border and click it.
If you want to display another program on the IDE, select the Open option on the File menu and choose the desired file name from the Files box. When you open the file, it will appear on a window which is on top of the window for the previous program. You may then run or edit the program. Each time you open another program, it will appear in a window on top of the previous window. To return to the program in the previous window, click the green rectangle in brackets imbedded in the top left corner of the window border. If you successively click the green rectangles in all the windows, all the windows will disappear, leaving a blue background on the screen.
2.5 CHANGING THE DIRECTORY When you select the Open option on the File menu, only the files in one directory will be displayed. To change the directory, pull down the Files menu and select Change dir.... A dialog box entitled Change Directory will appear containing a directory tree, as shown here.
On the tree, click the C:\ (which represents the root, the parent of all the directories in the tree). This will highlight the C:\. Then click the Chdir button at the right of the box. All the directories of the tree will appear. Then click the desired directory on the tree, and finally click the Chdir button. Now only the root and the selected directory will appear in the tree. To execute this change, conclude by clicking the OK button. The new directory name will appear in the box entitled Directory name. Now when you choose the Open option in the Files menu, all of the file names in your selected directory will appear.
2.6 THE TILE DISPLAY The mode in which the programs are displayed, one above the other, is called the cascade mode. To have the windows containing the separate programs displayed simultaneously, i.e., sharing the screen, click the Window menu and choose the Tile option. To see a list of all the programs displayed, choose the List option under the same menu .
When more than one program is displayed on the screen, as is shown here, because you have choosen the Tile option,
the active one (here, PROGRAM1.PAS) has the cursor blinking in its window. To make another window active, place the mouse cursor there and click once. The active window will have a green arrow at its upper right hand corner. In order to get the window to occupy the entire screen, click the green arrow.
2.7 SEARCH AND REPLACE To find a string (or word) in your program, select the Search menu and drag the cursor to the Search option. A dialog box opens on the screen and the cursor flashes in the Text to find box. Type the word you wish to find. It will appear in that box. After you type the word, do not hit the Enter key (if you do, the search will begin with the default options, the options indicated with the parenthesized dots). Then position the cursor with the mouse on the options you wish and click. For instance, to search the entire program for a given word, click the Whole words only and the Entire scope options. Finally, click the OK button.
To replace a string, choose the Replace option. A dialog box opens with the cursor flashing in the Text to find box,
however this time, the word you typed in the Find dialog box also appears here. You may, if you wish, change the word. Next position the cursor in the New text box and click it. Then type the word you wish to substitute and then with the mouse, click the options you want. The IDE assumes you wish to be prompted for each replacement, so an X is marked for the Prompt on replacement option. To override this option, click this X. Finally, click the OK box. The system then replaces the first occurence of the selected word. To replace additional occurrences of the word, select the Search again option. If you wish to replace all occurrences of a word, click the Replace all button.
2.8 EDITING (See also section C3, page 847 of the textbook for keyboard editing commands.) To select a block of text to move, copy or erase, click the beginning of the block of text and drag the cursor to the end of the block. The block of text will be highlighted, as shown here.
Select the Edit menu . If you want to copy the block to another place in the program, drag to the Copy option. The block will then be copied to what is called the clipboard (to see it, choose the Show clipboard option on the Edit menu; to return to your program, click the green spot at the upper left of the window).
Then, position the cursor to the place where you want to copy to, and then drag to the Paste option on the Edit menu. The block will then appear in its original position and the copied position. To simply move it from its original position to the desired destination, drag to the Cut option instead of the Copy option, and then after positioning the cursor to the desired destination, choose the Paste option. To erase a block, just cut it. To cancel an operation immediately after its performed, select the Undo option.
2.9 DEBUGGING To see the values asigned to a variable as execution progresses, place the cursor on the variable. Then click the Debug menu and drag to the Add watch option. A dialog box entitled Add watch appears on the screen and the variable chosen, here one, will appear in a rectangle labled Watch expression., as shown here,
Then click the OK button. If the proper variable does not appear in the box, type it. The variable will appear on the bottom of the screen in a window entitled Watches. If it does not, select the Watch option on the Debug menu and the Watch window will appear ..
Since the program hasn't been executed, the variable appearing in the window is described as an unknown identifier.
To add more watch variables to the watch window, repeat the process by selecting the Add watch option. To trace the steps in your program, continuously hit the F7 key. As you do this, the statement being executed will be highlighted in the program window, and the the variable's value at that statement will appear at the variable's right in the Watches window. We see in the following that the value of one is 23.
If you do not want to trace the statements in a procedure, hit the F8 key instead of the F7 key when the procedure call is highlighted.
To start the trace at a given statement in the program, position the cursor to that statement and then hit the F4 key. To stop a trace press Ctrl-F2. For more information on using the Watch window, see page 194 in the textbook for tracing the value of variables, page 456 for tracing arrays, and page 634 for tracing records.
Breakpoints. To see the values of the watch variables at certain points in the program, position the cursor on the statement of interest and press Ctrl-F8. The statement, called a breakpoint will be highlighted in red. You may select up to 20 breakpoints in the program. To see the values of the watch variables at the first breakpoint, press Ctrl-F9 and the program will execute until the first breakpoint. If the watch window is not displayed, choose the Watch option on the Debug menu. Each time you press Ctrl-F9, the program wil execute to the next breakpoint. To see where all the break points are, choose the Breakpoints option on the Debug menu and a window will appear. To erase all breakpoints, click the Clear all button in this window. All breakpoints should be cleared before the next program is executed.
Erasing variables from the Watch window. To erase a variable from the Watch window, select the window by clicking the mouse button. Next click the variable to be erased and it will be highlighted. Then hit the Del key. To clear all the watch variables from the window, select the Close option on the Window menu. This will also erase the watch window. To erase all the variables from the watch window without erasing the window itself, hit Alt-F10 while the watch window is selected. A pop-up menu will appear, as indicated here.
Select the Clear all option, and then select the OK button on the Confirmation dialog box. If another window is active, for instance a program window or output window, hitting Alt-F10 will produce a pop-up menu dedicated to the active window.
To print your program, select Print on the File menu. To print an image of what is on the screen, including any windows that are open, hit the PrScr key (print screen). You should use this last technique to print your output, after you have made the output window cover the entire screen (click the green arrow for this). See page 138 of the textbook for another way of printing output.
2.11 REDIRECTING THE INPUT AND OUTPUT FILES
Its possible to substitute files for the standard input from the keyboard and output to the screen. This is called redirection of input and output. Redirecting input allows you write your data only once and use it for several versions of your program. On the other hand, if your output fills several screenfulls and you therefore can't print it using the PrtSc key, redirecting output allows you to print all your output on a file and then print the file. It also allows you to print your output on a file and use that file as input to another program.
Redirecting input/output can be done without changing your program if you are not using the CRT unit (if you are, please see the next section). First, you must compile your program to disk as described on page 92 of the textbook in Saving the Compiled Version of a program, by:
1. Selecting the Destination option on the Compile menu until Disk is displayed.
2. Selecting the Compile option on the Compile menu and your program will be saved as a file with the extension EXE on the disk selected. For instance if you call your program prog1, it will be saved as prog1.exe. To see which directory this file is saved, select Directories on the Options menu. To the right of EXE & TPU directory will be displayed the desired directory.
To redirect the input file:
1. Write your data using the IDE with each line of data written in the same way you normally would write data for you program. Then save it in a file with the extension txt, for instance, to differentiate it from a pas file. For example you might save your data as data1.txt
2. Save this data file by hitting the F2 key.
3. Exit from Turbo Pascal and at the DOS prompt write prog1 < data1.txt. The program prog1.exe will execute. Every time a readln is executed, it will read the data from data1.txt instead of from the keyboard.
To redirect the output file:
1. Write prog1 > out1 at the DOS prompt. All of you output will be written to the file out1 instead of to the printer. If you run the program again with, say, different input, the original contents of out1 will be erased and the new output printed to out1. Note that all the output, even the message you use to prompt the user will be written to the file. So you will have to remember what the prompts are in order to run your program.
To redirect both the input and output files:
1. Type prog1 < data1.txt > out1. Your program will then take its input from data1.txt and write its output to out1.
2.12 REDIRECTING THE INPUT AND OUTPUT FILES WHEN USING THE CRT UNIT.
If you are using the CRT unit in your program, you cannot redirect input unless you assign the null string to the input file and reset it as described on page 372 of the textbook. To redirect the output file, write