Imperative programming

from Wikipedia, the free encyclopedia

Imperative programming ( Latin imperare , “to arrange”, “to command”) is a programming paradigm according to which “a program consists of a sequence of instructions that dictate the order in which what should be done by the computer ”.

Imperative programming is the longest known programming paradigm. Due to the range of languages ​​used in earlier programming languages, this procedure was formerly the classic type of programming. It is the basis for the design of many programming languages, for example ALGOL , Fortran , Pascal , Ada , PL / I , Cobol , C and all assembly languages .

Deviating terms: In the literature, this development concept is sometimes also called “imperative / procedural”, “algorithmic” or “state-oriented”. The term “ procedural programming ” is sometimes used synonymously, but this is also defined as “use of procedures ”.

Details

The defining characteristics for this type of programming are:

  • The source code specifies what is to be done in which order and how: “First do this and next do that”, step-by-step 'progression' of the commands; "Viewed as sequence of things to be done" (shown as a sequence of instructions).
  • The used to control command execution developers control structures such. B. sequence, loop , conditional branch .

The abstract execution model on which the imperative paradigm is based is closely based on the execution of machine code on computers that are implemented according to the Von Neumann architecture . For example, there are conditional and unconditional jump instructions. The status of the computer or the program results from the content of data fields in the main memory and the status of system variables (e.g. registers , command counters  ...).

The hardware implementation of almost all computers is imperative. Almost all computer hardware is designed to execute machine language inherent in the computer and written in the imperative style. From this simple perspective, the program status is defined by the contents of memory and the instructions are instructions in the computer's native programming language . Higher-level imperative languages ​​use variables and more complex statements, but still follow the same paradigm . Recipes and process checklists are not computer programs , but they are well-known concepts whose style is similar to imperative programming . Every step is an instruction, and the physical world holds the state. Since the basic ideas of imperative programming are both conceptually familiar and contained directly in the hardware , most computer languages are imperative style.

In the imperative paradigm, assignments perform an operation on information in memory and store the results in memory for later use. Higher imperative languages ​​also allow the evaluation of complex expressions, which can consist of a combination of arithmetic operations and function evaluations, as well as the assignment of the resulting value to memory. Loop statements ( while loops , do-while loops, and for loops ) allow a sequence of statements to be executed several times. Loops can either execute the statements that they contain predefined, or they can execute them repeatedly until some conditions change. Conditional branch instructions allow a sequence of instructions to be executed only if a condition is met. Otherwise the statements are skipped and the execution sequence continues from the following statement. Unconditional branch instructions allow an execution sequence to be transferred to another part of a program . These include the jump instruction , switch and the subroutine , subroutine or procedure call that normally returns to the next instruction after the call.

Early in the development of high level programming languages , the introduction of the block made it possible to create programs in which a set of statements and declarations could be treated as if they were one statement. In addition to the introduction of subroutines , this made it possible to express complex structures by breaking them down hierarchically into simpler procedural structures.

Many imperative programming languages are abstractions of assembly language.

Demarcation

As a counter-proposal to imperative programming paradigms are functional programming and declarative programming . In declarative programming, the developer defines in the source code what the program should do, but not how.

"Most [...] programming languages , including object-oriented languages such as C # , Visual Basic .NET , C ++ and Java , primarily [also] support imperative [...] programming". The principle of data encapsulation (information hiding) is often implemented in imperative languages ​​by combining procedures that form a logical unit in modules or packages.

The fact that the individual commands of the machine code are executed “command by command” during the physical execution of computer programs in the processor is not decisive for the classification as 'imperative' , because this is always the case regardless of the paradigm practiced . Rather, 'imperative' presupposes that “a developer creates code that describes in detail the steps that the computer has to carry out to carry out the task”. Declaratively created instructions, for example, are converted into executable machine commands by higher-level or integrated system components - and only 'based' on the source code  . Example ' SQL commands': components of the system software of the database management system interpret them, generate machine code from it and have it executed.

example

Output of the square numbers of odd numbers from 3 to 11.

Imperative programming (example in the programming language C ):

for (int i = 3; i < 12; i += 2)
{
    printf("%d\n", i * i);
}

Functional programming (example in the Haskell programming language ):

mapM_ print [ i^2 | i <- [3,5..11] ]

literature

  • Terrence W. Pratt, Marvin V. Zelkowitz: Programming Languages: Design and Implementation . 4th edition. Prentice Hall, 2000, ISBN 978-0-13-027678-0 .
  • Robert W. Sebesta: Concepts of Programming Languages . 9th edition. Addison-Wesley, 2009, ISBN 978-0-13-607347-5 .

Individual evidence

  1. a b Böhm, Jungkunz: Basic course in IT professions . Vieweg-Verlag, books.google.de
  2. a b c Microsoft Library MSDN
  3. ^ Goos: Lectures on Computer Science . books.google.co.uk
  4. a b Java and Object-Oriented Programming Paradigm . books.google.com
  5. a b Programming Paradigms and Methodology . books.google.com
  6. Bruce Eckel: Thinking in Java . Pearson Education , 2006, ISBN 978-0-13-187248-6 , p. 24.