目录ide
自增量 测试
++放在右边 spa
++放在左边 调试
--在左边 orm
--在右边 xml
示例1 element
示例2 rem
示例3 get
++a; 自加1
--a; 自减1
自增量的做用是将变量的值加上1。
下面的2个示例演示了 变量++后对变量值的影响。
int number = 10; number++; Console.WriteLine(number); //number = 11 |
int number = 10; ++number; Console.WriteLine(number); //number = 11 |
下面演示变量++后对表达式值的影响
//放在后面++ int number = 10; int result = 10 + number++;
Console.WriteLine(number);//11 Console.WriteLine(result);//10+10=20 |
上面的代码至关于这样
//
// int result = 10 + number++;至关于
// int result = 10 + number;
// number++;
//++放在左边 int number = 10; int result = 10 + ++number;
Console.WriteLine(number);//11 Console.WriteLine(result);//10 + 11 =21
|
//
// int result = 10 + ++number;至关于
// ++number;
// int result = 10 + number;
减减同上
//后置-- int number = 10; number--;//9 --number;//8 8-1
int result = 10 + --number;//10+7
Console.WriteLine(number);//10-3 = Console.WriteLine(result);//17 Console.ReadKey(); |
number的值
这里的number不论-—放在它的左边仍是右边,结果都是将number的值自身减去1,这里出现了3次--,因此number的值 = 10 -3 = 7
result的值
在计算到--number时,number已经自身减去2了(number = 8),而--是放在number的左边的,使用以前要减1,因此结果为result = 10 + 7 =17
number的值
这里的number不论--放在它的左边仍是右边,结果都是将number的值自身减去1,这里出现了3次--,因此number的值 = 10 -3 = 7
result的值
result在使用number以前number的值已经改变了,(减了2次1),如今的number值为8。表达式中的number--因为是后置—-因此这个number的值不变依然是上面的8,
因此result = 10 + 8 =18
//--放在左面 int number = 10; number--; --number; int result = 10 + number--;
Console.WriteLine(number);//10-3 = Console.WriteLine(result);//10+8 = Console.ReadKey(); |
++和-- 一个是自增1、一个是自减1,原理同样这里只解释++对表达式的影响。
在表达式中(var=1):
<![if !supportLists]>1. <![endif]>++放在左边的(y=++var),会当即对当前的变量值 +1。这里的var就等于2了
<![if !supportLists]>2. <![endif]>++放在右边的面的(y=var++),不会对当前值有影响(即此处var=1)但若是后面 仍然有var变量时,则会对后面的var值+1。
int x = 9; int a = x++ + 11;
x = 在变量a中,x++出现一次,x自加1,此时x=10(9+1得来的)
a = 在a这个变量中,它的值是个表达式,x++出现了一次,它是后置的,因此它改变不了当前x的值(但能改变它后面的值,然然后面没有x变量),x =9 结果 a = 9 + 11 = 20
|
int x = 9; int b = ++x + 11;
x = 在变量a中,x++出现一次,不论左++、右++结果都是对x的值+1,此时x=10(9+1得来的)
a = 在变量b值表达式中,++x是在左边的因此,x值会自增1并当即改变,此处的x = 10 (9+1得来的) 结果b = 10 + 11 = 21
|
int x = 9; int c = x++ + 11 + x++;
x = 以变量c的表达式中,x++出现了2次,因此x自加1了2次, x = 9 + 1 + 1 = 11
c = 这要和示例1对比 示例1中的a = x++ + 11; 表达式中只有一个x++和一个11 x++不是加在本身身上的,而是加在别人身上的,示例1中的a后面没有x了,而本例是有x的,因此第一个x++自加后的值会变成后面的x++的值。
结果 c = 9 + 11 + 10 = 30
其实c的值和c = x++ + 11 + x;是同样的
|
int x = 9; int a = x++ + 11; int b = ++x + 11; int c = x++ + 11 + x++;
x = 代码从上至下执行,x自加1 出现了4次,因此 x=9+4=13
a = x++是右置的,所此值不变 a = 9+11 = 20
b = 注意在算b的值前,先搞清楚此时的x的值是多少,是9呢仍是10呢,很明显是10,应为a中的x已经自+1了一次,而且b中的++是在左边因此此处的x为11(10+1获得的),因此b的值 b = 11 + 11 = 22
c = 同b的求值同样,在算到x++以前,x的值已经自增了2次这时x=11, 再来算c中的表达式x++ + 11 + x++ = 11 + 11 + 12 = 34
|
若是你真的会,会对下面的测试很是感到自信,而且跃跃欲试,那么下面就来测试吧。
下面有5个测试若是你都作对了说明你真的明白了。
我看过不少的关于 ++放在左边和放在右边的区别,但并不彻底明白主要有如下3点
<![if !supportLists]>1. <![endif]>++var先加后用
<![if !supportLists]>2. <![endif]>var++ 先用后加
<![if !supportLists]>3. <![endif]>不论 左++ 、 右++结果都是对var的自身+1
我说不明白是由于当我对很长一段 ++ -- 计算时,结果算出来有时对有时不对。
这个问题甚少放了几个月了没解决,也许有1年了,昨天又花了6个小时终于搞明白了!
测试使用说明
请事先计算出结果,而后再运行对比结果。最下面有答案。
你真的能区分 ++ 放在左边和放在右边的区别吗? 若是你对下面的测试表示无聊、或者反感、或者对下面的测试感受惧怕,这说明你并不明白。有时候你能算对,那么是由于结果是知道答案前提下拼凑出来的。 |
staticvoid Main(string[] args) { int x = 7; int y = ++x + x--; Console.WriteLine("x = {0}", x); Console.WriteLine("y = {0}", y);
Console.ReadKey(); } |
x =
y =
staticvoid Main(string[] args) { int x = 7; int y = ++x + --x; Console.WriteLine("x = {0}", x); Console.WriteLine("y = {0}", y); Console.ReadKey(); } |
x =
y =
staticvoid Main(string[] args) { int x = 5; int y = x++ + 11 + x-- + ++x + 3 + x--; Console.WriteLine("x = {0}", x); Console.WriteLine("y = {0}", y); Console.ReadKey(); } |
x =
y =
staticvoid Main(string[] args) { int x = 5; int y = (x++) + 3 + (--x) + (++x) + 5;
Console.WriteLine("x = {0}", x); Console.WriteLine("y = {0}", y);
Console.ReadKey(); } |
x =
y =
若是你对我给出的结果表示怀疑能够自已在vs中测试
x=7
y=8+8 =16
x=7
y=8+7=15
x = 5
y = 5+ 11 + 6+ 6 + 3 + 6=37
tmp_x =6
x=6
y=5+3+5+6+5=24
代码调试专用
staticvoid Main(string[] args) { int x = 5; int y = (x++) + 3 + (--x) + (++x) + 5; //tmp_x =6 //x=6 //y=5+3+5+6+5=24 Console.WriteLine("x = {0}", x); Console.WriteLine("y = {0}", y);
Console.ReadKey(); } |