What Is a Pseudocode?
Pseudocode is an informal, English-like language used to describe module structure diagrams. When people implement the same algorithm in different programming languages, they realize that their implementation (note: this is an implementation, not a function) is very different. Especially for programmers proficient in different programming languages, it may be difficult to understand the functionality of a program (written in another programming language), because the form of the programming language limits the programmer's understanding of key parts of the program. So pseudocode came into being. Pseudocode provides more design information, and the description of each module must appear with the design structure diagram.
- When considering algorithmic functions (rather than their language implementation), pseudocode is often applied. Pseudo code is often used in technical documents and scientific publications to represent algorithms, and is also used to express the logic of a program before the actual coding process of software development. Pseudocode is not a tool for users and analysts, but a tool for designers and programmers. Computer science usually uses virtual code in teaching so that all programmers can understand it.
- In summary, the code is easy to understand. Language-independent, used to indicate the process of program execution, and does not necessarily compile and run code. Algorithms are used a lot in data structures. Pseudocode is used to express the programmer's thoughts before starting coding.
- For example, the class
- Pseudocode : describes the algorithm with words and symbols (including mathematical symbols) between natural language and computer language.
- [Simple example] Enter 3 numbers and print the largest number. Can be represented by the following pseudo code:
- Begin
- Enter A, B, C
- IF A> B then A Max
- Otherwise B Max
- IF C> Max then C Max
- Print Max
- End (end of algorithm)
- Pseudo code is just used in the early stage of program design like a flowchart to help write the program flow. Simple programs generally do not need to write processes and ideas, but for complex code, it is best to write down the process and consider how to implement the entire function. After writing, it can be used not only as a basis for future testing and maintenance, but also for communicating with others. However, if writing down everything is bound to waste a lot of time, then pseudocode can be used at this time. such as:
- if before nine
- do private affairs
- if 9 to 18
- jobs;
- else
- Off work
- end if
- This will not only achieve the effect of the document, but also save time. More importantly, make the structure clearer and the expression more intuitive.
- The following describes the syntax rules of a Pascal-like pseudocode.
- In pseudo-code, each instruction occupies a line (else if exception,), without any symbol after the instruction (the statements in Pascal and C should end with a semicolon);
- "Indentation" in writing indicates the branch program structure in the program. This indentation style also applies to if-then-else statements. The statements of the same module have the same indentation, and the statements of the next-level module are indented relative to the statements of the parent module;
- E.g:
- line 1
- line 2
- sub line 1
- sub line 2
- sub sub line 1
- sub sub line 2
- sub line 3
- line 3
- In Pascal, this relationship is represented by the nesting of begin and end.
- line 1
- line 2
- begin
- sub line 1
- sub line 2
- begin
- sub sub line 1
- sub sub line 2
- end;
- sub line 3
- end;
- line 3
- In C, this relationship is represented by the nesting of {and},
- line 1;
- line 2;
- {
- sub line 1;
- sub line 2;
- {
- sub sub line 1;
- sub sub line 2;
- }
- sub line 3;
- }
- line 3;
- In pseudo code, continuous numbers or letters are usually used to indicate continuous statements in the same level of module, and sometimes labeling can be omitted.
- E.g:
- line 1
- line 2
- a. sub line 1
- b. sub line 2
- sub sub line 1
- sub sub line 2
- c. sub line 3
- line 3
- The content after the symbol indicates a comment;
- In pseudo code, variable names and reserved words are not case sensitive. This is the same as Pascal, but different from C or C ++;
- In pseudo code, variables do not need to be declared, but variables are local to a specific process, and global variables cannot be used without explicit instructions;
- The assignment statement is represented by the symbol , x exp means that the value of exp is assigned to x, where x is a variable, and exp is a variable or expression of the same type as x (the result of the expression is the same type as x); multiple The assignment i j e assigns the value of the expression e to the variables i and j. This representation is equivalent to j e and i e.
- E.g:
- x y
- x 20 * (y + 1)
- x y 30
- The above statements are expressed in Pascal as:
- x: = y;
- x: = 20 * (y + 1);
- x: = 30; y: = 30;
- The above statements are expressed in C as:
- x = y;
- x = 20 * (y + 1);
- x = y = 30;
- The select statement is expressed by if-then-else, and this if-then-else can be nested, which is no different from if-then-else in Pascal.
- E.g:
- if (Condition1)
- then [Block 1]
- else if (Condition2)
- then [Block 2]
- else [Block 3]
- There are three types of loop statements: while loop, repeat-until loop, and for loop. The syntax is similar to Pascal, but the indentation is used instead of begin-end
- E.g:
- x 0
- y 0
- z 0
- while x <N
- do x x + 1
- y x + y
- for t 0 to 10
- do z (z + x * y) / 100
- repeat
- y y + 1
- z z-y
- until z <0
- z x * y
- y y / 2
- The above statement is described in Pascal:
- x: = 0;
- y: = 0;
- z: = 0;
- while x <N do
- begin
- x: = x + 1;
- y: = x + y;
- for t: = 0 to 10 do
- begin
- z: = (z + x * y) / 100;
- repeat
- y: = y + 1;
- z: = z-y;
- until z <0;
- end;
- z: = x * y;
- end;
- y: = y / 2;
- The above statement is described in C or C ++:
- x = y = z = 0;
- while (z <N)
- {
- x ++;
- y + = x;
- for (t = 0; t <10; t ++)
- {
- z = (z + x * y) / 100;
- do {
- y ++;
- z-= y;
- } while (z> = 0);
- }
- z = x * y;
- }
- y / = 2;
- Array element access is indicated by the array name followed by "[subscript]". For example, A [j] indicates the j-th element of the array A. The symbol "..." is used to indicate the range of values in the array.
- E.g:
- A [1 ... j] represents a sub-array containing elements A [1], A [2], ..., A [j]
- Compound data is represented by objects, which are composed of attributes and fields. Domain access is indicated by the domain name followed by the object name enclosed in square brackets.
- E.g:
- An array can be regarded as an object, and its attribute has length, which indicates the number of elements in it, and length [A] indicates the number of elements in array A. Use square brackets when representing array elements and object attributes. Generally speaking, the meaning can be seen from the context.
- A variable used to represent an array or object is considered a pointer to data representing the array or object. For all domains f of an object x, assigning y x makes f [y] = f [x]. Furthermore, if f [x] 3, not only f [x] = 3, but also f [y] = 3, in other words, after assigning y x, x and y point to the same object.
- Sometimes a pointer does not point to any object, and at this time we assign him nil.
- The function and procedure syntax is similar to Pascal.
- The function value is returned using the "return" statement, and the calling method is similar to Pascal; the procedure is called with the "call procedure name" statement;
- E.g:
- x t + 10
- y sin (x)
- Cal call CalValue (x, y)
- Parameters are passed to a procedure by value: the called procedure accepts a copy of the parameter. If he assigns a value to a parameter, this change is not visible to the calling procedure. When passing an object, only the pointer to the object is copied, and its fields are not copied.