Control structure

from Wikipedia, the free encyclopedia

Control structures ( control constructs ) are instructions in imperative programming languages . They are used to control the flow of a computer program. A control structure is either a branch or a loop . Most of their execution is influenced by logical expressions of Boolean algebra . Control structures can be visualized using special diagrams (e.g. Nassi-Shneiderman diagrams ).

history

In the early 1960s, flowcharts and jump instructions were common in programs, which made larger programs almost unmaintainable because they quickly became unmanageable. In 1968 Edsger W. Dijkstra spoke out in his essay Go To Statement Considered Harmful (the title, however, goes back to Niklaus Wirth ), in favor of an abolition of the GOTO command in all higher programming languages.

In May 1966 Böhm and Jacopini published an article in the journal Communications of the ACM in which they showed that any program that contains goto instructions can be rewritten into a goto-free program that can only be branched (IF THEN ELSE ) and a loop (WHILE condition DO statement ) works, possibly with the help of some code duplication and the introduction of Boolean variables (true / false).

At the beginning of the 1970s, this approach of self-restriction to a few, typical elements began. The work of Niklaus Wirth with his programming language Pascal was groundbreaking .

Concept based on an example

Without control structures, repetitions and conditions must be implemented through jumps (GOTO, IF ... GOTO - English : jump to, If ... jump to). The output of the numbers from 1 to 100, but without the range 40 to 60, is implemented without a control structure ( pseudocode , based on BASIC )

     I := 1
 M1: PRINT I                  -- Ausgabe von I
     IF I=39 THEN GOTO M2     -- Wenn ... springen (bedingte Ausführung)
     I := I+1
     IF I<=100 THEN GOTO M1   -- Wenn ... springen (wiederholte Ausführung)
     END                      -- Programmende
 M2: I := 61
     GOTO M1                  -- Sprung nach M1
Unstructured program: Output of the numbers 1, 2, ..., 39, 61, 62, 63, ..., 99, 100

The use of control structures is also part of structured programming . Each control structure consists of at least a start and mostly an end keyword. Languages ​​like Pascal, C , Modula -2 (and also newer versions of the BASIC language) have control structures for repetitions

     REPEAT
       ''Anweisungen''
     UNTIL ''Bedingung''

and conditional execution of statements:

     IF ''Bedingung'' THEN
        ''Anweisungen''
     ELSE
        ''Anweisungen''
     ENDIF

The keywords frame the instructions that are executed several times or conditionally. The above programming task can then be implemented as follows:

     I := 1
     REPEAT              -- Wiederhole
        PRINT I
        IF I=39 THEN     --    Wenn ... Dann
           I := 61
        ELSE             --    Sonst
           I := I+1
        ENDIF            --    EndeWenn
     UNTIL I>100         -- bis
     END                 -- Programmende
Structured program: Output of the numbers 1, 2, ..., 39, 61, 62, 63, ..., 99, 100

The program is given a structure that is usually made clear by indentations. The new keywords and formatting make the program code easier to understand.

Avoiding goto

Control structures help to avoid jumps with Goto , since almost every programming task can be solved with the help of condition and repetition structures. Programming without goto using a modern language is the norm today. The Java programming language supports z. B. no more Goto statement.

Some of the freedoms are waived. In the example above, I=39the structured variant is still I>100queried, although this is actually not necessary. An optimizing compiler could correct this, but is probably not able to recognize such subtleties in all cases.

The use of control structures prevents the creation of source code, which is jumped back and forth in a confusing way. The following features characterize a program when using control structures:

  • Instructions that are repeated are clearly indicated by keywords
  • Conditions and repetitions are immediately recognizable
  • Avoidance of goto and jump marks

Flowcharts and Structograms

Flow charts are used to graphically represent the program flow . You have the same freedom that Goto offers in the program code. In 1972 I. Nassi and B. Shneiderman came up with the idea of ​​replacing flowcharts with structured diagrams, in which control structures are represented by graphic elements, the Nassi-Shneiderman diagrams . Structograms can then be converted into a structured program code.

The unstructured code for outputting the numbers 1 to 39 and 61 to 100 can be represented by a flow chart:

flow chart
Unstructured program flowchart
Output of the numbers 1,2, ..., 39,61,62, ..., 100

The structured code could also be represented by a similar diagram, but also by a structured chart

Structogram
Structure diagram of the structured program
Output of the numbers 1,2, ..., 39,61,62, ..., 100

See also

Individual evidence

  1. ^ Edsger W. Dijkstra: Letters to the editor: Go To Statement Considered Harmful . In: Communications of the ACM . tape 11 , no. 3 , March 1968, p. 147-148 , doi : 10.1145 / 362929.362947 .