Industry-Standard Programming For Beginners: Lesson 4 (A Simple Program III)

Having to change the program whenever a different time is to be entered is tedious, and there is a much better way to organise things – by making timetaken a variable, and by supplying a value for it as data. In this way, the program can remain the same, and won’t need to be recompiled each time. The data is completely distinct from the program, and depending on the compiler you are using, may be entered either via the keyboard or a data file. Whichever you choose, the data can be input into the program using the read statement. This principal is illustrated in the example program below, in which two values are read in as data:Program SimpleInterest ( input, output );{********************************************************************Example Program 2Calculation of simple interest on a principal at a given of interestand over a given number of days.*******************************************************************}constrate = 10; { percent }year = 365; { days }varprincipal, interest, amount: real;time: integer;beginread ( principal, time );interest:= time / year * rate / 100 * principalamount:= principal + interestwriteln ( ‘Example Program 2: Computation of simple interest’ );writeln ( ‘=================================================’ );writeln;writeln ( ‘principal = $’, principal:7:2 );writeln ( ‘interest rate = ‘, rate:4, ‘ %’ );writeln ( ‘term = ‘, time:4, ‘ days’ );writeln;writeln ( ‘interest = $’, interest:7:2 );writeln ( ‘new value of principal = $’, amount:7:2 );end.Before we look at the purpose of the program, try comparing its general format with that of Example Program 1. Again, there is an initial comment; but this time, in addition, there are further comments interspersed with the code. Comments may appear anywhere, except in a quoted piece of text, or in the middle of a multi-character symbol (multi-character symbols include names, numbers and the assignment operated ‘:=’). Apart from the comments, the general structure is the same as that of Example Program 1. There is a program heading, followed by a series of declarations, i.e. lists of constant variable names to be used. (Comparing a computer program with a recipe, the declarations can be likened to a list of ingredients preceding the instructions.) Then comes a set of statements to be executed, enclosed between the words begin and end – the statement part. All Pascal programs have these three parts: heading, declarations, and statement part, and all are terminated by ‘.’. The section of the program consisting of the declarations and statement part is known as the program body, or block.Certain words occur in both Example Program 1 and 2. These include program, var, begin and end, which always have a special meaning in Pascal. These are known as reserved words. In this program, they have been highlighted by bold lettering, and if you are lucky, your compiler will format your code in the same way for you, automatically.The title and names used to identify quantities within a program are invented by the programmer. Any suitable names may be chosen, with the exception of reserved words. Formally, a name (or ‘identifier’) in a program may consist of any sequence of letters or digits, as long as it begins with a letter.The purpose of Example Program 2 is to calculate the interest paid on a sum of money over a given number of days, with a given rate of interest.Now that we’re aware of how the two programs are similar, and we have a better understanding of how a program should be constructed, we will move on to how Example Program 2 is so different (and so much more powerful) than Example Program 1, in the next lesson.