有人可以告诉我为什么吗? (一元前后增量运算符 [英] Can someone tell me why? (Unary pre and post increment operator
本文介绍了有人可以告诉我为什么吗? (一元前后增量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请评估以下片段:
int a = 5;
int b;
b = a ++ + a;
Edit1-> Text = b;
a = 5;
b = a ++ + ++ a;
Edit2-> Text = b;
你能告诉我为什么分配给Edit1-> Text的b值是10
而值b分配给Edit2->文字是12?
如果可以的话,为什么不记录?
-
警告:
请勿回复此电子邮件
仅在此新闻组回复我
Please evaluate the following snippet:
int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;
Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?
And if you can, why isn''t it documented?
--
WARNING:
DO NOT REPLY TO THIS EMAIL
Reply to me only on this newsgroup
推荐答案
Andreas Sheriff发布:
Andreas Sheriff posted:
请评估以下片段:
int a = 5;
定义一个名为a的变量类型为int的并将其值设置为5.
int b;
定义变量:Name =" b"。 Type =" int"。值=没有特定值
b = a ++ + a;
" b"最终可能会有两个可能的值,或者:
10
或
11
因为标准并没有规定是否a ++或a或a。将首先完成。
变量a最终得到值6.
Edit1-> Text = b;
文本为10或11.
a = 5;
将a的值设为5.
b = a ++ + ++ a;
" b"获得:
12
或者是5 + 7或6 + 6。
" a"变成7.
Edit2-> Text = b;
文本设置为12.
你能告诉我为什么分配给Edit1-> Text的b的值分配给Edit2-> Text的b的值是12?
Please evaluate the following snippet:
int a=5;
Defines a variable called "a" of the type "int" and sets its value to 5.
int b;
Defines variable: Name = "b". Type = "int". Value = no particular value
b = a++ + a;
"b" could end up with two possible values here, either:
10
or
11
as the Standard doesn''t dictate whether "a++" or "a" will be done first.
The variable "a" ends up with the value 6.
Edit1->Text = b;
The text will be either 10 or 11.
a = 5;
Sets "a"''s value to 5.
b = a++ + ++a;
"b" gets:
12
either by "5 + 7" or "6 + 6".
"a" becomes 7.
Edit2->Text = b;
Text is set to 12.
Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?
欢迎你!
-JKop
You''re welcome!
-JKop
" Andreas Sheriff" < SP ********* @ iion.com>在消息中写道
news:noQ4d.289519
"Andreas Sheriff" <sp*********@iion.com> wrote in message
news:noQ4d.289519
Lj.229505@fed1read03 ...
Lj.229505@fed1read03...
请评估以下代码段:
int a = 5;
int b;
b = a ++ + a;
Edit1-> Text = b;
a = 5;
b = a ++ + ++ a;
Edit2-> Text = b;
你能告诉我为什么分配给Edit1-> Text的b的值是10
而分配给Edit2-> Text的b的值是12?
如果可以的话,为什么不记录?
- <警告:
请勿回复此电子邮件
仅在此新闻组回复我
这就是我的想法:
int a = 5;
int b;
b = a ++ + a;
1)b = 5 + 5
2)a ++在完成任务后完成,a = 6
Edit1-> Text = b;
a = 5;
b = a ++ + ++ a;
1)++ a在作业之前完成,a = 6
2)b = 6 + 6
3)a ++在作业完成后完成, a = 7
编辑2 - >文字= b;
Please evaluate the following snippet:
int a=5;
int b;
b = a++ + a;
Edit1->Text = b;
a = 5;
b = a++ + ++a;
Edit2->Text = b;
Can you tell me why the value of b assigned to Edit1->Text is 10
while the value of b assigned to Edit2->Text is 12?
And if you can, why isn''t it documented?
--
WARNING:
DO NOT REPLY TO THIS EMAIL
Reply to me only on this newsgroup
Here''s what I think happens:
int a=5;
int b;
b = a++ + a; 1) b = 5 + 5
2) a++ is done after the assignment, a = 6
Edit1->Text = b;
a = 5;
b = a++ + ++a; 1) ++a is done before the assignment, a = 6
2) b = 6 + 6
3) a++ is done after the assignment, a = 7
Edit2->Text = b;
PKH
PKH
这篇关于有人可以告诉我为什么吗? (一元前后增量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文