c++ simple sum in for loop
i am using g++ (GCC) 4.6.0 and i am having trouble to producing the correct result. given the following simple for loop in c++
void sum(){
int sum;
for(int i=0,sum=0;i<=10;sum+=i,++i);
cout << sum << endl;
}
the output is giving 开发者_StackOverflowme 0. suppose within the for loop i added
cout << sum << endl;
it give me 0,1,3... until the very last line 0;
I think the reason I am getting 0 is because variable shadowing in the for loop? so I tried ::sum as in ::sum=0, and ::sum +=i. but it complains by the compiler. Also I tried
for(sum=0,int i=0;i<=10;sum+=i,++i);
the compiler also complains about not having primary expression in the first clause in the for loop
Really, you have to use the identifier sum
three times for three different things? :-S
Just write it readbly:
int sum = 0;
for(int i = 0; i <= 10; ++i) { sum += i; }
No more confusion, no more shadowing, no more uninitialized variables. Who are you trying to trick? Think about your replacement who will have to learn and understand your code!
Tip: Turn on compiler warnings!
PS: Before anyone talks about efficiency and starts counting CPU cycles: a) don't. b) hug your wife. c) compare the assembly of this code and your code.
int i=0,sum=0
is not the same as int i=0; sum=0;
. It is one statement that declares two variables.
This means that you are shadowing the outer sum
:
void sum() {
int sum; // <-- one `sum`
for (int i=0,sum=0;i<=10;sum+=i,++i) {} // <-- second `sum`
cout << sum << endl;
}
Only a declaration statement like that can be in the first clause of the for
preamble (think about whether sum=0,int i=0
would be valid elsewhere in your code), but you can workaround this issue by pulling out the "initialisation" to 0
entirely:
void sum() {
int sum = 0;
for (int i = 0; i <= 10; sum += i, ++i) {}
cout << sum << endl;
}
Or, so that it's actually legible:
void sum() {
int sum = 0;
for (int i = 0; i <= 10; i++) {
sum += i;
}
cout << sum << endl;
}
Change your code to:
void sum(){
int sum = 0;
for(int i=0;i<=10;sum+=i,++i); // all work is in for loop
cout << sum << endl;
}
Some (older) compilers wouldn't allow your original code, because you were defining a second sum
inside your for loop which was hiding the original one. So, it was accumulating correctly within the for loop, but the sum
defined outside the for loop was left untouched. For a little more detail:
int i = 0, sum = 0;
is the same as:
int i = 0;
int sum = 0;
You have two different sum
variables. One declared in the
int sum;
line, and another one declared in the initializing declaration of your for loop.
The first part of a for
statement is either a single declaration or an expression. In the latter case, the expression can consist of several assignments separated by comma operators, but you cannot mix and match declarations and expressions in one for
loop. Thus, int i=0, sum=0
will be parsed as one declaration that declares i
and sum
. So in your loop you increase the inner sum
, but after the loop only the outer one is visible, and that has still has whichever garbage value it started out with.
(Also, shame on you for putting the meat of the loop in the update expression instead of in the body!)
You are right. Solution is to do this this way:
void sum(){
int sum = 0;
for(int i=0;i<=10;sum+=i,++i);
cout << sum << endl;
}
Why did you put the ;
after for-loop? Because of this you have 10 iterations of loop without any output and finally you have your result - zero.
精彩评论