This articleneeds additional citations forverification.(August 2016) |
Incomputer science,pseudocodeis a description of the steps in analgorithmusing a mix of conventions ofprogramming languages(likeassignment operator,conditional operator,loop) with informal, usually self-explanatory, notation of actions and conditions.[1][2]Although pseudocode shares features with regularprogramming languages,it is intended forhumanreading 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 isaugmentedwithnatural languagedescription details, where convenient, or with compactmathematical 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 andscientific publicationsto document algorithms and in planning of software and other algorithms.
No broad standard for pseudocodesyntaxexists, as a program in pseudocode is not an executable program; however, certain limited standards exist (such as for academic assessment). Pseudocode resemblesskeleton programs,which can becompiledwithout errors.Flowcharts,drakon-chartsandUnified Modelling Language(UML) charts can be thought of as a graphical alternative to pseudocode, but need more space on paper. Languages such asHAGGISbridge the gap between pseudocode and code written in programming languages.
Application
editTextbooks andscientific publicationsrelated tocomputer scienceandnumerical computationoften 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.
Aprogrammerwho 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 atop-downstructuring approach, with a process of steps to be followed as a refinement.
Pseudocode is also used in standardization. For example, theMPEGstandards make heavy use of formalC-like pseudocode and cannot be understood without grasping the details of the code.[4]
Syntax
editPseudocode generally does not actually obey thesyntaxrules 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 includeFortran,Pascal,BASIC,C,C++,Java,Lisp,andALGOL.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: procedurefizzbuzz;
fori:=1to100do
print_number:=true;
ifiisdivisibleby3thenbegin
print"Fizz";
print_number:=false;
end;
ifiisdivisibleby5thenbegin
print"Buzz";
print_number:=false;
end;
ifprint_number,printi;
printanewline;
end
|
C style: fizzbuzz(){
for(i=1;i<=100;i++){
print_number=true;
if(iisdivisibleby3){
print"Fizz";
print_number=false;
}
if(iisdivisibleby5){
print"Buzz";
print_number=false;
}
if(print_number)printi;
printanewline;
}
}
|
Python style: deffizzbuzz():
foriinrange(1,101):
print_number=true
ifiisdivisibleby3:
print"Fizz"
print_number=false
ifiisdivisibleby5:
print"Buzz"
print_number=false
ifprint_number:printi
printanewline
|
Mathematical style pseudocode
editInnumerical computation,pseudocode often consists ofmathematical notation,typically frommatrixandset theory,mixed with the control structures of a conventional programming language, and perhaps alsonatural languagedescriptions. 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 mathematicalalgorithms.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-ASCIItypesettingis used for the mathematical equations, for example by means of markup languages, such asTeXorMathML,or proprietaryformula editors.
Mathematical style pseudocode is sometimes referred to aspidgin code,for examplepidginALGOL(the origin of the concept),pidginFortran,pidginBASIC,pidginPascal,pidginC,andpidginLisp.
Common mathematical symbols
editType 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∈A1/a
|
Example
editThe following is a longer example of mathematical-style pseudocode, for theFord–Fulkerson algorithm:
algorithmford-fulkersonis input:GraphGwith flow capacityc, source nodes, sink nodet output:Flowfsuch thatfis maximal fromstot (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 eachedge (u,v)inGEdo f(u,v)← 0 f(v,u)← 0 whilethere exists a pathpfromstotinthe residual networkGfdo letcfbe the flow capacity of the residual networkGf cf(p) ← min{cf(u,v) | (u,v)inp} for eachedge (u,v)inpdo f(u,v)←f(u,v)+cf(p) f(v,u)← −f(u,v) returnf
Machine compilation of pseudocode style languages
editNatural language grammar in programming languages
editVarious attempts to bring elements of natural language grammar into computer programming have produced programming languages such asHyperTalk,Lingo,AppleScript,SQL,Inform,and to some extentPython.In these languages, parentheses and other special characters are replaced by prepositions, resulting in quite verbose code. These languages are typicallydynamically typed,meaning that variable declarations and otherboilerplate codecan 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
editAn 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 formalspecification languagesinclude set theory notation using special characters. Examples are:
- Z notation
- Vienna Development MethodSpecification Language (VDM-SL).
Somearray programminglanguages include vectorized expressions and matrix operations as non-ASCII formulas, mixed with conventional control structures. Examples are:
- A programming language(APL), and its dialectsAPLXandA+.
- MathCAD.
See also
editReferences
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.ISBN978-1-6654-9503-5.
- ^Mitchell et al. 1996,p. 105.
- ^McConnell, Steve(2004).Code Complete.Pearson Education. p. 54.ISBN978-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 quotedin this stackexchange question
- ^Lamport, Leslie (2 January 2009)."The PlusCal Algorithm Language"(PDF).Microsoft Research.Retrieved28 May2024.
Further reading
edit- Zobel, Justin (2013). "Algorithms".Writing for Computer Science(Second ed.). Springer.ISBN978-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.ISSN1531-4278.S2CID25810599.
- 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.ISBN978-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.ISBN978-3-540-74107-7.Retrieved2023-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.ISBN978-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.ISSN0737-0024.
External links
edit- A pseudocode standard
- Collected Algorithmsof theACM
- Pseudocode Guidelines,PDF file.