首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

i++和++i的区别

Posted on 2008-03-12 19:25  停留的风  阅读(666)  评论(0编辑  收藏  举报

一组简单的计算题:

        int i=0 ,j=0;

        
int a=0, b=0;

        请计算: i 
= i++        求:   i=
                   a 
= i++              i=?,a=?
                   j 
= ++j               j=
                   b 
= ++j              j=?,b=?
测试用例:
        int i=0 ,j=0,k=0,p=0;

        
int a=0, b=0;

        Response.Write(
"i的初始值是:" + i.ToString());
        i 
= i++;
        Response.Write(
"=======算式(i=i++)=========");
        Response.Write(
"计算后的i是:" + i.ToString() + "\r\n");
        Response.Write(
"<br>");

        Response.Write(
"k的初始值是:" + k.ToString());
        a 
= k++;
        Response.Write(
"======算式(a=k++)========");
        Response.Write(
"计算后的a是:" + a.ToString() + ";计算后的k是:" + k.ToString() + "\r\n");
        Response.Write(
"<br>");



        Response.Write(
"j的初始值是:" + j.ToString());
        j 
= ++j;
        Response.Write(
"=====算式(j=++j)========");
        Response.Write(
"计算后的j是:" + j.ToString() + "\r\n");
        Response.Write(
"<br>");

        Response.Write(
"q的初始值是:" + p.ToString());
        b 
= ++p;
        Response.Write(
"=====算式(b=++p)========");
        Response.Write(
"计算后的b是:" + b.ToString() + ";计算后的p是:" + p.ToString() + "\r\n");
        Response.Write(
"<br>");

得出的结果:
i的初始值是:0=======算式(i=i++)=========计算后的i是:0 
k的初始值是:
0======算式(a=k++)========计算后的a是:0;计算后的k是:1 
j的初始值是:
0=====算式(j=++j)========计算后的j是:1 
q的初始值是:
0=====算式(b=++p)========计算后的b是:1;计算后的p是:1