Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Comments in a program

Comments 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 }


Here is another similar program which asks for two numbers and then multiplies them together.

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 }


I hope at this point Sandra won't mind me getting technical. I've put it off as long as I can.

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.


Figure 1 - Variables As Boxes


Now in program, clock the three variables contained in the VAR list all have the same type, that is INTEGER. So we can think of this as the program creating three boxes of type INTEGER, which have the names; minutes_in, minutes, and hours. Once these variables are declared in the VAR list, the program can put any legal value into them (Pascal is very picky, about what is legal and what isn't), if you try to put a letter for instance, into an INTEGER variable it won't like it, and the program will crash, writing a run-time-error to the screen. Later on you might like to try this, your program will crash, but it will do no harm to either your program or the computer. So back to program clock, now we have a VAR list set up with the required data types for this program. We can now move onto the body of the program.



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);


In the program the line:

WRITE('Enter total number of minutes : ');


serves only to produce a user prompt. This tells the user that some input is required from the keyboard, and should give an indication of the type of input which is required. Next, the line

READLN(minutes_in);


could be read as `put the value typed in at the keyboard into the INTEGER box marked minutes_in', since that is pretty much the job it does.


Date: 2016-03-03; view: 739


<== previous page | next page ==>
The Elements of a Program | Field Width Specifiers
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)