Pseudocode

In computer science, pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a normal programming language, but is intended for human reading rather than machine reading. It typically omits details that are essential for machine understanding of the algorithm, such as variable declarations and language-specific code. The programming language is augmented with natural language description details, where convenient, or with compact mathematical notation. The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications to document algorithms and in planning of software and other algorithms.

No broad standard for pseudocode syntax exists, as a program in pseudocode is not an executable program; however, certain limited standards exist (such as for academic assessment). Pseudocode resembles skeleton programs, which can be compiled without errors. Flowcharts, drakon-charts and Unified Modelling Language (UML) charts can be thought of as a graphical alternative to pseudocode, but need more space on paper. Languages such as HAGGIS bridge the gap between pseudocode and code written in programming languages.

1 Application

Textbooks and scientific publications related to computer science and numerical computation often use pseudocode in description of algorithms, so that all programmers can understand them, even if they do not all know the same programming languages. In textbooks, there is usually an accompanying introduction explaining the particular conventions in use. The level of detail of the pseudocode may in some cases approach that of formalized general-purpose languages.

A programmer who needs to implement a specific algorithm, especially an unfamiliar one, will often start with a pseudocode description, and then “translate” that description into the target programming language and modify it to interact correctly with the rest of the program. Programmers may also start a project by sketching out the code in pseudocode on paper before writing it in its actual language, as a top-down structuring approach, with a process of steps to be followed as a refinement.

2 Syntax

Pseudocode generally does not actually obey the syntax rules of any particular language; there is no systematic standard form. Some writers borrow style and syntax from control structures from some conventional programming language, although this is discouraged.[1][2] Some syntax sources include Fortran, Pascal, BASIC, C, C++, Java, Lisp, and ALGOL. Variable declarations are typically omitted. Function calls and blocks of code, such as code contained within a loop, are often replaced by a one-line natural language sentence.

Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other.

This is an example of pseudocode (for the mathematical game fizz buzz):

Fortran style pseudocode:

program fizzbuzz
Do i = 1 to 100
set print_number to true
If i is divisible by 3
print “Fizz”
set print_number to false
If i is divisible by 5
print “Buzz”
set print_number to false
If print_number, print i
print a newline
end do
Pascal style pseudocode:

procedure fizzbuzz;
for i := 1 to 100 do
print_number := true;
if i is divisible by 3 then begin
print “Fizz”;
print_number := false;
end;
if i is divisible by 5 then begin
print “Buzz”;
print_number := false;
end;
if print_number, print i;
print a newline;
end
C style pseudocode:

fizzbuzz() {
for (i = 1; i <= 100; i++) {
print_number = true;
if (i is divisible by 3) {
print “Fizz”;
print_number = false;
}
if (i is divisible by 5) {
print “Buzz”;
print_number = false;
}
if (print_number) print i;
print a newline;
}
}
Structured Basic style pseudocode:

Sub fizzbuzz()
For i = 1 to 100
print_number = True
If i is divisible by 3 Then
Print “Fizz”
print_number = False
End If
If i is divisible by 5 Then
Print “Buzz”
print_number = False
End If
If print_number = True Then print i
Print a newline
Next i
End Sub
See also: Category:Articles with example pseudocode

3 Mathematical style pseudocode

3.1 Common mathematical symbols

3.2 Example

4 Machine compilation of pseudocode style languages

4.1 Natural language grammar in programming languages

4.2 Mathematical programming languages

5 See also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值