using namespace std;
Int main ()
{int sum;
for(int i=1; i<=100; I++)
{
Sum=0;
Sum=sum + I;
}
Cout & lt;
}
On computers and mobile phones to compile results are 100
I don't know what is going wrong?
CodePudding user response:
Has been solved, the sum=0 should be put in circulation outsideCodePudding user response:
Yes, that's the sum=0 should be written in the book circulation in vitro, otherwise each loop is executed sum=0, before calculation was done not have, only the last 100 will add to, so the result is 100, but when the program is more complex, it is easy to appear this problem, is the variable initialization in the right position, this requires our logical thinking to be clear, can see this article, https://blog.csdn.net/weixin_43956598/article/details/90049508CodePudding user response: