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 ).
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).
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 )
- 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:
- 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.
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:
The structured code could also be represented by a similar diagram, but also by a structured chart