What are the disadvantages of a structured programming language


3. Programming languages ​​and basics of programming

3.1 programming languages

are used to create user programs. Necessary for individual problem solutions. Note: `` Programming '' is also more or less important in program packages such as dBase (own programming language!), Excel or Lotus 1-2-3 (formulas in the spreadsheet; macros) and even in word processing (Word for Windows: Word BASIC for macros).

Classification

Machine language:

lowest level, direct input of the machine code in numerical form (`` bit by bit ''). Disadvantages: very laborious, confusing, difficult to modify, totally machine-dependent.

Assembler (macro assembler):

serve for the more comfortable creation of programs in machine language. Instead of the numerical program code, the commands are entered as text, as a `` mnemonic '' code (e.g .:,,,,,). Assembler programs are clearer and easier to modify than the machine language itself. Advantages: Assembler programs require little memory space and work at the highest possible speed. Disadvantage: Totally dependent on the processor (the CPU) used, lengthy and difficult programming work, as each program step has to be entered individually. (Groups of program steps can be combined with a macro.)

Higher programming languages:

are (more or less) problem-oriented. They offer more powerful commands and functions than tens of lines of code in an assembly language program. Advantages: they can be learned relatively quickly and lead (with simple problems) quickly to the goal, they are (almost) machine-independent (portable). In many cases, help is provided for creating programs and for troubleshooting. Disadvantage: Program code and execution times are longer than with assembler programs. `` Interpreted '' programs (such as Interpreter-BASIC) are particularly slow.

Important higher programming languages ​​(for natural scientists)

FORTRAN (FORmula TRANslator), BASIC (Beginners' All-purpose Symbolic Instruction Code), PASCAL (named after the French mathematician Blaise Pascal, developed by Niklaus Wirth), C (developed by Kernighan & Ritchie from `` B '').

Others (only mentioned here): ADA (in the military sector), ALGOL (similar to PASCAL, out of date), APL (mathematics; matrix and vector calculation), ASYST (similar to FORTH; developed for the acquisition and processing of measurement data), COBOL (COmmon Business Oriented Language; in the office area), FORTH (stack-oriented, `` UPN '' as with HP pocket calculators, easy creation of your own commands), LISP (older language for expert systems), MODULA-2 (strictly modular structure, otherwise PASCAL -similar), PL / 1, Oberon (successor to Pascal or Modula), Prolog (for expert systems, `` artificial intelligence ''), Smalltalk (object-oriented language), ...

FORTRAN:

scientific programming language with a long tradition and very extensive program collections. Application to purely numerical problems simple and clear, but not suitable for sophisticated program constructions or for text manipulation. Another disadvantage: origin from the `` punch card age ''; Each program line consists of 80 columns, whereby the actual program text must be in columns 7-72. (FORTRAN IV = FORTRAN 66, FORTRAN V = FORTRAN 77; new: FORTRAN 90)

BASIC:

Available on most home and personal computers, easy to learn, and easy to use. Mostly available as an `` interpreted '' language, i.e. the commands are executed immediately without first being converted into a machine program. Advantage of this method: lower demands on the performance of the computer and very fast possibility for program changes; Disadvantage: program execution (very) slow. However, there are also `` compilers '' for BASIC (Quick BASIC, Power BASIC, Visual BASIC), which convert a BASIC program into machine language and thus generate `` fast '' programs. There are many BASIC dialects; old BASIC interpreters require line numbers and offer few options for clear, `` structured '' programming. These disadvantages have largely been eliminated with Quick BASIC and Turbo BASIC (Power BASIC), for example. Conclusion: Modern BASIC variants are well suited for scientific programming tasks of small and medium scope, they usually lead to the goal faster than other programming languages. For extensive, complicated tasks such as quantum mechanical calculations (except perhaps HMO), BASIC should not be considered.

PASCAL:

supports the principle of `` structured programming '', new commands can be easily created in the form of procedures (subroutines). All variables used must (in contrast to BASIC or FORTRAN) be `` declared '' beforehand. PASCAL is relatively easy to learn. Turbo-Pascal or Borland-Pascal (on PCs) is famous for its speed (in compilation and program execution). New versions also allow object-oriented programming. Disadvantages: limited mathematical possibilities (e.g. the exponentiation is not predefined!), Not very suitable for very extensive projects.

C:

Supports the principle of `` structured programming '' similar to PASCAL, but is considerably more powerful and also allows programming that is very close to the machine. C is ideally suited for programming very extensive and complex problems. The variant C ++ allows object-oriented programming. The disadvantage is that C is more difficult to learn than PASCAL, for example, and that C programs are difficult to `` read ''; Programming errors can easily lead to the `` crash '' of the program and even the system. Conclusion: C is a programming language for professionals.

3.2 Basics of programming

From source code to executable program

The creation of an application program basically takes place in the following steps: Problem analysis and preparation of a program flow chart, implementation in a programming language (coding), translation into an executable program (see below), finally testing of the program and, if necessary, debugging, syntactic elimination and logic error). As a programmer, you create a program in plain text, the source code, which first has to be translated into the executable program. Several steps are necessary for this:

  1. Compiler: source code -> assembler program
  2. Assembler: assembler program -> object code
  3. Linker: Object code (+ libraries) -> executable program

Interpreter: interpreted programs cannot run as such, but must be called via the interpreter (e.g. BASIC).

Number systems, information units

In the end, computers can only distinguish between the states `` off '' and `` on ''. Distinction between exactly two different options such as `` off '' or `` on '' or `` 0 '' or `` 1 '' is now the smallest unit of information, which is referred to as a bit. With the help of the digits 0 and 1, a number system can be set up analogous to the decimal system, in which all numbers can in principle also be represented. As with decimal numbers, you work with a position system, but each digit does not mean a power of ten, but a power of two. In addition to the decimal and dual systems, the octal (base 8) and hexadecimal system (base 16) are also important.

Number systems

Decimal Dual Octal Hexadecimal (Base 10) (Base 2) (Base 8) (Base 16)
0 0 0 0 1 1 1 1 2 10 2 2 3 11 3 3 4 100 4 4 5 101 5 5 6 110 6 6 7 111 7 7 8 1000 10 8 9 1001 11 9 10 1010 12 A 11 1011 13 B 12 1100 14 C 13 1101 15 D 14 1110 16 E 15 1111 17 F 16 10000 20 10

1 bit: 0 ... 1, smallest information unit; 1 byte = 8 bit: 0 ... 255, i.e. 2 ^ 8 possibilities; 1 KB = 2 ^ 10 bytes = 1024 bytes (!). One byte stores one letter (26 small, 26 large, plus umlauts, 10 digits, special characters, etc.). 1 computer word: 8, 16, 32 or 64 bit depending on the machine.

Storage capacities

1 A4 page (closely printed): 60 lines with 60 characters each, i.e. contains 3600 characters or 3600 bytes. 1 diskette (DS / DD) holds 360 KB (i.e. 100 A4 pages), 1 diskette (HD) holds 1.2 or 1.44 MB (i.e. 330 or 400 A4 pages), 1 (small) hard disk holds 200 MB (i.e. 55,000 A4 pages), 1 CD-ROM has a capacity of over 500 MB (i.e. 140,000 A4 pages).

internal number representation

Integers with 2 bytes = 16 bits, range -32768 ... 32767

Long integers with 4 bytes = 32 bits, range -2147483648 ... 2147483647

Floating point numbers (in the IEEE standard) with 4 bytes = 32 bits, range (+ -) 1.2 * 10 ^ -38 ... (+ -) 3.4 * 10 ^ 38, precision about 8 decimal places

Double precision floating point numbers with 8 bytes = 64 bits, range (+ -) 2.23 * 10 ^ -308 ... (+ -) 1.79 * 10 ^ 308, precision about 16 decimal places

(Floating point numbers are used internally as a mantissa + exponent in the dualsystem saved.)

Data types

Characters (letters, digits, special characters), character strings (text), whole numbers (integers), floating point numbers (real), logical quantities (`` true '' or `` untrue '') as well as compound data types (date, time; compounds, structures ).

Variable types

BASIC FORTRAN PASCAL C
Characters A $ character * 1 char char character string A $ character * (*) string [] char * integer A% integer * 2 integer int long integer A & integer * 4 longint long floating point number A! real * 4 single float double exact A # real * 8 double double logical - logical boolean -

Flow chart

A program flow chart can be shown particularly clearly in the form of a flow chart.

( BEGIN )

/ Input /

| Calculation rule |

/ Output (print) /

< Abfrage: Weiter? >

( STOP )

3.3 Programming in BASIC

Interpreter BASIC

for original IBM-PC / AT: BASIC and (better) BASICA (`` advanced ''), for compatible GWBASIC. Calling the interpreter (e.g.): BASIC. (If you want to run a finished BASIC program (.BAS) immediately, you can also use BASIC basicpro call.) Within the BASIC interpreter (exit: SYSTEM) you can use the Direct mode or in Program mode work. (a) Direct mode can be used like a pocket calculator to perform smaller calculations. To output the results do you need the PRINT command or equivalent? (Question mark), e.g .:

=> 3.14159 ... (pi)

(b) In program mode, each line must start with a Line number (0 ... 65535) can be initiated (possibly automatically with AUTO). Program start with RUN, abort with Ctrl + Break or Ctrl + C.

Example: Calculating the circumference and area of ​​a circle.


100 PI = 4 * ATN (1) 110 INPUT "Radius"; R 120 U = 2 * PI * R 130 F = PI * R * R 140 PRINT "RADIUS ="; R 150 PRINT "UMFANG ="; U 160 PRINT "AREA ="; F 170 END

Interpreter-BASIC: control commands

SYSTEM terminates BASIC, return to DOS RUN F2 starts the BASIC program (abort: Ctrl + C) LIST F1 outputs the BASIC program list LIST from-to list from line ... to line LLIST Print out the BASIC program list LOAD `` ... '' F3 loads a BASIC program SAVE `` ... '' F4 saves the BASIC program (coded!) SAVE `` ... '', A saves the BASIC program as plain text MERGE ``. .. '' Load a BASIC program (plain text only) EDIT no edit line no DELETE from-to delete from line ... to line NEW deletes the BASIC program CONT F5 continues the program execution RENUM re-numbers the BASIC program RENUM new, old, step (new line no., old line, step size) AUTO automatisc he generation of line numbers (abort: Ctrl + C) TRON F7 trace mode on (output of line numbers) TROFF F8 trace mode off

3.4 BASIC: most important commands

(a) Input (from the keyboard): INPUT

INPUT A INPUT A, B, C INPUT TEXT $ INPUT "Input x, y:", X, Y INPUT "Radius"; R '(the semicolon creates a question mark)

Note: several variables or inputs are indicated by Commas Cut.

(b) Output (on the screen): PRINT or? (Printer: LPRINT)

PRINT A PRINT A, B, C '(the comma acts as a tabulator) PRINT TEXT $ PRINT "Radius ="; R' (semicolon: link without tab) PRINT "Input:"; '(Semicolon at the end suppresses line feed)

(c) Comments: REM or '(apostrophe),

(d) Program interruption: STOP, end of program: END

(e) Jump instruction: GOTO line no.

GOTO 100 '(Interpreter-BASIC) GOTO MARKE' (modern compiler-BASIC) BRAND:

BASIC: math. Standard functions

BASIC math. Note _______________________________________________________________ SIN (X) sin x sine (argument in radians) COS (X) cos x cosine (argument in radians) TAN (X) tan x tangent (argument in radians) ATN (X) arctan x arc tangent (in radians ) SQR (X) sqrt x square root EXP (X) e ^ x exponential function LOG (X) ln x natural logarithm ABS (X) | x | Absolute amount INT (X) Integer <= x SGN (X) Sign (-1, 0 or 1) RND (1) Pseudo-random number (0 <= y <1)

BASIC: arithmet. Operators

^ Exponentiation * / multiplication, division + - addition, subtraction \ integer division (`` with remainder '') MOD modulo (`` remainder '' when dividing) = assignment

BASIC: comparison operators

= equal to greater than <= less than or equal to> = greater than or equal to <> not equal

conditional statements

Simple instructions are carried out `` absolutely ''. Any `` intelligent '' programming is based on the fact that the program sequence can be made dependent on the fulfillment of certain conditions. Simplest variant:

if conditions met then execute command [otherwise skip this command]

BASIC: IF condition THEN command

Variant with alternative decision:

if conditions met then Command 1 otherwise Command 2

BASIC: IF condition THEN command1 ELSE command2

grind

Problem: For many tasks it is necessary that certain parts of the program multiple times be processed. In principle (but clumsy and uneconomical) this problem can be solved with `` GOTO '' and `` IF ''. Standard-BASIC offers the following possibility (simplest variant):

FOR i = i1 TO i2 '(initialization and test) ' (repeated execution) NEXT i '(end of loop, incrementation of i)

Extension: selectable step size (STEP); negative also possible!

FOR i = i1 TO i2 STEP i3 '(initialization and test) ' (repeated execution) NEXT i '(end of loop, i = i + i3)

WHILE ... WEND loop:

WHILE '(as long as condition is met) ' (repeated execution) WEND '(end of loop)

DO ... LOOP UNTIL loop (only Turbo and Quick BASIC):

DO '(repeated execution) LOOP UNTIL ' (check only at the end of the loop)

In this case, the check only takes place at the end of the loop, so the loop is run through at least once. It can also be exited early with EXIT DO (Quick BASIC) or EXIT LOOP (Turbo BASIC).

Fields (arrays)

Problem: Saving of measurement data series, tables, vectors and matrices (very cumbersome with `` simple '' variables, not efficiently programmable). Solution: Fieldswhose elements have a index (or several indices) are addressable. Example:, A is the field name, I is the index.

Dimensioning of fields

Fields generally have to be dimensioned. This is necessary in order to reserve the required storage space and (in the case of multi-dimensional fields) to be able to calculate the element position from the index information:

Indexing usually starts at zero, so field A (100) contains 101 elements; the field C (3,3) contains 4 * 4 = 16 elements.

Attention: The dimensioning of fields (arrays) is necessary in all higher programming languages. There is an exception to this in BASIC: DIM X (10) is specified `` automatically '' (`` default ''), i.e. fields with a maximum index of 10 do not necessarily need to be declared (but you should do it anyway!) .

Initialization of variables

After the program start (RUN), all variables are automatically set to zero in BASIC. The same can be achieved with the BASIC command CLEAR. Attention: CLEAR deletes all variables and the dimensions of the fields!

Assignment of constants

(a) awkward:

A = 1.23 B = 2.34 C = 3.14

(b) more elegant with DATA and READ:

DATA 1.23,2.34,3.14 READ A, B, C

(The DATA statement can be reset with RESTORE)

Definition of new functions

(Definition)

(Call)

Example:

(decadic logarithm)

=> 0.30103

(The function name must begin with FN!)

Subroutines

serve for structuring of the program by taking on certain subtasks (e.g. diagonalizing a matrix, creating a diagram). They are especially useful when they multiple times be called because programming only once he follows.

Interpreter-BASIC only offers a limited option for working with subroutines: GOSUB ... RETURN.

100 110 GOSUB 2000 '(branching into the subroutine) 120 ' (the return takes place here) ... ... 1000 END 2000 '(start of the subroutine) 2010 .... .. RETURN '(end of subroutine, return to main program)

A subroutine is called with GOSUB line number. There is no special start command for the subroutine. The subroutine is ended with RETURN; There is a return to the main program and the instruction that follows `` GOSUB '' continues. Caution: All variables are in this type of subroutine global, have the same value as in the main program!

Working with files

In order to be able to read data from a floppy disk or hard disk (or to write data), the relevant file must first open (if necessary, newly created beforehand).

BASIC: Working with files

File operation BASIC command
open OPEN close CLOSE read INPUT LINE INPUT write PRINT WRITE

Each file becomes a Channel number assigned (similar to FORTRAN). Open files must be restored after the read / write operations are complete closed become. Caution: If a program is left `` roughly '' (especially by switching off the device, emergency exit with Ctrl + C or Alt + Ctrl + Del), this can lead to errors in the file system (=> CHKDSK) when files are being written were open!

In the (PC-Interpreter-) BASIC there are four different modes of file processing that have to be defined when opening:

OUTPUT - output only; old file content (if any) will be lost!

APPEND - output only; Append to existing data

INPUT - input only (read)

RANDOM - read and write access possible (organization in `` records '' and data fields)

(a) Reading from a file:

OPEN "scratch.dat" FOR INPUT AS # 1 '(channel no. 1) INPUT # 1, X' (analogous to input from the keyboard) INPUT # 1, Y LINE INPUT # 1, TEXT $ '(reads a whole line , also with commas) CLOSE # 1

(b) Writing to a file:

OPEN "scratch.dat" FOR OUTPUT AS # 3 '(channel no. 3) PRINT # 3, X PRINT # 3, TEXT $ CLOSE # 3

Difference between PRINT and WRITE:

PRINT # 3, 1,2,3 => 1 2 3

WRITE # 3, 1,2,3 => 1,2,3

With WRITE, the output data is passed through within one line Commas separated so that they can be properly read in again with INPUT. (In BASIC, the comma serves as a list separator for input fields, in Pascal or C, however, a space!)

Working with `` RANDOM '' files is more complicated. The file is divided into data records (RECORDS) with a defined length, which are numbered consecutively (starting from zero). FIELD defines the data record format, GET or PUT (in connection with LSET or RSET) reads or writes a data record.

3.5 Quick BASIC, Turbo BASIC

Call: QBASIC or TB.

Quick BASIC main menu and keyboard commands

(accessible with the mouse or with Alt)

File: New, open, save, save as, print, exit

To edit: Cut, Copy, Paste, Delete, New SUB, New FUNCTION

View: SUBs, splitting, output screen

Search: Search, search further, change

To run: Start, restart, continue

Debug: Single step, procedure step, backtracking on, breakpoint on / off, delete all breakpoints, define next instruction

Options: Check screen display, path for help, syntax

Help: Index, Contents, Topic, Using Help, Info

Keyboard commands: F1: Help on keyword, Shift-F1: General help, F2: SUBs, F3: Find next, F4: Output screen, F5: Execute / Next, Shift-F5: Execute / Start, F6: Window, F7: Execute to cursor, F8: Single step, F9: Breakpoint, F10: Procedure step, Alt: Menu, Esc: Cancel, Shift-Del: Cut, Ctrl-Ins: Copy, Shift-Ins: Paste, Del: Delete, Ctrl-Y: Delete line

Special features of Quick Basic (Power Basic, Turbo Basic)

This is a modern compiler BASIC with Pascal-like language elements. Note: Interpreter basic programs can (generally) also be compiled with QBASIC or TB without changes. For this, the basic program must be available as an ASCII file (SAVE `` PROG '', A). Line numbers are not necessary in QBASIC or TB, but are allowed. When working without line numbers, the commands GOTO and GOSUB (which it is better to avoid here) must be given a jump label:

GOTO brand ... brand:

Jump labels with an appended colon (:) must stand individually on one line.

The number type `` double precision '' (variable #) corresponds to the IEEE standard (in contrast to the interpreter basic).

Block structures

Program blocks, e.g. conditional statements (IF ... THEN ... ELSE) or loops, often consist of several individual statements. The programming of multi-line blocks is supported by QBASIC, PB and TB in:

  • conditional statements: IF ... THEN ... ELSE ... END IF
  • Selection blocks: SELECT CASE ... CASE ... END SELECT
  • Loops: DO ... LOOP
  • Subroutines: SUB ... END SUB
  • Functions: FUNCTION fct () ... END FUNCTION

Block IF

IF THEN 'THEN is at the end of the line! ELSEIF THEN 'ELSEIF is a word! ELSE 'if everything else does not apply END IF' END IF are two words!

The blocks ELSEIF (possibly several) and ELSE (logically after all ELSEIFs) are optional. Note the different spelling of ELSEIF and END IF. If necessary, the block IF structure can be left prematurely with EXIT IF.

Selection block (SELECT)

A multiple selection can be programmed with the help of IF queries. But you can work more elegantly with the SELECT statement (query of integers or character strings):

SELECT CASE var% CASE 1 'IF var% = 1 THEN ... CASE 2: ' IF var% = 2 THEN ... CASE 3: 'IF var% = 3 THEN .. CASE ELSE: 'ELSE ... END SELECT

(The colon is used to separate several commands that are on one line.)

Loop (LOOP)

DO IF THEN EXIT DO 'optional LOOP UNTIL

The LOOP loop can be left prematurely with EXIT DO (Quick Basic; Turbo Basic: EXIT LOOP).

Subroutines (SUB)

In addition to the conventional GOSUB ... RETURN construction, Quick Basic and Turbo Basic allow working with real Subroutines (SUB). Use real subroutines local Variables that only apply within the subroutine.

There are two options for exchanging data with the main program:

(a) Parameter transfer

In the example there are three transfer parameters (par1, par2, par3). The rules correspond to those of FORTRAN, so that data exchange is possible in both directions (when using variable names); (numerical) values ​​can also be transferred (of course only to the subroutine).

(b) Declaration of `` common '' variables with SHARED (similar to COMMON in FORTRAN).

Definition of subroutines:

SUB unterprog (par1, par2, par3) 'dummy parameters SHARED var1, var2, field ()' common variables ... END SUB

The `` dummy '' parameters (transfer parameters), local and common variables can be used within the subroutine.

Call:


Subroutines can also be parameter-free (SUB noparam ... END SUB, call: CALL noparam). Advantages of the `` SUB '' subroutines: with Names addressable, simple value transfer [e.g. CALL proc (27.3)], implementation of the same procedures with different variables (also fields!), avoidance of side effects due to inadvertent use of global variables.

`` SUB '' subroutines can be exited prematurely with EXIT SUB.

Block functions

Definition (QBASIC):

FUNCTION xyz (par1, par2, par3) xyz = expression 'Value assignment! END FUNCTION

The function must be assigned a value once! The function definition can be left prematurely with EXIT FUNCTION.

Call:

3.6 Turbo Pascal

Call: TURBO.

Sample Pascal program with explanation:


PROGRAM test; {Start of the PROGRAM} VAR {all variables must be declared} input: String; {`input 'is a string} i, j, k: Integer; {i, j, k are integer variables} BEGIN {BEGIN of the command block} Readln (input); {read a line from the console} Writeln ('your text:', input); {output one line} Write ('Enter 2 summands:'); Readln (i, j); k: = i + j; Writeln ('Sum:', k); Readln; END. {END of the program}

Remarks:

  1. All variables must be in Pascal declared become.
  2. Each command ends with a semicolon (;).
  3. The assignment symbol is: =
  4. Strings are enclosed by apostrophes ('...') (not by quotation marks!)
  5. Each block (group of commands) is enclosed between BEGIN and END.
  6. The last END is followed by a period (END.)
  7. Comments are enclosed in {curly brackets}
  8. No distinction is made between upper and lower case (except within character strings).

3.7 Bibliography for programming

K. Ebert, H. Ederer: `` Computer applications in chemistry '', VCH, Weinheim 1983; 2nd edition 1985 (examples in Interpreter-BASIC)

M. D. Johnston, Jr .: `` Computational Chemistry '', Elsevier, Amsterdam 1988. (Examples in Interpreter and Quick BASIC)

P. C. Jurs: `` Computer Software Applications in Chemistry '', Wiley, New York 1986. (Examples in FORTRAN)

W. H. Press, B. P. Flannery, S. A. Teukolsky, W. T. Vetterling: `` Numerical Recipes '', Cambridge University Press, Cambridge 1986. (Comprehensive collection of programs for standard problems in numerical mathematics. Versions for FORTRAN, PASCAL or C available)

J. Hückstädt: `` Programming with Turbo-Basic '', Markt & Technik, Haar near Munich, 1987

W. Kassera, V. Kassera: `` Programming with Turbo-Pascal 5.5 '', Markt & Technik, Haar near Munich, 1989