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!
==[[Conditional (programming)|Selection]]: if== The selection (often called the "conditional statement" or "if statement") is a list of guarded commands, of which one is chosen to execute. If more than one guard is true, one statement whose guard is true is arbitrarily chosen to be executed. If no guard is true, the result is undefined, that is, equivalent to '''abort'''. Because at least one of the guards must be true, the empty statement '''skip''' is often needed. The statement '''if fi''' has no guarded commands, so there is never a true guard. Hence, '''if fi''' is equivalent to '''abort'''. ===Syntax=== '''if''' G0 β S0 β‘ G1 β S1 ... β‘ Gn β Sn '''fi''' ===Semantics=== Upon execution of a selection, the guards are evaluated. If none of the guards is ''true'', then the selection aborts, otherwise one of the clauses with a ''true'' guard is chosen arbitrarily and its statement is executed. ===Implementation=== GCL does not specify an implementation. Since guards cannot have [[Side effect (computer science)|side effects]] and the choice of clause is arbitrary, an implementation may evaluate the guards in any sequence and choose the first ''true'' clause, for example. ===Examples=== ====Simple==== In [[pseudocode]]: if a < b then set c to True else set c to False In guarded command language: '''if''' a > b β c := true β‘ a < b β c := false '''fi''' ====Use of skip==== In pseudocode: if error is True then set x to 0 In guarded command language: '''if''' error β x := 0 β‘ {{not}}error β '''skip''' '''fi''' If the second guard is omitted and error is False, the result is abort. ====More guards true==== '''if''' a β₯ b β max := a β‘ b β₯ a β max := b '''fi''' If a = b, either a or b is chosen as the new value for the maximum, with equal results. However, the [[implementation]] may find that one is easier or faster than the other. Since there is no difference to the programmer, any implementation will do.
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)