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
Dangling else
(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!
==Description== In many [[programming language]]s, one may write conditionally executed code in two forms: the if-then form, or the if-then-else form. (The else clause is optional.): '''if''' a '''then''' s '''if''' b '''then''' s1 '''else''' s2 Ambiguous interpretation becomes possible when there are nested statements; specifically when an if-then-else form replaces the statement <code>s</code> inside the above if-then construct: '''if''' a '''then''' '''if''' b '''then''' s1 '''else''' s2 In this example, <code>s1</code> gets executed if and only if <code>a</code> is true ''and'' <code>b</code> is true. But what about <code>s2</code>? One person might be sure that <code>s2</code> gets executed whenever <code>a</code> is false (by attaching the ''else'' to the first ''if''), while another person might be sure that <code>s2</code> gets executed only when <code>a</code> is true ''and'' <code>b</code> is false (by attaching the ''else'' to the second ''if''). In other words, someone could interpret the previous statement as being equivalent to either of the following unambiguous statements: '''if''' a '''then''' { '''if''' b '''then''' s1 } '''else''' s2 '''if''' a '''then''' { '''if''' b '''then''' s1 '''else''' s2 } The dangling-else problem dates back to [[ALGOL 60]],<ref>{{cite journal |last1=Abrahams |first1=P. W. |title=A final solution to the Dangling else of ALGOL 60 and related languages |journal=Communications of the ACM |volume=9 |issue=9 |pages=679β682 |year=1966 |doi=10.1145/365813.365821 |doi-access=free |s2cid=6777841}}</ref> and subsequent languages have resolved it in various ways. In [[LR parser]]s, the dangling else is the archetypal example of a [[shift-reduce conflict]].
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)