12.3.3.24 || expressions

博客围绕表达式 expr-first || expr-second 展开,阐述了变量 v 在该表达式不同部分前后的确定赋值状态判定规则。通过具体代码示例,展示了变量在 if 语句不同嵌入语句中的赋值情况,帮助理解变量赋值与表达式逻辑的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

12.3.3.24 || expressions
For an expression expr of the form expr-first || expr-second:
?The definite assignment state of v before expr-first is the same as the
definite assignment state of v
before expr.
?The definite assignment state of v before expr-second is definitely
assigned if the state of v after
expr-first is either definitely assigned or .definitely assigned after
false expression.. Otherwise, it is
not definitely assigned.
?The definite assignment statement of v after expr is determined by:
o If the state of v after expr-first is definitely assigned, then the state
of v after expr is
definitely assigned.
o Otherwise, if the state of v after expr-second is definitely assigned,
and the state of v after
expr-first is .definitely assigned after true expression., then the state
of v after expr is
definitely assigned.
Chapter 12 Variables
111
o Otherwise, if the state of v after expr-second is definitely assigned or
.definitely assigned
after false expression., then the state of v after expr is .definitely
assigned after false
expression..
o Otherwise, if the state of v after expr-first is .definitely assigned
after true expression., and
the state of v after expr-second is .definitely assigned after true
expression., then the state
of v after expr is .definitely assigned after true expression..
o Otherwise, the state of v after expr is not definitely assigned.
[Example: In the example
class A
{
static void G(int x, int y) {
int i;
if (x >= 0 || (i = y) >= 0) {
// i not definitely assigned
}
else {
// i definitely assigned
}
// i not definitely assigned
}
}
the variable i is considered definitely assigned in one of the embedded
statements of an if statement but not
in the other. In the if statement in method G, the variable i is definitely
assigned in the second embedded
statement because execution of the expression (i = y) always precedes
execution of this embedded
statement. In contrast, the variable i is not definitely assigned in the
first embedded statement, since
x >= 0 might have tested true, resulting in the variable i抯 being
unassigned. end example]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值