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!
===Avoiding ambiguity by changing the syntax=== The problem can also be solved by making explicit the link between an else and its if, within the syntax. This usually helps avoid human errors.<ref>[https://code.google.com/archive/p/copute/issues/21#c2 Ambiguity of dangling else: non-context-free grammars are semantically opaque]</ref> Possible solutions are: * Having an "end if" symbol delimiting the end of the if construct. Examples of such languages are [[ALGOL 68]], [[Ada (programming language)|Ada]], [[Eiffel (programming language)|Eiffel]], [[PL/SQL]], [[Visual Basic]], [[Modula-2]], and [[AppleScript]]. * Disallowing the statement following a "then" to be an "if" itself (it may however be a pair of statement brackets containing only an if-then-clause). Some implementations of [[ALGOL 60]] follow this approach.<ref>''4.5.1 Conditional Statements β Syntax'' in P. Nauer (ed.), ''Revised Report on the Algorithmic Language ALGOL 60'', CACM 6,1, 1963 pp. 1-17</ref> * Requiring braces (parentheses) when an "else" follows an "if".<ref>[https://code.google.com/archive/p/copute/issues/21#c1 Ambiguity of dangling else: require braces when else follows if]</ref> * Requiring every "if" to be paired with an "else". To avoid a similar problem concerning semantics rather than syntax, [[Racket (programming language)|Racket]] deviates from [[Scheme (programming language)|Scheme]] by considering an <code>if</code> without a fallback clause to be an error, effectively distinguishing conditional ''expressions'' (i.e <code>if</code>) from conditional ''statements'' (i.e <code>when</code> and <code>unless</code>, which do not have fallback clauses). * Using different keywords for the one-alternative and two-alternative "if" statements. [[S-algol]] uses <code>if e do s</code> for the one-alternative case and <code>if e1 then e2 else e3</code> for the general case.<ref>{{citation |last=Davie |first=Antony J. T. |author2=Ronald Morrison |editor=Brian Meek |title=Recursive Descent Compiling |series=Ellis Horwood series in computers and their applications |year=1981 |publisher=Ellis Horwood |location=Chichester, West Sussex |isbn=0-470-27270-8 |page=20}}</ref> * Requiring braces unconditionally, like [[Swift (programming language)|Swift]] and [[Rust (programming language)|Rust]]. This is effectively true in [[Python (programming language)|Python]] as its indentation rules delimit every block, not just those in "if" statements.
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)