How to save and compile or run your program

You shouldn't be afraid of Pascal. It is one of the easy-to-learn programming languages. In order to be able to write efficient and easy-to-debug (debug = trace a program in order to find bugs, which could make your program run false), you should first of all plan your solution, as if you were trying to solve it without programming. After having done this, and you are sure that it will work, go on to write your program onto paper. This is just for the beginning, you won't need it when you are able to write hundreds of lines of code. You shouldn't go over to the computer and begin writng your code it unless you are sure that your strategy works well and there is no other, perhaps much better solution to the program. Take a look at this schema describing the best process to develop a program:

Diagram

There are two methods used to run any computer program that is written in a readable form of English. The first method is to use an interpreter. An interpreter is a program that looks at each line of the "English" program, decides what the "English" on that line means, and does what it says to do. If one of the lines is executed repeatedly, it must be scanned and analyzed each time, greatly slowing down the solution of the problem at hand. A compiler, on the other hand, is a program that looks at each "English" statement one time and converts it into a code that the computer understands directly. When the compiled program is actually run, the computer does not have to figure out what each statement means, it is already in a form that the computer can run directly, resulting in much faster execution of the program.

Let's come to the way you can write, edit and execute your pascal program. In order for this to be possible, you have to have a Pascal compiler or a Pascal IDE. Assuming that you have the compiler already installed, what you should do at the very beginning is to write a program. If you have installed Turbo Pascal in the directory \tp\bin\, you should change to that directory (by writing cd \tp\bin\) and write turbo. Turbo Pascal will start. There, you should press Alt+F simultaneous to arrive at the File menu. Now, press Enter if highlighted. You now have an empty Window, where you can enter your code."New" is

The minimum pascal program is :

program First(input,output);
begin
end.
Without these 3 lines, your program wouldn't work. The only thing you are allowed to change here is the phrase "First". Instead you could enter anything you like, but stick to the alphabet and don't use any other character.

The general pascal program structure is:

1. program program_name(input,output);
2 const
3 constname=const_value;
4. type
5. typename=type_name;
6. var
7. varname:variable_type;
8. begin
9. instruction1;
10. instruction2; {This is a instruction}
11. instructionN; (*This is also a instruction*)
12. end.

A program consists of two parts: A declaration part and a program body. The declaration part tells the compiler what memory cells are needed in the program. The program body contains the statements which are to be executed.

Lets look at each single line:

The first line gives the program name, as obvious. The program name should have the same format as identifiers. No underscores and special characters should be used, stick to letters and digits with the first character being a letter.

The second line begins the type declaration part. This is explained later in the type section. It is first line of the declaration part.

The fourth line begins the constant declaration part. A constant declaration reserves a memory cell with the name of the constant and the value you specify. You don't change the value of a constant. For more information about it see the constant section.

The sixth line begins the variable declaration part.A variable declaration also reserves a memory cell with the name of the variable, with one exception that it also stores the type of the variable but no initial value. The value of the variable can be changed throughout the program. You can use the variable instead of values which change frequently. So you wouldn't have to write it manually everywhere when it changes. For example, if you have a variable which computes your salary according to the hours you worked you can enter the number of hours you worked and get the amount of the salary and use it in the further part of your program. So you don't have to calculate your salary yourself and enter. For more informaton about it see the Identifiers and Basic Types section.

The eight line begins the program body part. Here you begin to write your program which you have worked on. These are the instructions. If you want to enter comments to your lines, then you should use bracklets '{' not a ordinary parenthesis or a parenthesis with a star '(*' after it. You also have to close your comment parts with a '}' or '*)'. It is not important that the comment ends on the same line as it begins. Pascal does not take care whether you write your instruction in one line or not, it has a free format, but if you obey to the indentation used in the examples you have a much more readable code. With the 12. line the program ends.

Saving your Work

You should save your work in order not to have to write it once again. For this, you press Alt+F, and then S. A dialog appears where you can enter the name of your choice and press Enter to confirm.If you want to save your work on a floppy, then you should write a:\progname. That is important, because otherwise you save your work on the harddrive.

You now are ready to compile and run your first program! Just press Ctrl+F9 simultaneously and it runs. Of course it does nothing, yet, but we will add to it. If you press Alt+F9 simultaneously, then you just compile your program but you don't run it.

A First Look A First Look at Pascal Basic Input/Output Identifiers & Types
Constants Standard Functions If..Then Statement Case Statement
Repetitions Procedures and Functions Enumerated /Subrange Types Sets
I/O, Text Files Arrays Records Recursion
Pointer How to Debug Previous Exam Questions Advanced
ASCII Code Table Homepage University Homepage Computer Engineering Dept.