Pseudocode

from Wikipedia, the free encyclopedia

The pseudocode is a program code that is not used for machine interpretation, but only to illustrate a paradigm or algorithm . Most of the time it resembles high level programming languages mixed with natural language and mathematical notation. A program sequence can be described with pseudocode independently of the underlying technology. It is therefore often more compact and easier to understand than real program code. On the other hand, it is more formal and therefore clearer and less ambiguous than a description in natural language.

use

To understand an algorithm , you can examine it as a program. This is made more difficult by the peculiarities of the programming language, especially its syntax . In addition, different programming languages ​​have different syntaxes. Any formulation as a program in a certain programming language excludes all readers who are not able to speak that language. That is why the algorithm is formulated similar to a program, but without going into a specific programming language: in pseudocode.

Pseudocode is used when the functionality of an algorithm is to be explained and details of the implementation in a programming language would interfere. A typical example are the fields that are indexed from one in Pascal , for example , from zero in other languages ​​such as Java . In textbooks, algorithms are therefore occasionally reproduced in pseudocode.

A program can be specified by pseudocode . This should be avoided, however, because the formulation as pseudocode is already a programming activity that distracts from concentrating on the requirements.

Pseudocode is also used in the development of algorithms and the reshaping of programs ( program transformation , refactoring ).

Appearance and styles

Pseudocode claims to be intuitively clear. Suitable metaphors from colloquial language concisely reflect a procedural step without the need for an explanation, for example "run through field a with index i" or "swap the contents of the variables x and y". Such stylistic devices improve the overview. In addition, metasyntactic variables (foo, bar, ...) are often used.

Pseudocode can lean in its style to a specific high-level language, for example at Pascal or C . A pseudo-code based on the Java programming language is called Jana . In the Pascal-style keywords such as begin, end, then, do, repeat, untilused. In the C-style, curly braces {, are used instead }and the keyword thenis omitted. This style is often used by programmers who use such languages. Both styles can be found in textbooks.

The block structure is sometimes just shown by indentation.

A list of commonly used keywords:

Modules

  • program Programmname ... end Programmname
  • klasse Klassenname { ... }

Case distinctions

  • if ... then ... else ... end if/exit
  • wenn ... dann ... sonst ... wenn_ende
  • falls ... dann ... falls_nicht ... falls_ende

grind

  • wiederhole ... solange/bis ... wiederhole_ende
  • while ... do ...
  • repeat ... until ...
  • for ... to ... step Schrittweite ... next

Comments

  • // kommentar
  • # kommentar
  • /* kommentar */

Definition of functions

  • function() ... begin ... end
  • funktion() ... start ... ende

Assurances

  • assert
  • jetzt gilt

Examples

Pascal style pseudocode

program Name und Kurzbeschreibung
   LiesDatenStruktur()
   LiesDatenInhalt()
    ...
   if DatenUnvollständig then
      FehlerMelden
      exit
   end if
   HauptstatistikBerechnen
   ZusammenstellungBerechnen
   Resultate in HTML-Datei schreiben
end program Name

Pseudocode in the book Algorithms - An Introduction

In the book Algorithms - An Introduction (English original title Introduction to Algorithms , translated by Paul Molitor ) conventions for a pseudocode are defined. No error handling or other exceptions are handled. The following example (based on the book mentioned) shows the insertion sort algorithm in this pseudocode variant.

1 INSERTION-SORT(A)
2    for j=2 to A.länge
3       schlüssel=A[j]
4       //füge A[j] in den sortierten Beginn des Arrays A[1..j-1] ein
5       i=j-1
6       while i>0 und A[i]>schlüssel
7          A[i+1]=A[i]
8          i=i-1
9       A[i+1]=schlüssel

The following conventions apply to this pseudocode variant:

Indentations mark the block structure. In the example, lines 2 to 9 are INSERTION-SORTassigned to the procedure , lines 3 to 9 to the forloop and lines 7 and 8 to the whileloop.

grind

There are three loop constructs :

  • whileLoop with the following syntax:
while <Bedingung>
   <eingerückte Anweisung>*
  • forLoop with the following syntax:
for <Initialisierung> to|downto <Endebedingung> [by <delta>]
   <eingerückte Anweisung>*

The run variable of a for loop retains its value even after the loop has run. It then contains the value of the last loop pass. In for loops, the keyword is toused if the run variable is deltaincreased by or one in each loop pass , or the keyword downtoif the run variable is deltadecreased by or one in each pass .

  • repeat-untilLoop with the following syntax:
repeat
   <eingerückte Anweisung>*
* until <Endebedingung>

Branches

Branches are identified by if-else:

if <Bedingung>
   <eingerückte Anweisungen im If-Teil>*
[else
   <eingerückte Anweisung im Else-Teil>*]

Others

  • Comments are indicated by //.
  • Multiple assignments like i = j = kare interpreted from right to left: j = kandi = j
  • Variables can only be used locally without explicit identification.
  • Items in a field are accessed using an index in square brackets: A[3]returns the item with the index 3.
  • Related data are encapsulated in objects whose attributes can be accessed with the dot operator, e.g. B. the variables Vornameand Nachnameare Personencapsulated in an object . The Person.Vornameattribute Vornamecan be accessed with.
  • When calling procedures, basic types are passed as values ​​("call by value"), objects and fields with a reference ("call by reference").
  • The keyword returnmarks the end of a procedure and can contain an optional return value.
  • The Boolean operators “and” and “or” are sluggish operators, that is, for “x and y”, x is evaluated first. If x is false, y is no longer evaluated.
  • The keyword erroris used when an error has occurred. The calling procedure takes care of the error handling and does not have to be specified any further.

Alternatives

Instead of pseudo-code, flow diagrams such as the Nassi-Shneiderman diagram , the Jackson diagram or the standardized program flow chart can also be used.

Individual evidence

  1. ^ Kurt Mehlhorn and Peter Sanders : Algorithms and Data Structures . Springer, Berlin Heidelberg 2008, ISBN 978-3-540-77977-3 , p. 26 .
  2. Johannes Siedersleben (Ed.): Software technology . Hanser, Munich 2003, ISBN 3-446-21843-2 , p. 44 ff .
  3. ^ A b Thomas H. Cormen, Charles E. Leiserson, Ronald Rivest, Clifford Stein: Algorithmen - An Introduction . 3. Edition. Oldenburg Wissenschaftsverlag GmbH, Munich 2010, ISBN 978-3-486-59002-9 , p. 21st ff .