Chapter 14. Blocks and Statements
14.5. Statements
There are many kinds of statements in the Java programming language. Most correspond to statements in the C and C++ languages, but some are unique.
As in C and C++, the if
statement of the Java programming language suffers from the so-called "dangling else
problem," illustrated by this misleadingly formatted example:
if (door.isOpen()) if (resident.isVisible()) resident.greet("Hello!"); else door.bell.ring(); // A "dangling else"
The problem is that both the outer if
statement and the inner if
statement might conceivably own the else
clause. In this example, one might surmise that the programmer intended the else
clause to belong to the outer if
statement.
The Java programming language, like C and C++ and many programming languages before them, arbitrarily decrees that an else
clause belongs to the innermost if
to which it might possibly belong. This rule is captured by the following grammar:
Statement: StatementWithoutTrailingSubstatement LabeledStatement IfThenStatement IfThenElseStatement WhileStatement ForStatement StatementWithoutTrailingSubstatement: Block EmptyStatement ExpressionStatement AssertStatement SwitchStatement DoStatement BreakStatement ContinueStatement ReturnStatement SynchronizedStatement ThrowStatement TryStatement StatementNoShortIf: StatementWithoutTrailingSubstatement LabeledStatementNoShortIf IfThenElseStatementNoShortIf WhileStatementNoShortIf ForStatementNoShortIf
The following are repeated from §14.9 to make the presentation here clearer:
IfThenStatement: if ( Expression ) Statement IfThenElseStatement: if ( Expression ) StatementNoShortIf else Statement IfThenElseStatementNoShortIf: if ( Expression ) StatementNoShortIf else StatementNoShortIf
Statements are thus grammatically divided into two categories: those that might end in anif
statement that has no else
clause (a "short if
statement") and those that definitely do not.
Only statements that definitely do not end in a short if
statement may appear as an immediate substatement before the keyword else
in an if
statement that does have an else
clause.
This simple rule prevents the "dangling else
" problem. The execution behavior of a statement with the "no short if
" restriction is identical to the execution behavior of the same kind of statement without the "no short if
" restriction; the distinction is drawn purely to resolve the syntactic difficulty.
Chapter 14. Blocks and Statements的更多相关文章
- 零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II
原文:零元学Expression Blend 4 - Chapter 14 用实例了解布局容器系列-「Pathlistbox」II 本章将延续上一章的范例,步骤解析. 本章将延续上一章的范例,步骤解析 ...
- Chapter 14 G-estimation of Structural Nested Models
目录 14.1 The causal question revisited 14.2 Exchangeability revisited 14.3 Structural nested mean mod ...
- Chapter 14 观察者模式
观察者模式又叫做发布-订阅模式:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 观察者模式所做的工作其 ...
- Thinking in Java Chapter 14
From Thinking in Java 4th Edition RTTI(Run-Time Type Information),运行时类型信息,使得你可以在程序运行时发现和使用类型信息.对RTTI ...
- [using_microsoft_infopath_2010]Chapter 14高级话题
本章提要: 1.剖析表单逻辑 2.从多个表单中合并数据 3.重新连接XML表单到XSN模板文件 4.在repeating table中设置默认值 5.离线填写表单的权衡
- CHAPTER 1 Architectural Overview of Oracle Database 11g
Which SGA structures are required, and which are optional? The database buffer cache, log buffer, an ...
- Chapter 6 — Improving ASP.NET Performance
https://msdn.microsoft.com/en-us/library/ff647787.aspx Retired Content This content is outdated and ...
- Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services
本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...
- CHAPTER 7 Science in Islam 第7章 伊斯兰中的科学
CHAPTER 7 Science in Islam 第7章 伊斯兰中的科学 Galen did not live to see the decline of the Roman Empire, bu ...
随机推荐
- java并发编程实战:第十章----避免活跃性危险
在安全性和活跃性之间通常存在着某种制衡 一.死锁 定义:在线程A持有锁L并想获得锁M的同时,线程B持有锁M并尝试获得锁L,线程AB均不会释放自己的锁,那么这两个线程将永远地等待下去 在数据库系统的设中 ...
- Android-GsonUtil-工具类
GsonUtil-工具类 是把Google提供的Gons进行了方法封装,提供了关于一些常用的Gons使用的公共方法: package common.library.utils; import andr ...
- Mac提示App已损坏 你应该将它移到废纸篓的解决方案
现象 "Elmedia Player.app"已损坏,打不开. 您应该将它移到废纸篓. 原因 很多朋友们在安装软件时Mac OS系统出现提示"XXXApp 已损坏&quo ...
- nginx停止
- js加密解密 base64
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- LOJ#6045. 「雅礼集训 2017 Day8」价(最小割)
题面 传送门 题解 首先先把所有权值取个相反数来求最大收益,因为最小收益很奇怪 然后建图如下:\(S\to\)药,容量\(\inf+p_i\),药\(\to\)药材,容量\(\inf\),药材\(\t ...
- 《Spark MLlib 机器学习实战》1——读后总结
1 概念 2 安装 3 RDD RDD包含两种基本的类型:Transformation和Action.RDD的执行是延迟执行,只有Action算子才会触发任务的执行. 宽依赖和窄依赖用于切分任务,如果 ...
- CTF常见加密方式汇总
1.栅栏密码 在IDF训练营里做过一道关于栅栏密码的问题. 栅栏密码的解法很简单,也有点复杂,字符长度因数多得会有很多个密码.对,栅栏密码的解法就是:计算该字符串是否为合数,若为合数,则求出该合数除本 ...
- ListOrderedMap与Map的区别
ListOrderedMap位于commons-collections的jar包里,与普通的map相比,ListOrderedMap的key可保持原有顺序,在某些情况比如做报表的时候,非常实用. 代码 ...
- nginx实现动静分离--附nginx配置文件详解
转自http://www.cnblogs.com/1214804270hacker/p/9299462.html 一.认识访问静态资源与访问动态资源的区别 静态资源:指存储在硬盘内的数据,固定的数据, ...