`
talin2010
  • 浏览: 520727 次
  • 性别: Icon_minigender_1
  • 来自: 河北
社区版块
存档分类
最新评论

15.7 Selection statements

阅读更多
Selection statements select one of a number of possible statements for
execution based on the value of some
expression.
selection-statement:
if-statement
switch-statement
15.7.1 The if statement
The if statement selects a statement for execution based on the value of a
boolean expression.
if-statement:
if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement
boolean-expression:
expression
An else part is associated with the lexically nearest preceding if that is
allowed by the syntax. [Example:
Thus, an if statement of the form
if (x) if (y) F(); else G();
is equivalent to
Chapter 15 Statements
183
if (x) {
if (y) {
F();
}
else {
G();
}
}
end example]
An if statement is executed as follows:
? The boolean-expression (§14.16) is evaluated.
? If the boolean expression yields true, control is transferred to the
first embedded statement. When and
if control reaches the end point of that statement, control is transferred
to the end point of the if
statement.
? If the boolean expression yields false and if an else part is present,
control is transferred to the
second embedded statement. When and if control reaches the end point of
that statement, control is
transferred to the end point of the if statement.
? If the boolean expression yields false and if an else part is not
present, control is transferred to the
end point of the if statement.
The first embedded statement of an if statement is reachable if the if
statement is reachable and the
boolean expression does not have the constant value false.
The second embedded statement of an if statement, if present, is reachable
if the if statement is reachable
and the boolean expression does not have the constant value true.
The end point of an if statement is reachable if the end point of at least
one of its embedded statements is
reachable. In addition, the end point of an if statement with no else part
is reachable if the if statement is
reachable and the boolean expression does not have the constant value true.
15.7.2 The switch statement
The switch statement selects for execution a statement list having an
associated switch label that corresponds
to the value of the switch expression.
switch-statement:
switch ( expression ) switch-block
switch-block:
{ switch-sectionsopt }
switch-sections:
switch-section
switch-sections switch-section
switch-section:
switch-labels statement-list
switch-labels:
switch-label
switch-labels switch-label
switch-label:
case constant-expression :
default :
A switch-statement consists of the keyword switch, followed by a
parenthesized expression (called the
switch expression), followed by a switch-block. The switch-block consists
of zero or more switch-sections,
C# LANGUAGE SPECIFICATION
184


enclosed in braces. Each switch-section consists of one or more
switch-labels followed by a statement-list
(§15.2.1).
The governing type of a switch statement is established by the switch
expression. If the type of the switch
expression is sbyte, byte, short, ushort, int, uint, long, ulong, char,
string, or an enum-type,
then that is the governing type of the switch statement. Otherwise, exactly
one user-defined implicit
conversion (§13.4) must exist from the type of the switch expression to
one of the following possible
governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char,
string. If no such
implicit conversion exists, or if more than one such implicit conversion
exists, a compile-time error occurs.
The constant expression of each case label must denote a value of a type
that is implicitly convertible
(§13.1) to the governing type of the switch statement. A compile-time
error occurs if two or more case
labels in the same switch statement specify the same constant value.
There can be at most one default label in a switch statement.
A switch statement is executed as follows:
? The switch expression is evaluated and converted to the governing type.
? If one of the constants specified in a case label in the same switch
statement is equal to the value of
the switch expression, control is transferred to the statement list
following the matched case label.
? If none of the constants specified in case labels in the same switch
statement is equal to the value of
the switch expression, and if a default label is present, control is
transferred to the statement list
following the default label.
? If none of the constants specified in case labels in the same switch
statement is equal to the value of
the switch expression, and if no default label is present, control is
transferred to the end point of the
switch statement.
If the end point of the statement list of a switch section is reachable, a
compile-time error occurs. This is
known as the ?no fall through? rule. [Example: The example
switch (i) {
case 0:
CaseZero();
break;
case 1:
CaseOne();
break;
default:
CaseOthers();
break;
}
is valid because no switch section has a reachable end point. Unlike C and
C++, execution of a switch
section is not permitted to ?fall through? to the next switch section, and
the example
switch (i) {
case 0:
CaseZero();
case 1:
CaseZeroOrOne();
default:
CaseAny();
}
results in a compile-time error. When execution of a switch section is to
be followed by execution of another
switch section, an explicit goto case or goto default statement must be
used:
Chapter 15 Statements
185
switch (i) {
case 0:
CaseZero();
goto case 1;
case 1:
CaseZeroOrOne();
goto default;
default:
CaseAny();
break;
}
end example]
Multiple labels are permitted in a switch-section. [Example: The example
switch (i) {
case 0:
CaseZero();
break;
case 1:
CaseOne();
break;
case 2:
default:
CaseTwo();
break;
}
is valid. The example does not violate the ?no fall through? rule because
the labels case 2: and default:
are part of the same switch-section. end example]
[Note: The ?no fall through? rule prevents a common class of bugs that
occur in C and C++ when break
statements are accidentally omitted. In addition, because of this rule, the


switch sections of a switch
statement can be arbitrarily rearranged without affecting the behavior of
the statement. For example, the
sections of the switch statement above can be reversed without affecting
the behavior of the statement:
switch (i) {
default:
CaseAny();
break;
case 1:
CaseZeroOrOne();
goto default;
case 0:
CaseZero();
goto case 1;
}
end note]
[Note: The statement list of a switch section typically ends in a break,
goto case, or goto default
statement, but any construct that renders the end point of the statement
list unreachable is permitted. For
example, a while statement controlled by the boolean expression true is
known to never reach its end
point. Likewise, a throw or return statement always transfers control
elsewhere and never reaches its end
point. Thus, the following example is valid:
switch (i) {
case 0:
while (true) F();
case 1:
throw new ArgumentException();
case 2:
return;
}
end note]
[Example: The governing type of a switch statement may be the type string.
For example:
C# LANGUAGE SPECIFICATION
186
void DoCommand(string command) {
switch (command.ToLower()) {
case "run":
DoRun();
break;
case "save":
DoSave();
break;
case "quit":
DoQuit();
break;
default:
InvalidCommand(command);
break;
}
}
end example]
[Note: Like the string equality operators (§14.9.7), the switch statement
is case sensitive and will execute a
given switch section only if the switch expression string exactly matches a
case label constant. end note]
When the governing type of a switch statement is string, the value null is
permitted as a case label
constant.
The statement-lists of a switch-block may contain declaration statements (§1
5.5). The scope of a local
variable or constant declared in a switch block is the switch block.
Within a switch block, the meaning of a name used in an expression context
must always be the same
(§14.5.2.1).
The statement list of a given switch section is reachable if the switch
statement is reachable and at least
one of the following is true:
? The switch expression is a non-constant value.
? The switch expression is a constant value that matches a case label in
the switch section.
? The switch expression is a constant value that doesn?t match any case
label, and the switch section
contains the default label.
? A switch label of the switch section is referenced by a reachable goto
case or goto default
statement.
The end point of a switch statement is reachable if at least one of the
following is true:
? The switch statement contains a reachable break statement that exits the
switch statement.
? The switch statement is reachable, the switch expression is a
non-constant value, and no default
label is present.
? The switch statement is reachable, the switch expression is a constant
value that doesn?t match any
case label, and no default label is present.
分享到:
评论

相关推荐

    MATLAB课件:ch5_selection_statements.pdf

    在MATLAB课件“ch5_selection_statements.pdf”中,主要讲解了如何使用`if`语句以及其扩展形式`if-else`语句来实现条件判断。 1. `if`语句: `if`语句用于检查一个条件是否为真,如果条件满足,就执行相应的代码块...

    mybatisMapped报错 Statements collection does not contain value for

    ### mybatisMapped报错 Statements collection does not contain value for 在使用MyBatis框架进行数据库操作时,可能会遇到“Mapped Statements collection does not contain value for”这样的错误提示。这通常...

    pg_stat_statements.so(centos 7.3 x64)

    复制到 pgsql-12/lib 目录下就...shared_preload_libraries = 'timescaledb, pg_stat_statements' # (change requires restart) pg_stat_statements.max = 10000 pg_stat_statements.track = all 最后重启postgrelSQL

    HANA SQLStatements ALL

    标题 "HANA SQLStatements ALL" 指的是 SAP HANA 数据库系统中的 SQL 语句大全,这通常涵盖了用于各种报告的 SQL 查询和操作。在 SAP HANA 中,SQL(结构化查询语言)是用于管理和处理数据库的标准语言,它允许用户...

    SQLStatements.zip

    标题“SQLStatements.zip”暗示了这个压缩包包含与SQL语句相关的资料,特别是针对SAP HANA数据库管理系统。描述提到“HANA script for Admin”,这表明这些内容是为HANA数据库管理员准备的,可能涉及管理和优化...

    5_thethreefinancial_statements_

    标题 "5_thethreefinancial_statements_" 暗示了我们关注的核心是财务报表的三大主要组成部分:资产负债表(Balance Sheet)、利润表(Income Statement)和现金流量表(Cash Flow Statement)。这三份报表共同提供...

    IntroSQL_V4_All_SQL_Statements

    从给定的文件信息中,我们可以提取到一系列与MySQL数据库管理相关的知识点,这些知识点主要集中在数据库的创建、用户管理、权限授予以及数据表的构建上。以下是对这些知识点的详细阐述: ### 数据库用户管理 ...

    2017 Statutory Annual Report including IFRS Financial Statements

    根据提供的文件信息,我们可以从这份2017年度法定报告中提炼出以下几个关键知识点: ### 一、公司概况 #### 1.1 公司历史与发展 - **STMicroelectronics N.V.** 是一家全球领先的半导体公司。...

    Structured Programming with go to Statements

    ### 结构化编程与goto语句:一场历史的辩论 #### 概述 在计算机科学领域,关于结构化编程(Structured Programming)与goto语句的争议堪称经典,它不仅是编程语言设计史上的一个重要篇章,也深刻影响了软件工程的...

    C++语句和程序结构(英文)

    * 选择语句(Selection statements):用于根据条件选择执行不同的操作,例如if-else语句、switch语句等。 * 迭代语句(Iteration statements):用于重复执行某些操作,例如for循环、while循环等。 * Try块语句:...

    02 - Financial Statements, Cash Flow, and Taxes

    ### 金融报表、现金流与税收 #### 一、引言 在投资领域,尤其是在寻找具有增长潜力的投资标的时,理解并分析企业的财务状况至关重要。彼得·林奇(Peter Lynch),这位曾管理过价值100亿美元的富达麦哲伦基金...

    SQL-statements-Collection.rar_Grant_sql statements

    下列语句部分是MsSql语句,不可以在access中使用。 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控制语言(GRANT,REVOKE,COMMIT...

    inconsistent return statements(解决方案).md

    inconsistent return statements(解决方案).md

    Textual Analysis of Customer Statements for Quality Control

    Textual Analysis of Customer Statements for Quality Control and Help Desk Support

    Introduction to Programming Using Python (英文版)

    statements, loops, and functions, before moving into defining classes. Students learn basic logic and programming concepts before moving into object-oriented programming, and GUI programming. Another ...

    .predicates-quantified-statements-1.np

    .predicates-quantified-statements-1.np

    pg_log_statements:PostgreSQL扩展,用于记录特定数据库会话SQL语句

    pg_log_statements pg_log_statements是一个PostgreSQL扩展,它允许记录特定数据库会话SQL语句:可以为特定服务器进程设置log_statement ,而不是在实例级别或数据库级别设置log_statement参数。安装编译中可以使用...

    Laravel开发-bank-statements-wrapper

    【Laravel开发-bank-statements-wrapper】是一个专为在 Laravel 框架中处理银行交易数据的API包装器。这个项目旨在简化与银行服务API的交互,帮助开发者更高效地集成银行对账单数据到自己的应用程序中。通过使用此...

Global site tag (gtag.js) - Google Analytics