Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pseudocode
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Description of an algorithm that resembles a computer program}} {{Distinguish|Generic programming}} {{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, 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 reasons for using pseudocode are 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. ==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}} ==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.<ref>{{cite book|title=Code Complete|title-link=Code Complete|page=54 |quote=Avoid syntactic elements from the target programming language|first1=Steve|last1=McConnell|authorlink=Steve McConnell|isbn=978-0-7356-1967-8 |date=2004|publisher=Pearson Education }}</ref><ref>Invitation to Computer Science, 8th Edition by Schneider/[[Judith Gersting|Gersting]], "Keep statements language independent" as quoted [https://cs.stackexchange.com/questions/97851/writing-pseudocode-to-be-language-independent in this stackexchange question]</ref> Some syntax sources include [[Fortran]], [[Pascal (programming language)|Pascal]], [[BASIC]], [[C (programming language)|C]], [[C++]], [[Java (programming language)|Java]], [[Lisp (programming language)|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".<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" |+ An example of pseudocode (for the [[mathematical game]] [[fizz buzz]]) |-valign="top" || Pascal style: <syntaxhighlight lang="Pascal" style="font-size:85%"> 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 </syntaxhighlight> || C style: <syntaxhighlight lang="C" style="font-size:85%"> 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; } } </syntaxhighlight> || Python style: <syntaxhighlight lang="Python" style="font-size:85%"> 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 </syntaxhighlight> |} {{Category see also|Articles with example pseudocode}} ==Mathematical style pseudocode== In [[numerical computation]], pseudocode often consists of [[mathematical notation]], typically from [[matrix (mathematics)|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 [[algorithm]]s. 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: {{nowrap|Return <math>\sum_{k\in S} x_k</math>}} 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 editor]]s. 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 (programming language)|Pascal]]'', ''pidgin [[C (programming language)|C]]'', and ''pidgin [[Lisp (programming language)|Lisp]]''. ===Common mathematical symbols=== <!-- Copied from Wikipedia:WikiProject_Computer_science/Manual_of_style#Algorithms --> {| class="wikitable" ! Type of operation || Symbol || Example |- | Assignment || β or := ||<code>''c'' β 2Ο''r''</code>, <code> ''c'' := 2Ο''r''</code> |- | Comparison || =, β , <, >, β€, β₯ || |- | Arithmetic || +, β, Γ, /, mod || |- | Floor/ceiling || β, β, β, β || <code>''a'' β β''b''β + β''c''β</code> |- | Logical | '''and''', '''or''' | |- | Sums, products | Ξ£ Ξ | <code>''h'' β Ξ£<sub>''a''β''A''</sub> 1/''a''</code> |} ===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 --> '''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<sub>(u,v)</sub> is the flow from node u to node v, and c<sub>(u,v)</sub> is the flow capacity from node u to node v)'' '''for each''' edge (''u'', ''v'') '''in''' ''G''<sub>''E''</sub> '''do''' ''f''<sub>(''u'', ''v'')</sub> β 0 ''f''<sub>(''v'', ''u'')</sub> β 0 '''while''' there exists a path ''p'' from ''s'' to ''t'' '''in''' the residual network ''G''<sub>''f''</sub> '''do''' let ''c''<sub>''f''</sub> be the flow capacity of the residual network ''G''<sub>''f''</sub> ''c''<sub>''f''</sub>(''p'') β min{''c''<sub>''f''</sub>(''u'', ''v'') | (''u'', ''v'') '''in''' ''p''} '''for each''' edge (''u'', ''v'') '''in''' ''p'' '''do''' ''f''<sub>(''u'', ''v'')</sub> β ''f''<sub>(''u'', ''v'')</sub> + ''c''<sub>''f''</sub>(''p'') ''f''<sub>(''v'', ''u'')</sub> β β''f''<sub>(''u'', ''v'')</sub> '''return''' ''f'' ==Machine compilation of pseudocode style languages== ===Natural language grammar in programming languages=== Several attempts to bring elements of natural language grammar into computer programming have produced programming languages such as [[HyperTalk]], [[Lingo (programming language)|Lingo]], [[AppleScript]], [[SQL]], [[Inform]], and to some extent [[Python (programming language)|Python]]. In these languages, parentheses and other special characters are replaced by prepositions, resulting in quite verbose code. These languages are typically [[Dynamic typing|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=== 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 language]]s 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: * [[APL (programming language)|A programming language]] (APL), and its dialects APLX and [[A+ (programming language)|A+]]. * [[MathCAD]]. ==See also== * [[Concept programming]] * [[DRAKON|Drakon-chart]] * [[Flowchart]] * [[Literate programming]] * [[Program Design Language]] * [[Short Code]] * [[Structured English]] ==References== {{Reflist}} ==Further reading== *{{cite book |first=Justin |last=Zobel |year=2013 |chapter=Algorithms |title=Writing for Computer Science |url=https://archive.org/details/springer_10.1007-978-0-85729-422-7 |edition=Second |publisher=Springer |isbn=978-1-85233-802-2 |ref=none}} * {{cite journal | last=Roy | first=Geoffrey G | title=Designing and explaining programs with a literate pseudocode | journal=Journal on Educational Resources in Computing | publisher=Association for Computing Machinery (ACM) | volume=6 | issue=1 | year=2006 | issn=1531-4278 | doi=10.1145/1217862.1217863 | page=1| s2cid=25810599 }} * {{cite conference | last1=Ulate-Caballero | first1=Bryan Alexander | last2=Berrocal-Rojas | first2=Allan | last3=Hidalgo-Cespedes | first3=Jeisson | title=2021 XLVII Latin American Computing Conference (CLEI) | chapter=Concurrent and Distributed Pseudocode: A Systematic Literature Review | publisher=IEEE | date=2021-10-25 | pages=1β10 | doi=10.1109/clei53233.2021.9640222| isbn=978-1-6654-9503-5 }} * {{cite book | last=Reisig | first=Wolfgang | chapter = Abstract State Machines for the Classroom | title=Logics of Specification Languages | publisher=Springer Berlin Heidelberg | series=Monographs in Theoretical Computer Science. An EATCS Series | year=2007 | isbn=978-3-540-74107-7 | chapter-url=https://books.google.com/books?id=pGrD8NtyR_EC&pg=PA23 | access-date=2023-10-05 | pages = 15β46}} * {{cite book | last1=Mitchell | first1=Joan L. | last2=Pennebaker | first2=William B. | last3=Fogg | first3=Chad E. | last4=LeGall | first4=Didier J. | title=MPEG Video Compression Standard | chapter=Pseudocode and Flowcharts | publisher=Springer US | publication-place=New York, NY | year=1996 | isbn=978-0-412-08771-4 | doi=10.1007/0-306-46983-9_6 | pages=105β116}} * {{cite journal | last=Bellamy | first=Rachel | title=What Does Pseudo-Code Do? A Psychological Analysis of the use of Pseudo-Code by Experienced Programmers | journal=Human-Computer Interaction | publisher=Informa UK Limited | volume=9 | issue=2 | date=1994-06-01 | issn=0737-0024 | doi=10.1207/s15327051hci0902_3 | pages=225β246}} ==External links== {{Wiktionary}} *[http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html A pseudocode standard] *[http://calgo.acm.org/ Collected Algorithms] of the [[Association for Computing Machinery|ACM]] *[http://www.cs.cornell.edu/Courses/cs482/2003su/handouts/pseudocode.pdf Pseudocode Guidelines], PDF file. {{Authority control}} [[Category:Articles with example pseudocode]] [[Category:Source code]] [[Category:Algorithm description languages]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Authority control
(
edit
)
Template:Category see also
(
edit
)
Template:Cite book
(
edit
)
Template:Cite conference
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Distinguish
(
edit
)
Template:More citations needed
(
edit
)
Template:Nowrap
(
edit
)
Template:Reflist
(
edit
)
Template:Sfn
(
edit
)
Template:Short description
(
edit
)
Template:Wiktionary
(
edit
)