for loop

what the difference between

#include<iostream>
using namespace std;
int main()
{
int count,sum=0;
for(count=1;count<=10;count++)
sum += count*count;
cout<<"sum s2:"<<sum;
return 0;

}
.....................

#include<iostream>
using namespace std;
int main()
{
int count,sum;
for(count=1;count<=10;count++)
sum += count*count;
cout<<"sum s2:"<<sum;
return 0;

}
you are intitialising sum to zero;
if you dont do this sum could in theory take any value on initialisation, which you do not want.

edit: by the way, your thread title has nothing to do with your actual question.
Last edited on
thank you
Topic archived. No new replies allowed.