Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
The very simplest kinds of computer program are introduced in this chapter. Even so there is much to learn—the basic structure of Pascal programs, how to feed in data and layout results, but most of all how to get the computer to do what you want it to and find out what’s going wrong when it doesn’t.
Computers, like the Delphic Oracle, are used to answer questions, but, as with the Oracle, you have to be careful how you phrase your request or you may get a misleading answer. In fact the computer may give you no answer at all if it doesn’t like the question.
The first question we shall ask the computer is: ‘given that a stone takes 3 seconds to reach the bottom of a well how deep is the well?’ It would be nice if we could phrase the question exactly in this way and expect the computer to get the right answer. It will be a few years before that becomes possible. In the meantime the best way to get the computer to provide an answer is to write a computer program to do the job. Program 1.1 does just this. Before we examine the program in detail have a preliminary look at it to see if you can work out what’s happening.
Program 1.1
Ignoring much of the punctuation for now let’s look at the program line by line. (Note that the line numbers that appear on the left-hand side are for reference only and are not part of the program.)
On the first line the program is given a title. The user chooses an appropriate name but, as with all Pascal names, there must be no break in the middle—Well depth would not work. The name output in parentheses, signifies that the answer that the computer gives should be transmitted to the device called output. This might be a printer*, vdu* or teletype*. Lines 3 to 10 contain a description of the purpose of the program. This description enclosed between braces ‘{’ and ‘}’ is known as a comment. In all programs, whatever the language, it is important to have a piece of text at or near the beginning describing the purpose of the program and, possibly, giving other information such as the name of the writer and the date of completion. In Pascal, any information enclosed between ‘{’ and ‘}’ is disregarded by the compiler* (even if it extends over several lines) and so explanatory text may be inserted at appropriate points#. In this example the rectangle of asterisks has no significance for the computer but helps to highlight the enclosed introductory description for the reader.
A blank line follows, and then, on lines 13 and 14, the constants to be used in the program are given values. In order to work out the depth of the well the computer needs to know how long a stone takes to reach the bottom and also how fast it is moving. The time taken was 3 seconds and a name timetaken has been invented to represent this. Note, again, that it would not be acceptable to have a name in two parts (time taken). There must be no spaces in the middle. The second constant 9.8 is given the name gravity. The stone actually falls because of the gravitational pull of the earth and if we are to determine the depth of the well this will be needed too.
After the next blank line, the word var (short for ‘variable’) indicates that the name following it denotes a variable in the program. The word that follows is depth and represents the depth of the well. At this point you may be wondering why depth is called a variable giving the impression that it is something that alters. Surely the depth remains unchanged in the same way that the time taken and the gravitational constant do? The distinction occurs because we know the values of the constants before we start but the value of the depth is to be calculated in the program itself. In Pascal any names used to denote values that are computed during the running of a program are designated as variables.
The fact that the depth will be a number which will probably contain a fractional part is indicated by placing the word real after the variable name.
None of the code up to line 17 actually commands the computer to carry out the task we are interested in. However, the directions on lines 12 to 17 are important to the organisation of information within the computer and to the execution of the code contained between the words begin and end in the ‘statement part’.
The code on line 21 computes the depth from the given information using a formula. The order of the elements on the line is misleading since it does not correspond to the order in which the computations are performed. First, the formula to the right of the ‘: = ‘is evaluated using the values defined earlier in the program (note that multiplication is denoted by an asterisk and not by the more usual ‘×’ symbol and that sqr indicates that the squaring operation is to be performed). The result is then assigned to the variable depth on the left-hand side. The assignment operator ‘: = ‘may be read as ‘becomes’ or ‘takes the value’.
The complete instruction on this line is called an assignment statement and tells the computer to halve the value of the gravitational constant, multiply this by the square of the time and assign the result to the variable called depth. When this instruction is executed the value of depth becomes approximately 44.1 (the result of 0.5 × 9.8 × 3 × 3)—this will be the computer’s estimate of the depth of the well in metres. The actual precision of the result will vary from computer to computer. Note that the symbol ‘=’ is not used for assignment as it was for giving values to constants. The link between a variable and its value is a looser attachment and we will return to this point later.
Even though the result has been computed, this is not the end of the problem: the value obtained will still be inside the computer. In order to inform the user of the result, the value must be transmitted to the output unit* and printed. The instructions on lines 23 to 29 tell the computer to send the result and other information to the printer.
When a writeln statement is carried out it has the effect of sending specified information to one line of the output device. The statement on line 23 has the effect of printing one item—a piece of text that is to be the title of the output. The second writeln causes another piece of text to be output on the next line, underlining the title. The word writeln on line 25 has no items in parentheses following it and simply causes a blank line to be output.
The statement on line 27 indicates that three items are to be output—a piece of text between quotation marks (quotes), the value of the constant timetaken and then another piece of text. If an item is not in quotes then its value is output rather than the actual word itself. The last two writeln statements will similarly cause three items to be printed—two pieces of text and a value.
Now you know what the program does but you may be wondering why it is written in this elaborate way. The result could be worked out with a calculator in much less time than it takes to read the program and certainly much less time than it takes to run it. There are two answers to this. Firstly, the program is purely illustrative and is included to show what a simple one looks like. Secondly, as we shall see later, the program needs only a slight modification to make it much more powerful.
Even for the task that it does you may feel that the program is too verbose. Certainly it could be shortened in several ways so let’s examine each of these possible short cuts in turn. Before we do this though, let’s be clear what the purpose of a program is. Principally it is for getting the computer to solve a problem. A small program may only take a few minutes to write but as you become more ambitious you may write programs that have hundreds or even thousands of lines and take several weeks or months to complete. It is quite likely that unless your program is carefully written and has adequate documentation* you will, over a period of time, forget what different parts of it are supposed to do and changes will therefore be very difficult to make. If you write software for a living then you will probably be writing programs that others have to amend and also modifying other people’s programs. For these reasons it is important that as you write your program you spend as much time as possible making it easy to follow.
The first way the program could be shortened would be to remove those comments enclosed between ‘{’ and ‘}’. Hopefully you will agree now that this is not a good idea. They don’t make the program any less efficient and are of great assistance in helping the reader to find out what the program is supposed to do and in explaining the purpose of the names used.
What about those names though? Surely it would be possible to get rid of most of them. The assignment statement could then be abbreviated to
or (since everyone knows what 3 × 3 gives)
The usefulness of constant names like gravity and timetaken is not so obvious as that of the comments. In fact, in part, they fulfil a similar purpose,...
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.
Dateiformat: PDFKopierschutz: Adobe-DRM (Digital Rights Management)
Das Dateiformat PDF zeigt auf jeder Hardware eine Buchseite stets identisch an. Daher ist eine PDF auch für ein komplexes Layout geeignet, wie es bei Lehr- und Fachbüchern verwendet wird (Bilder, Tabellen, Spalten, Fußnoten). Bei kleinen Displays von E-Readern oder Smartphones sind PDF leider eher nervig, weil zu viel Scrollen notwendig ist. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.
Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Dateiformat: ePUBKopierschutz: Wasserzeichen-DRM (Digital Rights Management)
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet - also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Wasserzeichen-DRM wird hier ein „weicher” Kopierschutz verwendet. Daher ist technisch zwar alles möglich – sogar eine unzulässige Weitergabe. Aber an sichtbaren und unsichtbaren Stellen wird der Käufer des E-Books als Wasserzeichen hinterlegt, sodass im Falle eines Missbrauchs die Spur zurückverfolgt werden kann.