Pseudocode
Pseudo code is code that is not intended for automatic interpretation, but merely illustrative of a paradigm or algorithm. In most cases, it resembles natural language and high level languages , mixed with mathematical notation. With pseudo code program flow, regardless of the underlying technology are described, and is thus often more compact and easier to understand than real code.
Use
To understand an algorithm, one can examine it as a program. This is made difficult by the peculiarities of programming language, including their syntax. In addition, several programming languages have different syntaxes. Each formulation as a program in a particular programming language excludes all readers who do not have this language. Therefore, to formulate the algorithm, although similar to a program, but without going into a particular programming language: in pseudocode.
Pseudo-code is used when the operation of an algorithm will be explained and would interfere with details of the implementation in a programming language. A typical example are the fields that will be indexed in Pascal from one to, in C, however, from scratch. Therefore, algorithms are sometimes given in pseudo-code in textbooks.
You can specify a program by pseudocode. This should, however, be rather avoided, because the formulation as pseudo- code is already a programming activity that distracts from focusing on the requirements.
Even in the development of algorithms and transformation of programs ( program transformation, refactoring ) pseudocode is employed.
Appearance and styles
Pseudo code has the claim to be intuitively clear. Suitable metaphors from everyday language to give a concise step back without the need for an explanation is necessary, for example, " go through the array a with index i" or " reverse the contents of the variables x and y". Such stylistic devices improve visibility.
Pseudocode can lean in his style to a particular high-level language, such as Pascal or C. A ajar to the Java programming language pseudo-code is called Jana.
Pascal-style keywords like begin, end, then, do, repeat, until are used. In the C - style instead braces { } are set, and then keyword is omitted. This style is often used by programmers using those languages. Both styles can be found in textbooks.
The block structure is sometimes also reproduced only by engaging.
A list of frequently used keywords:
Modules
- Program program name ... end program name
- Class classname { ...}
Case distinctions
- If ... then ... else ... end if / exit
- If ... then ... else ... wenn_ende
- If ... then ... falls_nicht ... falls_ende
Grind
- Repeat ... until / to ... wiederhole_ende
- While ... do ...
- Repeat ... until ...
- For ... to ... step increment next ...
Comments
- / / Comment
- # comment
- / * Comment * /
Definition of functions
- Function () ... begin ... end
- Function ( ) start ... end ...
Assurances
- Assert
- Now applies
Examples
Pseudocode in the style of Pascal
Program name and brief description Read data structure ( ) Read data () ... if data incomplete then Report error exit end if Central Statistical computing Compilation Calculate Write results to HTML file end program name Pseudocode by Leiserson et. al.
Leiserson et. al. (2010) define a clear and concise pseudocode. In this case, no error handling, and other exceptions are handled. The following example shows the insertion sort algorithm in this pseudo- code version.
INSERTION -SORT (A) for j = 2 to A.länge key = A [ j] / / add A [ j] into the sorted array A the beginning of the [ 1. j- 1] i = j-1 while i> 0 and A [i ]> key A [i 1 ] = A [i] i = i-1 A [i 1 ] = key Apply in this pseudo- code version of the following conventions:
Indentation mark the block structure. Thus, the lines in the example to 9 of the procedure INSERTION -SORT assigned 2, the lines 3-9 of the for loop and lines 7 and 8 of the while loop. There are three looping constructs:
While loop with the following syntax:
While
For
Repeat-until loop with the following syntax:
Repeat
If
Multiple assignment as i = j = k are interpreted from right to left: j = k and i = j
Variables without explicitly marking only locally used.
On elements in a field is accessed through an index in square brackets: A is the element with index 3 back.
Related data are encapsulated in objects, their attributes can be accessed using the dot operator, eg variables first name and last name are encapsulated in an object person. With Person.Vorname can be accessed on the attribute first name.
In procedure calls, base types are passed by value ( " call by value " ), objects and fields with a reference ( " call by reference ").
The return keyword marks the end of a procedure, and may include an optional return value.
The Boolean operators "and" and "or" operators are carriers, that is at "x and y" is first evaluated x. If x is false, y is not evaluated.
Error keyword is used when an error has occurred. The error handler accepts the calling procedure and need not be further specified.
Alternatives
Instead of pseudo-code can also flow charts such as the Nassi- Shneiderman diagram used Jackson - diagram or the normalized flow chart.