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 ...
随机推荐
- GPU的历史:从固定管线到可编程管线再到通用计算平台
开始的时候GPU不能编程,也叫固定管线的,就是把数据按照固定的通路走完. 和CPU同样作为计算处理器,顺理成章就出来了可编程的GPU,但是那时候想在GPU上编程可不是容易的事,你只能使用GPU汇编来写 ...
- GlusterFS 一
GlusterFS 一 1 安装源 yum install centos-release-gluster312.noarch 列出所有可用安装包yum list gluster* 安装glusterf ...
- 使用Object.create()实现继承
一.常见继承方式 我们日常开发中常见的继承方式主要有: 1.默认模式: Child.prototype = new Parent(); 2.借用构造函数: function Child(a, b, c ...
- Index--复合索引的思考1
在创建复合索引时,除了考虑索引键的选取外,还需考虑索引键的先后顺序.下面借助一些场景来讲解. 场景1表dbo.UserLoginStats记录每个用户每天的登录统计,目前表中存放10亿数据,每天新增数 ...
- Android studio 报错 installation failed with message failed to finalize session:INSTALL_FAILED_INVALID_APK 解决方法
解决方案: File->Setting->Build->Instant Run
- 使用 IIS 在 Windows 上托管 ASP.NET Core(Windows安装实践)
原文地址 https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.0&tabs= ...
- asp.net Frameset框架集的嵌套使用
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Frame.aspx.cs& ...
- .NetCore分布式部署中的DataProtection密钥安全性
在.NetCore中默认使用DataProtection来保护数据,例如Cooike等.一般情况下DataProtection生成的密钥会被加密后存储,例如默认的文件存储 可以看到使用了Windows ...
- SQL查询速度慢的原因分析和解决方案
SQL查询速度慢的原因分析和解决方案 查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建 ...
- 如何学习sql语言?
如何学习 SQL 语言? https://www.zhihu.com/question/19552975 没有任何基础的人怎么学SQL? https://www.zhihu.com/question/ ...