x++和++x的区别

版权声明:转载请注明 https://blog.csdn.net/qq_34720818/article/details/86502536

当他们是单独的式子的时候 ,没区别;但当他们在其余的表达式中的时候就有了。如下代码解释:

            int x = 5;
            int y = 5;
            //作为单独的式子,前++与后++没有区别,都是x+1
            x++;//x=x+1=6;
            ++y;//y=y+1=6;
            MessageBox.Show("作为单独的式子x++=" + x.ToString()+" ++y="+y.ToString(), "提示");

            x = 5;
            int temp = ++x;//先给x+1,再将加1后的值赋给temp,所以temp=6
            MessageBox.Show("++x = "+temp.ToString(),"提示");
           
            x = 5;
            temp = x++;//先将x的值赋给temp,再将x+1,所以temp=5
            MessageBox.Show("x++ =" + temp.ToString(), "提示");

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_34720818/article/details/86502536