Pseudo-code (reprint)

Source: Internet
Author: User

How pseudo-code is spelled

Pseudo-code (PSEUDOCODE) is an algorithm description language. The purpose of using pseudo-code is to make the described algorithm easy to implement in any programming language (PASCAL,C,JAVA,ETC). As a result, pseudocode must be structurally clear, code simple, readable, and similar to natural language. Between the natural language and the programming language.

It specifies the function of the algorithm in the written form of the programming language. It is more like natural language than programming languages (such as Java, C++,c, Dephi, and so on). It is a semi-angular, non-standard language. We can use the entire algorithm to run the structure of the process in close to natural language form (here, you can use any kind of familiar text, Chinese, English, etc., the key is that you put the meaning of your program) described. The use of pseudo-code, can help us to better express the algorithm, do not rigidly adhere to the specific implementation.

When people implement the same algorithm in different programming languages, they realize that their implementation (note: Here is implementation, not functionality) is very different. Especially for programmers who are skilled in different programming languages, it can be difficult to understand the functionality of a program written in another programming language, because the form of a program language restricts the programmer's understanding of the key parts of the program. So the pseudo-code is born.

Pseudo-code is often applied when considering algorithmic functions (rather than their language implementations). Computer science often uses virtual codes in teaching so that all programmers can understand them.

In summary, easy to say, easy to understand code. A language-independent code that is used to represent the execution of a program, not necessarily to compile a run. In the data structure of the algorithm used a lot of time.

Grammar rules

For example, the syntax rules for pseudo-code in a class Pascal language are: in pseudo-code, each instruction takes one line (else if, exception). Instructions are not followed by any symbols (the statements in Pascal and C end with semicolons). The "indent" on the write represents the branch structure in the program. This indentation style also applies to if-then-else statements. Using indentation instead of the begin and end statements in traditional Pascal to represent the block structure of a program can greatly improve the clarity of the Code; the statements of the same module have the same indentation, and the statements of the second-level module are indented relative to the statements of their parent modules.

The pseudo-code language of the algorithm may seem less formal in some ways, but it provides a lot of convenience for us to describe the algorithm, and allows us to ignore the many troublesome details of the algorithm implementation. Usually each algorithm begins to describe its input and output, and each line of the algorithm is numbered, in the process of interpreting the algorithm will often use the line number in the algorithm step to refer to the algorithm steps. The pseudo-code description of the algorithm is not very strict, its main characteristics and the usual provisions are as follows:
1) The array or variable that appears in the algorithm can be of the following types: integers, real numbers, characters, bit strings, or pointers. Usually these types can be clear from the context of the algorithm and do not need to be explained in extra terms.
2) Some instructions or subtasks in the algorithm can be described in words, for example, "Set X is the largest item in a", where a is an array, or "Insert X in L", where L is a linked list. The purpose of this is to avoid clutter in the algorithm itself because of the details that are irrelevant to the main problem.
3) An arithmetic expression can use the usual arithmetic operators (+,-,*,/, as well as the ^ that represents a power). Logical expressions can use relational operators =,≠,<,>,≤ and ≥, as well as logical operators with (and), or (or), non-(not).
4) The assignment statement is the following form of statement: A<-b.
Here A is a variable, an array item, and b is an arithmetic expression, a logical expression, or a pointer expression. The meaning of the statement is to assign the value of B to a.
5) If both A and B are variables and array items, then the token a<->b represents the contents of a and B are exchanged.
6) Goto statement has form
Goto label (Goto label)
It will result in a turn to the statement with the specified label.
7) There are two forms of the conditional statement:
If C then s or
If C then s
else s′
Here c is the logical expression, s and s′ are either single statements or string of statements that are enclosed between do and end. For both of these forms, if C is true, then S is executed once. If C is false, in the first form the execution of the IF statement is completed, and in the second form, the s′ is executed. In all cases, the control moves to the next statement, unless the goto statement in S or s′ makes the control move elsewhere.
8) There are two kinds of cyclic instructions: while and for.
The while statement is in the form
While C do
S
End
Here c is a logical expression, and S is a string of statements consisting of one or more statements. When C is true, execute s. C is checked before each execution of S, and if C is false, the control is taken to the statement immediately following the while statement. Note that when the control reaches the while statement for the first time, if C is false, then s does not execute at the same time.
The For statement is in the form
for VAR init to limit by incr do
S
End
Here var is a variable, and init, limit, and incr are all arithmetic expressions, while S is a statement string consisting of one or more statements. At initial time, Var is given the value of init. If incr≥0, just var≤limit, execute s and add incr to Var. (If incr<0, just var≥limit, execute s and add incr to Var). The INCR symbol cannot be changed by S.
9) The Exit statement can be used to end the execution of a while loop or for loop before the usual end condition is met. Exit causes a transition to a statement immediately following the (most inner) while or for loop that contains exit.
The return is used to indicate the end point of an algorithm execution, which is usually omitted if the algorithm ends after the last instruction, and it is used most often when an undesirable condition is detected. The return can be immediately followed by the information enclosed in quotation marks.
11) The annotations in the algorithm are enclosed in/* */. Various inputs or outputs, such as read and output, are also used when needed.

Pseudo Code Instance

Pseudo-code is just like a flowchart used in the early stages of programming to help write program flow. Simple programs generally do not have to write process, write ideas, but the complex code, it is best to write down the process, the overall consideration of how the entire function to achieve. After writing, not only can be used as the basis for later testing, maintenance, but also to communicate with others. However, it can take a lot of time to write down the whole thing, so this time it is possible to use pseudo-code. Like what:

IF nine points before then

do private affairs;

ELSE 9 point to 18 then

Work

ELSE

Work

END IF

This will not only achieve the effect of the document, but also save time. More importantly, make the structure more clear, the expression is more intuitive.

The following is a description of the pseudo-code syntax rules for a class Pascal language.

In pseudo-code, each instruction takes one line (else if exception), and the instruction does not follow any symbols (the statements in Pascal and C end with semicolons);

The "indent" on the write represents the branch structure in the program. This indentation style also applies to if-then-else statements. Using indentation instead of the begin and end statements in traditional Pascal to represent the block structure of a program can greatly improve the clarity of the Code; the statements of the same module have the same indentation, and the statements of the second-level module are indented relative to the statements of their parent modules;

In pseudo-code, successive statements in the same module are usually marked with consecutive numbers or letters, and sometimes the labels can be omitted.

The content after the symbol represents a comment;

In pseudo-code, variable names and reserved words are not case-sensitive, which is the same as Pascal, unlike C or C + +;

In pseudo-code, variables do not need to be declared, but variables are local to a particular procedure, and a description that cannot be displayed uses global variables;

The assignment statement is denoted by the symbolic ← x←exp, which indicates 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 that expression is the same as x); multiple assignments i←j←e the value of the expression E to variables I and J, which is equivalent to J←e and i←e.

For example:

X←y

x←20* (y+1)

X←y←30

The above statements are expressed in C as:

x = y;

x = 20* (y+1);

x = y = 30;

The SELECT statement is represented by If-then-else, and this if-then-else can be nested, and is no different from the If-then-else in Pascal.

For example:

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, whose syntax is similar to Pascal, except that it is indented instead of begin-end;

For example:

1. x←0

2. y←0

3. z←0

4. While x < N

1. Do x←x + 1

2. Y←x + y

3. For t←0 to 10

1. Do z← (z + x * y)/100

2. Repeat

1. Y←y + 1

2. Z←z-y

3. Until Z < 0

4. Z←x * y

5. Y←Y/2

The above statement is described in C or C + + as:

x = y = z = 0;

while (Z < N)

{

x + +;

Y + = x;

for (t = 0; t < t++)

{

z = (z + x * y)/100;

do {

Y + +;

Z-= y;

} while (z >= 0); } z = x * y;

}

Y/= 2;

Array elements are accessed with an array name followed by a "[Subscript]" representation. For example, A[j] indicates the J element of array A. The symbol "..." is used to indicate the range of values in the array.

For example:

A[1...J] = sub-array containing element a[1], a[2], ..., a[j];

Composite data is represented by objects (object), which are composed of attributes (attribute) and fields (field). Domain access is represented by the name of the domain followed by the name of the object enclosed in square brackets.

For example:

An array can be thought of as an object whose properties have length, which represents the number of elements in it, then Length[a] represents the number of elements in array A. Square brackets are used when representing array elements and object properties, which are generally seen in terms of context.

A variable used to represent an array or an object is treated as a pointer to the data representing the array or object. For all fields F of an object X, the assignment y←x makes F[y]=f[x], further, if there is f[x]←3, there is not only f[x]=3, but also f[y]=3, in other words, after the assignment y←x, x and y point to the same object.

Sometimes, when a pointer does not point to any object, we assign him nil.

function and procedure syntax are similar to Pascal.

The function value is returned using the return (function return value) statement, which is similar to Pascal's, and the procedure is invoked with the call Procedure name statement;

For example:

1. X←t + 10

2. Y←sin (x)

3. Call Calvalue (x, y)

Parameters are passed by value to a procedure: The called procedure takes a copy of the parameter, and if he assigns a value to a parameter, the change is not visible to the process of making the call. When you pass an object, you simply copy a pointer to that object without copying its fields.

Pseudo-code (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.