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
Short-circuit evaluation
(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!
== Definition == In any programming language that implements short-circuit evaluation, the expression <code>''x'' and ''y''</code> is equivalent to the [[Conditional (programming)|conditional expression]] <code>if ''x'' then ''y'' else ''x''</code>, and the expression <code>''x'' or ''y''</code> is equivalent to <code>if ''x'' then ''x'' else ''y''</code>. In either case, ''x'' is only evaluated once. The generalized definition above accommodates loosely typed languages that have more than the two [[truth-value]]s <code>True</code> and <code>False</code>, where short-circuit operators may return the last evaluated subexpression. This is called "last value" in the table below. For a strictly-typed language, the expression is simplified to <code>if ''x'' then ''y'' else '''false'''</code> and <code>if ''x'' then '''true''' else ''y''</code> respectively for the boolean case. === Precedence === Although {{code|AND}} takes [[operator precedence|precedence]] over {{code|OR}} in many languages, this is not a universal property of short-circuit evaluation. An example of the two operators taking the same precedence and being [[left-associative]] with each other is [[POSIX shell]]'s command-list syntax.<ref>{{cite web |title=Shell Command Language |url=https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html |website=pubs.opengroup.org}}</ref>{{rp|at=Β§2.9.3}} The following simple left-to-right evaluator enforces a precedence of {{code|AND}} over {{code|OR}} by a {{code|continue}}: '''function''' short-circuit-eval (''operators'', ''values'') '''let''' ''result'' := True '''for each''' (''op'', ''val'') in (''operators'', ''values''): '''if''' ''op'' = "AND" && ''result'' = False '''continue''' '''else if''' ''op'' = "OR" && ''result'' = True '''return''' ''result'' '''else''' ''result'' := ''val'' '''return''' ''result'' === Formalization === Short-circuit logic, with or without side-effects, have been formalized based on [[Hoare logic#Conditional rule|Hoare's conditional]]. A result is that non-short-circuiting operators can be defined out of short-circuit logic to have the same sequence of evaluation.<ref>{{cite arXiv |last1=Bergstra |first1=Jan A. |last2=Ponse |first2=A. |last3=Staudt |first3=D.J.C. |date=2010 |title=Short-circuit logic |eprint=1010.3674|class=cs.LO}}</ref>
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)