C题中的条件运算符
我只是有一个关于条件运算符的简单问题。这里仍然是一个崭露头角的程序员。 给定 x = 1、y = 2 和 z = 3。
我想知道为什么在这个语句之后:
y += x-- ? z++ : --z;
y 是 5。语句之后的值是 x = 0、y = 5 和 z = 4。 我知道条件运算符的工作方式是它的格式如下: 变量=条件? true 时的值: false 时的值。
对于条件 y += x-- ,y 如何变成 5?我只能看到 2 (2 += 0) 和 3 (2 += 1)(然后 x-- 变为零)作为可能性。非常感谢任何帮助。 :)
I just have a quick question about the conditional operator. Still a budding programmer here.
I am given x = 1, y = 2, and z = 3.
I want to know, why after this statement:
y += x-- ? z++ : --z;
That y is 5. The values after the statement are x = 0, y = 5, and z = 4.
I know the way the conditional operator works is that it is formatted like this:
variable = condition ? value if true : value if false.
For the condition, y += x-- , how does y become 5? I can only see 2 (2 += 0) and 3 (2 += 1)(then x-- becomes zero) as possibilities. Any help is much appreciated. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
当它计算条件 (x != 0) 时,x 仍然是 1(即
不是 0
)。所以它选择了z++
。仍然是 3。2 + 3 = 5。最终 x 变成了 0,z 变成了 4。看看 此处了解详细信息。记住一件简单的事情很重要:当您说
x ++
时,将使用x的当前值,并且然后它会递增。当您说++x
时,它首先会递增,然后然后使用。When it evaluates the condition (x != 0) x is still 1 (that is
not 0
). So it picksz++
. Which is still 3. 2 + 3 = 5. At the end of the day x has become 0 and z has become 4.Take a look here for details. It's important to remember a simple thing: when you say
x ++
the current value of x is used and then it is incremented. When you say++x
it is first incremented and then used.运算符 ?: 的优先级高于运算符 +=。那么你的表达式被计算为
x-- 的值? z++ : --z 表达式是表达式
z++
的值(即 3),因为表达式x--
的值为 1The Operator ?: has higher precedence than the operator +=. So Your expression is evaluated as
the value of
x-- ? z++ : --z
expression is the value of the expressionz++
(that is 3) because the value of the expressionx--
is 1只需将其分解为类似的
if
语句:在您的情况下,由于
x
是1
,因此您将采用“true”一侧这个 if 语句。这意味着您将z++
添加到y
,得到3 + 2
,结果是5
。请不要编写这样的代码。
Just break it down into a similar
if
statement:In your case, since
x
is1
, you'll take the "true" side of this if statement. That means you're addingz++
toy
, giving3 + 2
, resulting in5
.Please don't write code like this.
作为一个初露头角的程序员,你要知道你永远不应该写这样的东西,这样你就可以忘记它让你担心!
As a budding programmer just know that you should NEVER write anything like this so that way you can forget about it worrying you!
其原因是后递减/递增运算符(
x++
或x--
)执行以下操作:所以
x--
的返回值为1,表示true,所以语句z++
被求值,返回原始值3。因为
y = 2,<代码>y += 3是<代码>5。
The reason for this is that post-decrement/increment operator (
x++
orx--
) does the following:So the return value of
x--
is 1, indicating true, so the statementz++
is evaluated, returning the original value 3.Since
y = 2
,y += 3
is5
.x-- 表示表达式以 x 的当前值求值,然后 x 减 1。Z++ 的情况也是如此。这与 --z 的情况相反,这意味着这是根据 z 的新值进行计算的。
因此,在求值时,x 为 1,z 为 3。而在表达式求值之后,x 变为 0,z 变为 4; y = 2 + 3 = 5
x-- would mean the expression is evaluated at current value of x and after that x is reduced by 1. Same is the case with Z++. This is the other way around for --z, which means this is evaluated at the new value of z.
So at the time of evaluation x is 1, z is 3. and after the evaluation of expression x becomes 0 and z 4; and y = 2 + 3 = 5
请记住,递增和递减运算符返回不同的内容,具体取决于它们是放置在变量名称之前还是之后。
特别是,当计算
x--
时,它会将x
减1,但返回未修改的x,在本例中为 1。在 C 中,1 的计算结果为 true,因此三元运算符将返回
z++
。同样,由于 ++ 运算符位于变量后面,因此
z++
的返回值是z
未修改的值,即 3。因此,这可以归结为
y += 3
,结果y
为 5。Remember that the increment and decrement operators return different things depending on whether they're placed before or after the name of a variable.
In particular, when
x--
is evaluated, it decreasesx
by 1, but returns the unmodified value ofx
, which in this case is 1. In C, 1 evaluates to true, so the ternary operator will returnz++
.And again, because the ++ operator is placed after the variable, the return value of
z++
is the unmodified value ofz
, which is 3.Thus, this comes down to
y += 3
, which results iny
being 5.x-- 和 z++ 在使用后递减和递增。当计算三元运算符时,您最终会得到以下结果:
y += (1) ? (3):(--z);
--z 永远不会被调用,条件计算结果为 true 并执行三元运算符中的第一个选项。使用后,x 递减,z 递增。
x-- and z++ decrement and increment after they are used.You end up with the following when the ternary operator is evaluated:
y += (1) ? (3) : (--z);
--z never gets called, the conditional evaluates to true and executes the first option in the ternary operator. After use, x is then decremented, and z is incremented.
这是正常的,因为它首先“运行”三元运算符,然后递减,因为你的减量运算符(x--)是后缀,所以你得到的 z++ 是 3,所以你在 y 中有 5。
It is normal because it first "runs" ternary operator then does decrement as your decrement operator (x--) is postfix, so you got z++ which is 3 so you have 5 in y.
表达式
x--
的计算结果为x
的当前值,即 1。因此,条件表达式的结果为z++
,其计算结果为到 3。 3 被添加到y
上,总共 5。The expression
x--
evaluates to the current value ofx
, which is 1. Thus the result of the conditional expression isz++
, which evaluates to 3. 3 gets added toy
, for a total of 5.我认为你的根本问题是你假设 y+= x-- 是你的条件,而实际上你的条件只是 x-- 。条件运算符的返回使
y +=
成为条件运算的结果:x-- ? z++ : --z;
是 5。其他注释说明了它实际计算结果为 5 的原因。I think your fundamental issue here is that you're assuming
y+= x--
is your condition, when in fact your condition is merelyx--
. There is a return from the conditional operator which makesy +=
the result of the conditional operation:x-- ? z++ : --z;
is 5. Other comments have the reason why it actually evaluates to 5.y += (x-- ? z++ : --z);所以这是你的问题,答案很简单......
我们知道像 X-- 或 x++ 这样的东西被称为后增量或后减量。因此,根据后自增或自减的规则,表达式将首先被求值,然后只有自增或自减才会起作用。即首先评估,然后增加或减少......
现在让我们解决你的问题:
Y+=X--?Z++:--Z......现在它包含三个部分,即左,中和右......现在考虑的要点是:“如果左边部分为真,那么它将返回中间部分,否则右边部分......并且执行总是从左边部分开始,因为它是条件部分”
现在将语句简化为:Y+=X? Z:Z;...现在看看左边部分是否有前置或后增量或减量.....如果后++/--是der den首先评估简化的语句......den去++/--.....
现在左边部分有后减量...所以让我们首先评估表达式...即
y+=1:3:3 //条件部分中的任何非零值都是 true条件(即1)
所以现在我们的条件为真,它将返回中间部分,当控制转到中间部分时,只有x值会递减,即它变成0....
现在第二个简化语句是Y+= Z。 (\\因为条件为真并且我们得到了中间部分,编译器将跳过其余部分,即右侧部分。)
现在观察 Z 是否为 post ++/-- (或)pre ++/--) ...hahh ..其后增量..所以只需首先计算简化语句2,然后增加Z的值....即
现在计算表达式,即Y=5,所以现在增加Z的值,即它变成4
y += (x-- ? z++ : --z); so this is your question and the answer is simple................
As we know that something like X-- Or x++ are called post increment or decrement. So according to the rules of post increment or decrement the expression will be evaluated first and then only increment or decrement will come into action. i.e first evaluate and then increase or decrease.....
NOW lets solve your question:
Y+=X--?Z++:--Z....now it is containing three parts i.e left,middle and right...now the point of consideration is:"if left part is true then it will return middle part, otherwise right side part...and execution always starts from left part as it is the condition part"
Now simplify the statement as:Y+=X?Z:Z;....Now see whether left part is having pre or post increment or decrement.....if post ++/-- is der den first evaluate the simplified statement......den go for ++/--.....
Now left part is having post decrement...so lets first evaluate the expression...i.e
y+=1:3:3 //any non zero value in the condition part is a true condition(i.e 1)
so now our condition is true and it will return the middle part and when the control goes to middle part at that time only x value will be decremented i.e it becomes 0....
Now 2nd simplified statement is Y+=Z. (\\as condition is true and we got middle part,compiler will skip rest of the part i.e right part.)
Now observe whether Z is post ++/-- (or)pre ++/--) ...hahh..its post increment ..so simply first evaluate the simplified statement2 and then increase the value of Z....i.e
Now the expression is evaluated i.e Y=5,so now increment the value of Z i.e it become 4
我们知道,运算符会检查条件,如果条件为 true 即 1,则执行 true 语句。
如果它是
false ie 0
它执行 false 语句由于我们正在将
x 的值初始化为 1
它会执行true
语句因此它会公开结果
5
来自真实部分y=(y+z++)
As we know operators checks condition if it is
true i.e 1
it executes true statement.If it is
false i.e 0
it executes false statementBeing we are initialising value of
x to 1
it executestrue
statementAs a result it exposes result
5
from true party=(y+z++)