Ed 060 620 Author Title Institution Spons Agency Pub Date
Total Page:16
File Type:pdf, Size:1020Kb
DOCUMENT RESUME ED 060 620 EM 009 626 AUTHOR Bork, Alfred M. TITLE Introduction to Computer Programming Languages. INSTITUTION California Univ., Irvine. Physics Computer Development Project. SPONS AGENCY National Science Foundation, Washington, D-C. PUB DATE Dec 71 NOTE 5p. JOURNAL CIT JCST; P 12-16 December 1971 EDRS PRICE MF-$0.65 HC-$3.29 DESCRIPTORS Computer Assisted Instruction; Computer Science Education; *Guides; *Programing; *Programing Languages ABSTRACT A brief introduction to computer programing explains the basic grammar of ccmputer language as well as fundamental computer techniques. What constitutes a computer program is made clear, then three simple kinds of statements basic to the computational computer are defined: assignment statements, input-output statements, and branching statements. A short description of several available computer languages is given along with an explanation of how the newcomer would make use of basic computer software. Finally, five different versions of a simple program (for solving the harmonic oscillator numerically) are given with comparison. (RB) 74.-E U.S. DEPARTMENT OF HEALTH. F749 EDUCATION & WELFARE erP\ e-"ri". "79' - A ott, Tritl OFFICE OF EDUCATION t,.4 taicta- THIS DOCUMENT HAS BEEN REPRO- DUCED EXACTLY AS RECEIVED FROM THE PERSON OR ORGANIZATION ORIG- '14r- C-1"?Fig ErtrIgf7t Or>re!9,1 2 INATING IT. POINTS OF VIEW OR OPIN- 616 ti LiS kC..".4 .2) szyj /ONS STATED DO NOT NECESSARILY cp-S.e REPRESENT OFFICIAL OFFICE OF EDU- CATION POSITION OR POLICY. By Affred M. Bork r,9 he digital computer is a powerful, calculating. andwhat the programmer wants it to do. For convenience, L logical device. In order to take advantage of its capa-we can distinguish three of theseassignment, input- bilities. the user must know how to instruct the computeroutput, and branching. This does not, by any means, in a language that it understands.Different computersexhaust everything that a computer can do, but itis have difierent languages available to them, and even forenough to get started. the same computer, the language situation is likely to change. Assignment Statements First, since the computer is a calcuiational tool,itis not surprising that some A Program Before reviewing the -types of state-statements in the computer language are designed to tell ments used in computer programming, it might be usefulthe computer just what calculations the programmer to consider the overall question of what constitutes awants it to perform. A standard shorthand language de- computer program, Lee the instruction given to a com-scribing calculations algebraically already exists. A modi- puter. The computer program can be viewed as some-fication of this language, in one form or another, is used thing like a paragraph. composed of a number of -sen-in most contemporary computer languages. The basis for tences.-These individual sentences are executed in thedescribing a calculation in algebraic terms is a formula. order in which they appear in the paragraish, unless -asuch as -branching- sentence explicitly changes this order and F==MA directs the machine to go to another sentence.In some languages every sentence is numbered. and these numberswhich says that to determine the number F. one mast determine the order of execution except for .branchingmultiply the numbers M and A together. Similarly. statements. X = A sin (wt) Usually the computer offers some easy way of changing individual sentences, so that the program can be modifiedspecifies a more elaborate procedure for finding X from A, and corrected for future use.It is very important to keepw. and t. in mind the sequential nature of the calculationthe fact A formula with only a single unknown on the left that the sentences are cxecuted in order. For examnle, ifhand side, and a group of items describing how to calcu- a new value of some variable is calculated in one sentence,late that unknown on the right hand side, is characteristic that value will be used ir: later sentences which refer toof a typical computer assignment statement.Here are the same variable. several examples. with indications of the language. The flow of a computer program can be studied by means of a chart or diagram, sometimes called a flowchart, 2 0 V=VO+A*T (BASIC) and books cn computer languages will show many flow- P=-----N*R*T/V (FORTRAN or PL/l) charts.Although there are some conventions for flow- 171 A4B*(C±D) (APL) charts, the beginner does not really need to worry about them.It does sometimes help to think about a programAn assignment statement is somewhat different from a in a chart form, however, because it gives an overallformula, and some languages (e.g.. APL) replace the equal -picture- of what is happening in an easy-to-comprehend,sign with some other sign. suCh as a left-pointing arrow. graphic form. A formula does not. by itself, indicate that a calculation All computer languages intended peirnarily for calcu-must be carried out but ,merely states the relation among lation purposes have similar features and structures. Pre-the variables.Furthermore. a formula can have many paring a beginning program in any of these languagesvariables on both sides of the equation.However, the (e.g.,the type of program that might be needed informulas which lead to assignment statements in a corn- physics';is a similar process.Simple calculational corn-puter language are all ones with a single variable on the N,S3 purer programming involves a limited number of activi-left: they specify a collection oi* arithmetic operations as cl- ties. or kinds of statements. directing the computer to doindicated on the right to be carried out and assign that Dr. Bork is professor of physics andofinformation and computer science ofThe University of California, Irvine. i2 JCST value to whatever variable is indicated on the left. Mostfree choice of variable name, it is easy to see why explicit languages allow free use of spaces in order to make for-multiplication is required. Many forms of BASIC restrict mulas more readable. variables to single letters, or single letters followed by The computer treats the variables as storage slots innumbers. The variables, then, can have convenient names its memory. Corresponding to each .variable in a par-which remind the programmer of the intended meaning ticular program, an appropriate storage location exists. inor use. the computer memory. The memory can be thought of All the computational processes in a program can be as a collection of maif boxes. with the labels that identify described by series of assignment statements.These some of the mail boxes being the same labels which iden-statements are executed one by one, in sequence. A com- tify the variables of tbe problem. Thus, in the relationputer program is executed in the order of the statements that are given to the computer: although, as we will later 7.42 F = M*A (Joss) see, exceptions to this do exist.As each statement is executed. it uses the values currently stored in the memory in a computer program. the computer is being told to findslots.Thus, if a PL/1 program contains these two the numbers currently stored in the two locations desig-statements, nated m and A. to multiply these, and to store the results DISTACC*TIME*TIME in the location called F. \Vhatever number was previously stored in the location F will be changed in this operation: Q=TIME*DIST/ (DIST** 3. 7 ) but the numbers stored in locations m and A will not be.one following the other, the value of DIST calculated in (The process of how the computer associates variablesthe first statement will be used in the second statement. with locations in memory isa bit more complicated thanThe same value of TIME will be used in both cakulations. indicated here, but this description illustrates what isA corollary is that a variable must be defined, or calcu- happening.) lated, in a program before it is used in another calculation. The variable on the left can also occur on the rignt.If it is used without previous definition, the results may Thus the statement in an APL program be unpredictable. Some languages will alert the program- mer when this is happening, but not all have this capa- COUNTECOUNT + 1 bility. increases the value of the count by one. The assignment statement, with slight variants in dif- input-Output Statements The second type of fun- ferent languages, is one of three building blocks in com-damental statement in a computer program is the input/ puter programs.Most computer languages allow theoutput statement, used when it becomes necessary to enter standard arithmetic operations of addition. subtraction.information into-the computer while the program is run- multiplication, division, and exponentiation. Most usening or when it is necessary to run the program with an asterisk for multiplication, although APL uses thedifferent values. The programmer will almost certainly usual multiplication sign.All computer languages inwant to know the results of the calculations whichthe common use requ:re an explicit multiplication sign; itcomputer has been performing. The assignment state- cannot be omitted as in algebra. Thus: ments only store the results of the calculation internally: they do not give access to those results. The programmer A<--B (C+D) (APL) must tell the computer explici:ly when he wants-input and output. All computer languages have either simple would be an erroneous statement. because the multiplica-input statements that allow the programmer to enter tion sign following the B has been omitted. values and store them in some of the memory slots Computational computer languages have a symbol forpreliminary to doing a calculation. or they have statements exponentiation, that is, raising a number to a power.