![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
Comments in a programComments can, and should be, included within the source code. They should be contained within '{' (left hand), and '}' (right hand), or between '(*' (left hand), and '*)' (right hand). They will be ignored by the compiler and will not appear on the screen during the program run time. They serve to give an indication to the reader as to the purpose of the program, and any other additional information the programmer thinks useful. This is good practice. Also, if a part of your program is not behaving itself, you can comment out that piece of code, by placing a '{' or a '(*' on the line above the piece of code which is giving you trouble and a '}' or a '*)' on the line below, the compiler will then ignore all the code between these two curly brackets, this is very useful in debugging programs. This next program is very like program add, but it subtracts one number from the other. Program 2 PROGRAM subtract(INPUT,OUTPUT); { This program asks for two numbers then subtracts one from the other } 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 subtract }
Program 3 PROGRAM multiply(INPUT,OUTPUT); { This program asks for two numbers then multiplies 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 multiply }The next program is a little more complicated. In this program we use the INTEGER division operators DIV and MOD, but more about them soon. The program prompts the user for an INTEGER value called minutes_in, which is equal to the total number of minutes and the program calculates how many hours and minutes that would be. Program 4 PROGRAM clock(INPUT,OUTPUT); { this program reads in an integer and calculates the total number of hours and minutes } VAR minutes_in, minutes, hours : INTEGER; BEGIN { body }{ 1 } WRITE('Enter total number of minutes : ');{ 2 } READLN(minutes_in); { 3 } hours := minutes_in DIV 60;{ 4 } minutes := minutes_in MOD 60; { 5 } WRITELN('The hours are: ',hours:2);{ 6 } WRITELN('The minutes are: ',minutes:2); END. { body }
3.2. The VAR, how does it work? Each identifier in the VAR list is assigned a space in the computer memory, into which a value can be assigned during the running of the program. This space can be thought of abstractly as a container. These containers must have a type and for this example we can think of the type INTEGER as a kind of box.
The Program Body Pascal syntax requires the body to be begun with the reserved word BEGIN and ended with the reserved word END, notice that a full stop is required following the END. This marks the end of a Pascal program. Layout, and Syntax Notice, that in the VAR list each VAR is followed by a comma, and that a colon separates the list from its type definition, and the completed list is followed by a semi-colon. Also in the body of the program, that each statement is also separated by a semi-colon. Notice also that items contained within the WRITE, WRITELN, READ, READLN are separated by commas. READLN(item_one,item_two); WRITELN('The first item is ',item_one,' The second is ',item_two);
Date: 2016-03-03; view: 903
|