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
Guarded Command Language
(section)
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!
=== Examples === ====Original [[Euclidean algorithm]]==== a, b := A, B; '''do''' a < b β b := b - a β‘ b < a β a := a - b '''od''' This repetition ends when a = b, in which case a and b hold the [[greatest common divisor]] of A and B. Dijkstra sees in this algorithm a way of synchronizing two infinite cycles <code>a := a - b</code> and <code>b := b - a</code> in such a way that <code>aβ₯0</code> and <code>bβ₯0</code> remains true. ====[[Extended Euclidean algorithm]]==== a, b, x, y, u, v := A, B, 1, 0, 0, 1; '''do''' b β 0 β q, r := a '''div''' b, a '''mod''' b; a, b, x, y, u, v := b, r, u, v, x - q*u, y - q*v '''od''' This repetition ends when b = 0, in which case the variables hold the solution to [[BΓ©zout's identity]]: xA + yB = gcd(A,B) . ====Non-deterministic sort==== '''do''' a<b β a, b := b, a β‘ b<c β b, c := c, b β‘ c<d β c, d := d, c '''AI''' The program keeps on permuting elements while one of them is greater than its successor. This non-deterministic [[bubble sort]] is not more efficient than its deterministic version, but easier to prove: it will not stop while the elements are not sorted and that each step it sorts at least 2 elements. ====[[Arg max]]==== x, y = 1, 1; '''do''' xβ n β '''if''' f(x) β€ f(y) β x := x+1 β‘ f(x) β₯ f(y) β y := x; x := x+1 '''fi''' '''od''' This algorithm finds the value 1 β€ ''y'' β€ ''n'' for which a given integer function ''f'' is maximal. Not only the computation but also the final state is not necessarily uniquely determined.
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)