Pseudo Code Usage of pseudo code
Pseudo code is a typeAlgorithmDescription Language. The purpose of using the code is to make the described algorithm easy to use anyProgramming Language(Pascal, C, Java, etc) implementation. Therefore, pseudo-code must have a clear structure, simple code, good readability, and similar to natural languages.
The following describes the syntax rules for pseudo-code in Pascal.
Syntax Rules of pseudo-code
-
- In pseudo-code, each command occupies one line (except else if,), and the command is not followed by any symbol (the statements in Pascal and C should end with a semicolon );
- the "indent" in writing indicates the branch program structure in Program . This indent style also applies to if-then-else statements. Replacing the begin and end statements in traditional Pascal with indentation to represent the block structure of the program can greatly improve the clarity of the Code. The statements in the same module have the same indentation, the statements of the level-1 module are indented to those of its parent module.
example:
line 1 line 2 sub line 1 sub line 2 sub line 1 sub line 2 sub line 3
in Pascal, this relationship is represented by in and end nesting,
line 1 line 2 begin sub line 1 sub line 2 begin sub line 1 sub line 2 end ; sub line 3 end ; line 3
in C, this relationship is represented by nesting of {And,
line 1 line 2 { sub line 1 sub line 2 { sub line 1 sub line 2 } sub line 3 } line 3
- In pseudo-code, consecutive statements in the same module are usually identified by consecutive numbers or letters. Sometimes, labels can be omitted.
For example:
1. Line 1 2. Line 2 A. Sub line 1 B. Sub line 2 1. sub line 1 2. sub line 2 C. Sub line 3 3. Line 3
-
- The content after △represents a comment;
-
- In pseudo-code, variable names and reserved wordsCase InsensitiveThis is the same as Pascal, which is different from C or C ++;
-
- In pseudo-code, variables do not need to be declared, but the variables are partial to a specific process. You can use global variables without adding the display instructions;
-
- The value assignment statement is represented by the symbol 'distinct'. x 'expires' indicates that the exp value is assigned to X. X is a variable, exp is a variable or expression of the same type as X (the result of this expression is the same as X); multiple values I need J need e is to assign the value of expression E to the variables I and J, this representation is equivalent to J. E and I. e.
For example:
X ← Y x ← 20 * (Y + 1) x ← y ← 30
The preceding statements are represented by Pascal:
X: = y; X: = 20 * (Y + 1); X: = 30; Y: = 30;
The preceding statements are represented as follows in C:
X = y; X = 20 * (Y + 1); X = y = 30;
- the SELECT statement is represented by if-then-Else, and such if-then-else can be nested, which is no different from if-then-else in Pascal.
example:
If (condition1) then [block 1] else if (condition2) then [block 2] else [block 3]
- there are three types of loop statements: a while loop, a repeat-until loop, and a for loop. The syntax is similar to Pascal, except that in-end is replaced by indentation;
example:
1. X defaults 0 2. Y limit 0 3. Z limit 0 4. while x
the preceding statement is described in Pascal:
X: = 0; Y: = 0; Z: = 0; while x
the preceding statement is described in C or C ++:
X = y = z = 0; while (z
= 0);} z = x * Y;} y/= 2;
- The access to array elements is indicated by the array name followed by "[subscript. For example, a [J] indicates the J-th element of array. Symbol "…" Used to indicate the range of values in the array.
For example:
A [1... J] indicates the elements a [1], a [2],… , Subarray of a [J;
-
- Composite data is represented by objects, which are composed of attributes and fields. Domain access is represented by the object name enclosed by square brackets after the domain name.
For example:
An array can be considered as an object. Its attribute has length, indicating the number of elements. length [a] indicates the number of elements in array. Square brackets are used to represent the array elements and Object Attributes. The meaning of square brackets can be seen from the context.
Variables used to represent an array or an object are treated as pointers to data that represents an array or an object. If F is assigned to all the fields of an object X, F [y] = f [x] is used to make f [X]. If f [x] Isn 3, then not only f [x] = 3, but also F [y] = 3. In other words, after the value of Y ← X, X and Y point to the same object.
Sometimes, if a pointer does not point to any object, we grant itNil.
-
- The syntax of functions and procedures is similar to Pascal.
The function value is returned using the "Return (function return value)" statement. The Calling method is similar to Pascal. The call process name is used to call the function value;
For example:
1. x records T + 10 2. y records sin (x) 3. Call calvalue (x, y)
Parameters are passed to a process by value: a copy of the parameter is accepted during the call process. If a parameter is assigned a value, this change is invisible to the call process. When an object is passed, only the pointer pointing to the object is copied without copying its fields.