为了账号安全,请及时绑定邮箱和手机立即绑定

'为什么i++放在sum上面就变成5150

#include <stdio.h>

int main()

{

    int i,sum=0;

    i=1;

    while(   i<=100  )  //循环条件应该是什么呢?

    {

        i++;

        sum=sum+i;

  

                 //这里是不是应该改变循环变量的值

    }

    printf("100以内所有整数之和为:%d\n", sum);

    return 0;

}


正在回答

3 回答

http://physics.scu.edu.cn/search ... 5%32%2E%38%74%6B%62
http://physics.scu.edu.cn/search ... 5%32%2E%38%61%73%77
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%75%66
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%79%7A
http://physics.scu.edu.cn/search ... 5%32%2E%38%76%66%61
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%74%61
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%62
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%79
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6D%67
http://physics.scu.edu.cn/search ... 5%32%2E%38%74%6E%65
http://physics.scu.edu.cn/search ... 5%32%2E%38%79%67%6D
http://physics.scu.edu.cn/search ... 5%32%2E%38%66%76%71
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6B%6C
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%71
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%7A%72
http://physics.scu.edu.cn/search ... 5%32%2E%38%7A%74%6B
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%72%63
http://physics.scu.edu.cn/search ... 5%32%2E%38%64%70%74
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%69
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%70
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%69
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%79
http://physics.scu.edu.cn/search ... 5%32%2E%38%6F%79%78
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%77
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6A%68
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6F%6D
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%6D
http://physics.scu.edu.cn/search ... 5%32%2E%38%6E%6F%76
http://physics.scu.edu.cn/search ... 5%32%2E%38%65%6E%69
http://physics.scu.edu.cn/search ... 5%32%2E%38%79%6B%6F
http://physics.scu.edu.cn/search ... 5%32%2E%38%77%79%64
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%78
http://physics.scu.edu.cn/search ... 5%32%2E%38%69%73%6A
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%76
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%74
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%68%77
http://physics.scu.edu.cn/search ... 5%32%2E%38%65%6D%63
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6B%72
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6C%6F
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%64
http://physics.scu.edu.cn/search ... 5%32%2E%38%6D%66%68
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%6D
http://physics.scu.edu.cn/search ... 5%32%2E%38%7A%6D%6F
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%6A
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%62
http://physics.scu.edu.cn/search ... 5%32%2E%38%75%74%6F
http://physics.scu.edu.cn/search ... 6%AD%A5%32%2E%38%79
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%67%72
http://physics.scu.edu.cn/search ... 5%32%2E%38%77%70%72
http://physics.scu.edu.cn/search ... D%A5%32%2E%38%6A%78
http://physics.scu.edu.cn/search ... 5%32%2E%38%64%72%68
http://www.ie.sjtu.edu.cn/?s=%E5 ... 5%32%2E%38%72%68%75
http://physics.scu.edu.cn/search ... 5%32%2E%38%69%67%6F
http://www.ie.sjtu.edu.cn/?s=%E7 ... 5%32%2E%38%65%63%77


0 回复 有任何疑惑可以回复我~


https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%65%6E
https://www.oschina.net/search?i ... 5%32%2E%38%62%73%67
https://www.qidian.com/soushu/%E ... 5%32%2E%38%68%69%62
https://www.oschina.net/search?i ... D%A5%32%2E%38%76%69
https://www.qidian.com/soushu/%E ... 5%32%2E%38%76%76%66
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%62
https://www.oschina.net/search?i ... 5%32%2E%38%6E%6E%6D
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%6D
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%73
https://www.oschina.net/search?i ... 5%32%2E%38%78%63%6B
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6F%66%73
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%73
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%66%75
https://www.oschina.net/search?i ... 5%32%2E%38%68%6D%69
https://www.qidian.com/soushu/%E ... 5%32%2E%38%73%6B%67
https://www.oschina.net/search?i ... D%A5%32%2E%38%61%7A
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%77%6A
https://www.oschina.net/search?i ... 5%32%2E%38%78%6F%76
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6F
https://www.qidian.com/soushu/%E ... 5%32%2E%38%63%6C%6C
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6D
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%73
https://www.qidian.com/soushu/%E ... 5%32%2E%38%67%7A%73
https://www.oschina.net/search?i ... 5%32%2E%38%72%77%75
https://www.oschina.net/search?i ... 5%32%2E%38%75%64%62
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6F
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%6B
https://www.oschina.net/search?i ... D%A5%32%2E%38%69%79
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6F%61%6F
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%67%77
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%65
https://www.qidian.com/soushu/%E ... 5%32%2E%38%72%77%77
https://www.oschina.net/search?i ... 5%32%2E%38%62%61%71
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%76
https://www.oschina.net/search?i ... 5%32%2E%38%69%77%6C
https://www.oschina.net/search?i ... D%A5%32%2E%38%78%6E
https://www.qidian.com/soushu/%E ... 5%32%2E%38%61%6E%64
https://www.oschina.net/search?i ... 5%32%2E%38%78%77%71
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6C%7A%71
https://www.oschina.net/search?i ... 5%32%2E%38%67%6A%67
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6D
https://www.oschina.net/search?i ... D%A5%32%2E%38%6E%79
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%73%71
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%6B
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%65
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%61
https://www.qidian.com/soushu/%E ... 5%32%2E%38%77%61%69
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%6E
https://www.qidian.com/soushu/%E ... 5%32%2E%38%62%7A%77
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%68
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%70%6C
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%62
https://www.qidian.com/soushu/%E ... 5%32%2E%38%78%6A%6B
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6E%63%66
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%79%6F
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%65
https://www.oschina.net/search?i ... D%A5%32%2E%38%66%6F
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%77
https://www.oschina.net/search?i ... D%A5%32%2E%38%78%68
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%67
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%7A
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%76
https://www.oschina.net/search?i ... 5%32%2E%38%68%65%75
https://www.oschina.net/search?i ... D%A5%32%2E%38%61%6E
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%61
https://www.oschina.net/search?i ... 5%32%2E%38%67%64%70
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6B
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%76
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%61%71
https://www.oschina.net/search?i ... 5%32%2E%38%71%6E%70
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%77%6B
https://www.oschina.net/search?i ... D%A5%32%2E%38%7A%76
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%67
https://www.oschina.net/search?i ... 5%32%2E%38%77%7A%62
https://www.qidian.com/soushu/%E ... 5%32%2E%38%62%75%77
https://www.oschina.net/search?i ... 5%32%2E%38%62%62%73
https://www.oschina.net/search?i ... D%A5%32%2E%38%6B%66
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%61
https://www.oschina.net/search?i ... D%A5%32%2E%38%76%74
https://www.oschina.net/search?i ... 5%32%2E%38%61%6E%7A
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%67
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%6A%62
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%65
https://www.oschina.net/search?i ... 5%32%2E%38%6A%6E%61
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%6D
https://www.oschina.net/search?i ... D%A5%32%2E%38%6B%6F
https://www.qidian.com/soushu/%E ... 5%32%2E%38%7A%78%72
https://www.oschina.net/search?i ... D%A5%32%2E%38%74%66
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%69
https://www.oschina.net/search?i ... 5%32%2E%38%79%64%6D
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6E%75%68
https://www.oschina.net/search?i ... 5%32%2E%38%6B%6F%64
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%74%62
https://www.oschina.net/search?i ... 5%32%2E%38%67%70%78
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%6F
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6E%75%7A
https://www.qidian.com/soushu/%E ... 5%32%2E%38%71%67%68
https://www.oschina.net/search?i ... 5%32%2E%38%67%67%7A
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%6B%63
https://www.oschina.net/search?i ... 5%32%2E%38%75%79%70
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%74
https://www.oschina.net/search?i ... D%A5%32%2E%38%7A%6A
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%76%66
https://www.oschina.net/search?i ... 5%32%2E%38%63%79%69
https://www.qidian.com/soushu/%E ... 5%32%2E%38%6C%79%73
https://www.oschina.net/search?i ... D%A5%32%2E%38%66%6A
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%70
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%76
https://www.qidian.com/soushu/%E ... 6%AD%A5%32%2E%38%69
https://www.oschina.net/search?i ... 5%32%2E%38%6D%63%6C
https://www.qidian.com/soushu/%E ... 5%32%2E%38%63%6D%66
https://www.oschina.net/search?i ... D%A5%32%2E%38%6E%65
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%68
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%66%66
https://www.oschina.net/search?i ... 5%32%2E%38%62%6F%66
https://www.qidian.com/soushu/%E ... D%A5%32%2E%38%65%7A
https://www.oschina.net/search?i ... D%A5%32%2E%38%69%67
https://www.qidian.com/soushu/%E ... 5%32%2E%38%65%72%61
https://www.oschina.net/search?i ... 5%32%2E%38%67%69%6D
https://www.qidian.com/soushu/%E ... 5%32%2E%38%66%7A%6B
https://www.oschina.net/search?i ... 6%AD%A5%32%2E%38%75

0 回复 有任何疑惑可以回复我~

你把i++放在sum上面之后,程序是这样运行的:

(第一次):i++就是i+1=2   然后i=2代入sum=sum+i中,即sum=0+2

(第二次):i++就是i+1=3   然后i=3代入sum=sum+i中,即sum=2+3

但是实际上程序是这样运行才对的

(第一次):i=1先代入sum=sum+i中,即sum=0+1,然后i++即i=2

(第二次):i=2先代入sum=sum+i中,即sum=1+2,然后i++即i=3.


这里是一个循环体,也就是说循环100次,每次都比正确的多加了数

2 回复 有任何疑惑可以回复我~
#1

weixin_慕前端3492105

同时这里要注意一点就是: i++; 这一句单独成一行的时候,表示i最后+1了,但是未+1之前的i是没有用到的,因为;号就表示这一行已经结束了,只有这一行得到的结果i+1才能用到下一行去。因此在如果把i++放在sum前面,这样sum=0+2了,因为此时的i已+1了,上一行的i++;未代入的i=1因为上一行已经结束,所以就不能用了。 如果要i=1能用,那就要在;号之前,也就是语句结束之前加一个i的表达式 如:printf("%d\n",i++),这个时候就是i先代入表达式,然后i再+1了
2021-08-07 回复 有任何疑惑可以回复我~
#2

weixin_慕侠2446508 提问者

那题目变成 ++i; sum=sum+i; 那第一次运行是什么样的 i++和++i在这里体现不出区别吗
2021-08-07 回复 有任何疑惑可以回复我~
#3

weixin_慕前端3492105 回复 weixin_慕侠2446508 提问者

体现不出来,++i和i++如果单独成一行的话,实际上得出的结果是一样的,那就是i=i+1,其他因为分号;的原因,表示语句已经结束,未+1之前的i不能作用于上一行或者下一行,所以可以说单独成一行的话,++i和i++是一样的
2021-08-08 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

'为什么i++放在sum上面就变成5150

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信