Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Pseudocode

From Wikipedia, the free encyclopedia
Description of an algorithm that resembles a computer program
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Pseudocode" – news ·newspapers ·books ·scholar ·JSTOR
(August 2016) (Learn how and when to remove this message)

Incomputer science,pseudocode is a description of the steps in analgorithm using 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 forhuman 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 isaugmented withnatural language description details, where convenient, or with compactmathematical 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 andscientific publications to document algorithms and in planning of software and other algorithms.

No broad standard for pseudocodesyntax exists, 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 becompiled without errors.Flowcharts,drakon-charts andUnified Modelling Language (UML) charts can be thought of as a graphical alternative to pseudocode, but need more space on paper. Languages such asHAGGIS bridge the gap between pseudocode and code written in programming languages.

Application

[edit]

Pseudocode is commonly used in textbooks andscientific publications related tocomputer science andnumerical 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. Thistop-down structuring approach often starts with a pseudocode sketch refined into executable code. Pseudocode is also used in standardization; for example, theMPEG standards rely on formalC-like pseudocode, these standards cannot be understood without grasping the details of the code.[4]

Syntax

[edit]

Pseudocode generally does not actually obey thesyntax 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 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]

An example of pseudocode (for themathematical gamefizz buzz)

Pascal style:

procedurefizzbuzz;fori:=1to100doprint_number:=true;ifiisdivisibleby3thenbeginprint"Fizz";print_number:=false;end;ifiisdivisibleby5thenbeginprint"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=trueifiisdivisibleby3:print"Fizz"print_number=falseifiisdivisibleby5:print"Buzz"print_number=falseifprint_number:printiprintanewline
See also:Category:Articles with example pseudocode

Mathematical style pseudocode

[edit]

Innumerical computation, pseudocode often consists ofmathematical notation, typically frommatrix andset theory, mixed with the control structures of a conventional programming language, and perhaps alsonatural 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 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:

ReturnkSxk{\displaystyle \sum _{k\in S}x_{k}}

Normally non-ASCIItypesetting is used for the mathematical equations, for example by means of markup languages, such asTeX orMathML, 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

[edit]
Type of operationSymbolExample
Assignment← or :=c ← 2πr,c := 2πr
Comparison=, ≠, <, >, ≤, ≥
Arithmetic+, −, ×, /, mod
Floor/ceiling⌊, ⌋, ⌈, ⌉a ← ⌊b⌋ + ⌈c
Logicaland,or
Sums, productsΣ Πh ← ΣaA 1/a

Example

[edit]

The following is a longer example of mathematical-style pseudocode, for theFord–Fulkerson algorithm:

algorithm ford-fulkersonisinput: GraphG with flow capacityc,            source nodes,            sink nodetoutput: Flowf such thatf is maximal froms tot(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)inGEdof(u,v) ← 0f(v,u) ← 0while there exists a pathp froms totin the residual networkGfdo        letcf be the flow capacity of the residual networkGfcf(p) ← min{cf(u,v) | (u,v)inp}for each edge (u,v)inpdof(u,v)f(u,v) +cf(p)f(v,u) ← −f(u,v)returnf

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 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 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 formalspecification languages include set theory notation using special characters. Examples are:

Somearray programming languages include vectorized expressions and matrix operations as non-ASCII formulas, mixed with conventional control structures. Examples are:

See also

[edit]

References

[edit]
  1. ^Reisig 2007, p. 23, Pseudocode Programs and Their Semantics.
  2. ^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"
  3. ^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.
  4. ^Mitchell et al. 1996, p. 105.
  5. ^McConnell, Steve (2004).Code Complete. Pearson Education. p. 54.ISBN 978-0-7356-1967-8.Avoid syntactic elements from the target programming language
  6. ^Invitation to Computer Science, 8th Edition by Schneider/Gersting, "Keep statements language independent" as quotedin this stackexchange question
  7. ^Lamport, Leslie (2 January 2009)."The PlusCal Algorithm Language"(PDF). Microsoft Research. Retrieved28 May 2024.

Further reading

[edit]

External links

[edit]
Look uppseudocode in Wiktionary, the free dictionary.
Authority control databases: NationalEdit this at Wikidata
Retrieved from "https://en.wikipedia.org/w/index.php?title=Pseudocode&oldid=1281445340"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp