![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
The Elements of a ProgramElementary Pascal Programs
The parts of a program
The Program Title The program title, sometimes called the program heading follows the form: PROGRAM anyname(INPUT,OUTPUT);
The VAR list The VAR list contains a number of data items called variables. Each variable has a name, called its identifier. These identifiers are names chosen by the programmer to refer to these variables. These names can be almost anything, but they must begin with a letter. More on this later. These variables, are said to be declared when they are put into the VAR list. The word VAR is short for variable, and a variable is merely a storage device for values referred to, and manipulated by their identifier. In programs each of these variables must have a type. The elementary types in Pascal include
The body of a program The body of a Pascal program is all the text between the first BEGIN and the last END. - of course there can only be one END with a full-stop after it, and it must be the last one. This is the minimum body for a Pascal program: BEGIN END.The Elementary Pascal Types The type INTEGER Integers are the set of whole numbers, and include both negative and positive. In Pascal the size of the INTEGER value is limited. Usually, there is something along the line of the reserved words MAXINT (maximum integer) and MININT (minimum integer). The size of these are constrained by the components of each particular computer. This is what is called being machine dependent, on the Hewlett Packard workstation I use, they are equal to 2,147,483,647, and -2,147,483,648. To declare a variable of this type requires a piece of code similar to VAR student_date_of_birth : INTEGER;
Program 1 PROGRAM add(INPUT,OUTPUT); { This program asks for two numbers then adds them together } VAR first_integer, second_integer, answer : INTEGER; BEGIN WRITE('Enter an integer : '); READLN(first_integer); WRITE('Enter another integer : '); READLN(second_integer); answer := first_integer + second_integer; WRITELN('The answer = ',answer:1); END. { program add }
The type REAL Reals, are the floating point numbers (decimals) and scientific notation numbers (exponents). The numbers 3.14, -21.2, 15, 12.24321, -0.22311, 0 are all REAL numbers. The maximum and minimum REAL values are much larger than the integers, because of the exponential nature of REAL numbers, but there is a machine dependent limit on their size. It is similarly declared with a piece of code similar to: VAR student_account_bank_balance : REAL;
The type CHAR This type CHAR, is short for character, and the set of printable ASCII characters may be found in the ASCII table in Appendix I. These printable characters range from the character <space> to the character <~> (tilde) (the <space> has an ASCII value of 32, and the <~> (tilde) a value of 126). To use the type CHAR requires the lines VAR the_answer : CHAR;
The type STRING This type is a list of characters of up to the specified maximum size, and is declared with a piece of code similar to: VAR anyname : STRING[12];
The Elements of a Program Date: 2016-03-03; view: 1200
|