Pseudocode: Difference between revisions
→Common mathematical symbols: added add and remove notation for sets Tags: Reverted Visual edit |
→Application: fixing a factual error introduced during the copyedit |
||
(25 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|Description of an algorithm that resembles a computer program}} |
{{Short description|Description of an algorithm that resembles a computer program}} |
||
{{more citations needed|date=August 2016}} |
{{more citations needed|date=August 2016}} |
||
In [[computer science]], '''pseudocode''' is a description of the steps in an [[algorithm]] using a mix of conventions of [[programming languages]] (like [[assignment operator]], [[conditional operator]], [[loop (computing)|loop]]) with informal, usually self-explanatory, notation of actions and conditions.{{sfn|Reisig|2007|p=23|loc=Pseudocode Programs and Their Semantics}}<ref>An often-repeated definition of pseudocode since at least 2003 is "a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language"</ref> Although pseudocode shares features with regular [[programming languages]], it is intended for [[human]] reading rather than machine control. Pseudocode typically omits details that are essential for machine implementation of the algorithm. The programming language is [[augmented cognition|augment]]ed 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. |
In [[computer science]], '''pseudocode''' is a description of the steps in an [[algorithm]] using a mix of conventions of [[programming languages]] (like [[assignment operator]], [[conditional operator]], [[loop (computing)|loop]]) with informal, usually self-explanatory, notation of actions and conditions.{{sfn|Reisig|2007|p=23|loc=Pseudocode Programs and Their Semantics}}<ref>An often-repeated definition of pseudocode since at least 2003 is "a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language"</ref> Although pseudocode shares features with regular [[programming languages]], it is intended for [[human]] reading rather than machine control. Pseudocode typically omits details that are essential for machine implementation of the algorithm, meaning that pseudocode can only be verified by hand.<ref>{{cite book |last1=Ulate-Caballero |first1=Bryan Alexander |last2=Berrocal-Rojas |first2=Allan |last3=Hidalgo-Céspedes |first3=Jeisson |chapter=Concurrent and Distributed Pseudocode: A Systematic Literature Review |title=2021 XLVII Latin American Computing Conference (CLEI) |date=2021 |pages=1–10 |doi=10.1109/CLEI53233.2021.9640222|isbn=978-1-6654-9503-5 }}</ref> The programming language is [[augmented cognition|augment]]ed 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 (programming languages)|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 (computer programming)|skeleton programs]], which can be [[compiler|compiled]] without errors. [[Flowchart]]s, [[DRAKON|drakon-charts]] and [[Unified Modeling Language|Unified Modelling Language]] (UML) charts can be thought of as a graphical alternative to pseudocode, but need more space on paper. Languages such as <span class="plainlinks">[[HAGGIS]]</span> bridge the gap between pseudocode and code written in programming languages. |
No broad standard for pseudocode [[Syntax (programming languages)|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 (computer programming)|skeleton programs]], which can be [[compiler|compiled]] without errors. [[Flowchart]]s, [[DRAKON|drakon-charts]] and [[Unified Modeling Language|Unified Modelling Language]] (UML) charts can be thought of as a graphical alternative to pseudocode, but need more space on paper. Languages such as <span class="plainlinks">[[HAGGIS]]</span> bridge the gap between pseudocode and code written in programming languages. |
||
==Application== |
==Application== |
||
Pseudocode is commonly used in textbooks and [[scientific publications]] related to [[computer science]] and [[numerical computation]] to describe algorithms in a way that is accessible to programmers regardless of their familiarity with specific programming languages. Textbooks often include an introduction explaining the conventions in use, and the detail of pseudocode may sometimes approach that of formal programming languages. |
|||
[[Programmer|Programmers]] frequently begin implementing an unfamiliar algorithm by drafting it in pseudocode, then translating it into a programming language while adapting it to fit the larger program. This [[Top-down and bottom-up|top-down]] structuring approach often starts with a pseudocode sketch refined into executable code. Pseudocode is also used in standardization; for example, the [[Moving Picture Experts Group|MPEG]] standards rely on formal [[C (programming language)|C]]-like pseudocode, these standards cannot be understood without grasping the details of the code.{{sfn|Mitchell|Pennebaker|Fogg|LeGall|1996|p=105}} |
|||
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 and bottom-up design|top-down]] structuring approach, with a process of steps to be followed as a refinement. |
|||
The pseudocode is widely used in standardization, with the notable examples provided by the [[MPEG]] standards that make a very heavy use of [[C (language)|C]]-like pseudocode and cannot be understood without grasping the details of the code.{{sfn | Mitchell | Pennebaker | Fogg | LeGall | 1996 | p=105}} |
|||
==Syntax== |
==Syntax== |
||
Line 16: | Line 14: | ||
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. |
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 flexibility brings both major advantages and drawbacks: on the positive side, no executable programming language "can beat the convenience of inventing new constructs as needed and letting the reader try to deduce their meaning from informal explanations", on the negative, "untested code is usually incorrect".<ref>{{cite web |last1=Lamport |first1=Leslie |url=https://www.microsoft.com/en-us/research/uploads/prod/2016/12/The-PlusCal-Algorithm-Language.pdf |publisher=Microsoft Research |access-date=28 May 2024 | title = The PlusCal Algorithm Language |date=2 January 2009}}</ref> |
|||
{| class="wikitable" |
{| class="wikitable" |
||
Line 29: | Line 29: | ||
if i is divisible by 3 then begin |
if i is divisible by 3 then begin |
||
print "Fizz"; |
print "Fizz"; |
||
print_number := |
print_number := false; |
||
end; |
end; |
||
if i is divisible by 5 then begin |
if i is divisible by 5 then begin |
||
Line 108: | Line 108: | ||
| Σ Π |
| Σ Π |
||
| <code>''h'' ← Σ<sub>''a''∈''A''</sub> 1/''a''</code> |
| <code>''h'' ← Σ<sub>''a''∈''A''</sub> 1/''a''</code> |
||
|- |
|||
|Remove from set |
|||
|\ |
|||
|open <code>←</code>open \ {n} when |
|||
open is {a, b, c, n} results in |
|||
open is {a, b, c} |
|||
|- |
|||
|Add to set |
|||
|U |
|||
|closed <code>← closed U {h} adds</code>item h to existing set closed |
|||
|} |
|} |
||
===Example=== |
===Example=== |
||
The following is a longer example of mathematical-style pseudocode, for the [[Ford–Fulkerson algorithm]]: |
|||
<!-- Same example as in Wikipedia:WikiProject_Computer_science/Manual_of_style#Algorithms --> |
<!-- Same example as in Wikipedia:WikiProject_Computer_science/Manual_of_style#Algorithms --> |
||
Line 158: | Line 148: | ||
Some [[array programming]] languages include vectorized expressions and matrix operations as non-ASCII formulas, mixed with conventional control structures. Examples are: |
Some [[array programming]] languages include vectorized expressions and matrix operations as non-ASCII formulas, mixed with conventional control structures. Examples are: |
||
* [[APL (programming language)|A programming language]] (APL), and its dialects |
* [[APL (programming language)|A programming language]] (APL), and its dialects APLX and [[A+ (programming language)|A+]]. |
||
* [[MathCAD]]. |
* [[MathCAD]]. |
||
Latest revision as of 10:32, 2 December 2024
This article needs additional citations for verification. (August 2016) |
In computer science, pseudocode is a description of the steps in an algorithm using a mix of conventions of programming languages (like assignment operator, conditional operator, loop) with informal, usually self-explanatory, notation of actions and conditions.[1][2] Although pseudocode shares features with regular programming languages, it is intended for human reading rather than machine control. Pseudocode typically omits details that are essential for machine implementation of the algorithm, meaning that pseudocode can only be verified by hand.[3] 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.
Application
[edit]Pseudocode is commonly used in textbooks and scientific publications related to computer science and numerical computation to describe algorithms in a way that is accessible to programmers regardless of their familiarity with specific programming languages. Textbooks often include an introduction explaining the conventions in use, and the detail of pseudocode may sometimes approach that of formal programming languages.
Programmers frequently begin implementing an unfamiliar algorithm by drafting it in pseudocode, then translating it into a programming language while adapting it to fit the larger program. This top-down structuring approach often starts with a pseudocode sketch refined into executable code. Pseudocode is also used in standardization; for example, the MPEG standards rely on formal C-like pseudocode, these standards cannot be understood without grasping the details of the code.[4]
Syntax
[edit]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.[5][6] 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 flexibility brings both major advantages and drawbacks: on the positive side, no executable programming language "can beat the convenience of inventing new constructs as needed and letting the reader try to deduce their meaning from informal explanations", on the negative, "untested code is usually incorrect".[7]
Pascal style: 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: 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;
}
}
|
Python style: def fizzbuzz():
for i in range(1,101):
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
|
Mathematical style pseudocode
[edit]In numerical computation, pseudocode often consists of mathematical notation, typically from matrix and set theory, mixed with the control structures of a conventional programming language, and perhaps also natural language descriptions. This is a compact and often informal notation that can be understood by a wide range of mathematically trained people, and is frequently used as a way to describe mathematical algorithms. For example, the sum operator (capital-sigma notation) or the product operator (capital-pi notation) may represent a for-loop and a selection structure in one expression:
Return
Normally non-ASCII typesetting is used for the mathematical equations, for example by means of markup languages, such as TeX or MathML, or proprietary formula editors.
Mathematical style pseudocode is sometimes referred to as pidgin code, for example pidgin ALGOL (the origin of the concept), pidgin Fortran, pidgin BASIC, pidgin Pascal, pidgin C, and pidgin Lisp.
Common mathematical symbols
[edit]Type of operation | Symbol | Example |
---|---|---|
Assignment | ← or := | c ← 2πr , c := 2πr
|
Comparison | =, ≠, <, >, ≤, ≥ | |
Arithmetic | +, −, ×, /, mod | |
Floor/ceiling | ⌊, ⌋, ⌈, ⌉ | a ← ⌊b⌋ + ⌈c⌉
|
Logical | and, or | |
Sums, products | Σ Π | h ← Σa∈A 1/a
|
Example
[edit]The following is a longer example of mathematical-style pseudocode, for the Ford–Fulkerson algorithm:
algorithm ford-fulkerson is input: Graph G with flow capacity c, source node s, sink node t output: Flow f such that f is maximal from s to t (Note that f(u,v) is the flow from node u to node v, and c(u,v) is the flow capacity from node u to node v) for each edge (u, v) in GE do f(u, v) ← 0 f(v, u) ← 0 while there exists a path p from s to t in the residual network Gf do let cf be the flow capacity of the residual network Gf cf(p) ← min{cf(u, v) | (u, v) in p} for each edge (u, v) in p do f(u, v) ← f(u, v) + cf(p) f(v, u) ← −f(u, v) return f
Machine compilation of pseudocode style languages
[edit]Natural language grammar in programming languages
[edit]Various attempts to bring elements of natural language grammar into computer programming have produced programming languages such as HyperTalk, Lingo, AppleScript, SQL, Inform, and to some extent Python. In these languages, parentheses and other special characters are replaced by prepositions, resulting in quite verbose code. These languages are typically dynamically typed, meaning that variable declarations and other boilerplate code can be omitted. Such languages may make it easier for a person without knowledge about the language to understand the code and perhaps also to learn the language. However, the similarity to natural language is usually more cosmetic than genuine. The syntax rules may be just as strict and formal as in conventional programming, and do not necessarily make development of the programs easier.
Mathematical programming languages
[edit]An alternative to using mathematical pseudocode (involving set theory notation or matrix operations) for documentation of algorithms is to use a formal mathematical programming language that is a mix of non-ASCII mathematical notation and program control structures. Then the code can be parsed and interpreted by a machine.
Several formal specification languages include set theory notation using special characters. Examples are:
- Z notation
- Vienna Development Method Specification Language (VDM-SL).
Some array programming languages include vectorized expressions and matrix operations as non-ASCII formulas, mixed with conventional control structures. Examples are:
- A programming language (APL), and its dialects APLX and A+.
- MathCAD.
See also
[edit]- Concept programming
- Drakon-chart
- Flowchart
- Literate programming
- Program Design Language
- Short Code
- Structured English
References
[edit]- ^ Reisig 2007, p. 23, Pseudocode Programs and Their Semantics.
- ^ An often-repeated definition of pseudocode since at least 2003 is "a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language"
- ^ Ulate-Caballero, Bryan Alexander; Berrocal-Rojas, Allan; Hidalgo-Céspedes, Jeisson (2021). "Concurrent and Distributed Pseudocode: A Systematic Literature Review". 2021 XLVII Latin American Computing Conference (CLEI). pp. 1–10. doi:10.1109/CLEI53233.2021.9640222. ISBN 978-1-6654-9503-5.
- ^ Mitchell et al. 1996, p. 105.
- ^ McConnell, Steve (2004). Code Complete. Pearson Education. p. 54. ISBN 978-0-7356-1967-8.
Avoid syntactic elements from the target programming language
- ^ Invitation to Computer Science, 8th Edition by Schneider/Gersting, "Keep statements language independent" as quoted in this stackexchange question
- ^ Lamport, Leslie (2 January 2009). "The PlusCal Algorithm Language" (PDF). Microsoft Research. Retrieved 28 May 2024.
Further reading
[edit]- Zobel, Justin (2013). "Algorithms". Writing for Computer Science (Second ed.). Springer. ISBN 978-1-85233-802-2.
- Roy, Geoffrey G (2006). "Designing and explaining programs with a literate pseudocode". Journal on Educational Resources in Computing. 6 (1). Association for Computing Machinery (ACM): 1. doi:10.1145/1217862.1217863. ISSN 1531-4278. S2CID 25810599.
- Ulate-Caballero, Bryan Alexander; Berrocal-Rojas, Allan; Hidalgo-Cespedes, Jeisson (2021-10-25). "Concurrent and Distributed Pseudocode: A Systematic Literature Review". 2021 XLVII Latin American Computing Conference (CLEI). IEEE. pp. 1–10. doi:10.1109/clei53233.2021.9640222. ISBN 978-1-6654-9503-5.
- Reisig, Wolfgang (2007). "Abstract State Machines for the Classroom". Logics of Specification Languages. Monographs in Theoretical Computer Science. An EATCS Series. Springer Berlin Heidelberg. pp. 15–46. ISBN 978-3-540-74107-7. Retrieved 2023-10-05.
- Mitchell, Joan L.; Pennebaker, William B.; Fogg, Chad E.; LeGall, Didier J. (1996). "Pseudocode and Flowcharts". MPEG Video Compression Standard. New York, NY: Springer US. pp. 105–116. doi:10.1007/0-306-46983-9_6. ISBN 978-0-412-08771-4.
- Bellamy, Rachel (1994-06-01). "What Does Pseudo-Code Do? A Psychological Analysis of the use of Pseudo-Code by Experienced Programmers". Human-Computer Interaction. 9 (2). Informa UK Limited: 225–246. doi:10.1207/s15327051hci0902_3. ISSN 0737-0024.
External links
[edit]- A pseudocode standard
- Collected Algorithms of the ACM
- Pseudocode Guidelines, PDF file.