加加減減(你真的懂++--嗎) C#

 

 

目錄ide

自增量. 1 測試

++放在右邊. 1 spa

++放在左邊. 1 調試

--在左邊. 1 orm

--在右邊. 1 xml

++或者-- 放在左邊和放在右邊對錶達式的值影響. 1 ci

示例1 1 element

示例2 1 rem

示例3 1 get

你真的明白了嗎?. 1

測試1 1

測試2 1

測試3 1

測試4 1

答案. 1

測試1 1

測試2 1

測試3 1

測試4 1

 

 

 

 

 

自增量

++a; 自加

--a; 自減1

自增量的做用是將變量的值加上1

 

下面的2個示例演示了  變量++對變量值的影響。

int number = 10;

number++;

Console.WriteLine(number); //number = 11

 

 

int number = 10;

++number;

Console.WriteLine(number); //number = 11

 

 

 

 

下面演示變量++後對錶達式值的影響

 

++放在右邊

 number++ 放在表達式中則表示先使用number這個值,而後再把number的值+1

 

result的值是 10 +11仍是10 + 10呢?

//放在後面++

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的值已經改變了,(減了21),如今的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

示例1

 

int x = 9;

int a = x++ + 11;

 

x =

在變量a中,x++出現一次,x自加1,此時x=109+1得來的)

 

a =

a這個變量中,它的值是個表達式,x++出現了一次,它是後置的,因此它改變不了當前x的值(但能改變它後面的值,然然後面沒有x變量),x =9

結果 a = 9 + 11 = 20

 

 

 

示例2

 

int x = 9;

int b = ++x + 11;

 

x =

在變量a中,x++出現一次,不論++、右++結果都是對x的值+1,此時x=109+1得來的)

 

a =

在變量b值表達式中,++x是在左邊的因此,x值會自增1並當即改變,此處的x = 10 (9+1得來的)

結果b = 10 + 11 = 21

 

 

示例3

 

int x = 9;

int c = x++ + 11 + x++;

 

x =

   以變量c的表達式中,x++出現了2次,因此x自加12次,

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中的++是在左邊因此此處的x1110+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個小時終於搞明白了!

 

 

測試使用說明

請事先計算出結果,而後再運行對比結果。最下面有答案。

 

 

你真的能區分 ++ 放在左邊和放在右邊的區別嗎?

若是你對下面的測試表示無聊、或者反感、或者對下面的測試感受懼怕,這說明你並不明白。有時候你能算對,那麼是由於結果是知道答案前提下拼湊出來的

 

 

測試1

 

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 =

 

 

測試2

 

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 =

 

 

測試3

 

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 =

 

 

測試4

 

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中測試

測試1

x=7

y=8+8 =16

 

 

測試2

x=7

y=8+7=15          

測試3

x = 5

y = 5+ 11 + 6+   6 + 3 + 6=37

測試4

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();

     }

 

 



相關文章
相關標籤/搜索