How does this work ?

int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)
sum += i;
std::cout << i << " " << sum << std::end;

Output is 100 and 45. How is that ?
You have two variable named i in your program. One of them only exists inside the for loop. It's probably more clear what's going on if I rename the loop variable.

1
2
3
4
int i = 100, sum = 0;
for (int j = 0; j != 10; ++j)
	sum += j;
std::cout << i << " " << sum << std::end;
Last edited on
First, code tags:
1
2
3
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i) sum += i;
std::cout << i << " " << sum << std::end;

Then some style change (and such):
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
int main() {
  int i = 100;
  int sum = 0;
  for ( int i = 0;
        i != 10;
        ++i )
  {
    sum += i;
  }
  std::cout << i << " " << sum << '\n';
  return 0;
}

The program has three nested scopes:
1. Global scope. std::cout is in global scope.
2. Scope of main(). The i and sum are declared in it.
3. Scope of if. The i is declared in it.

The i is declared twice? Indeed, on lines 3 and 5.

Variables declared within loop live only to the end of the loop. They do mask (make invisible) variables of outer scopes that have the same name.
Last edited on
No I want to understand it with i as I am reading about scope. Also why it only outputs two numbers. How is it working ?
Control structures have either one statement or a brace-block of statements. Your for loop has a single statement in its body: sum += i;
http://www.cplusplus.com/doc/tutorial/control/
Yeah but how the output is 100 and 45 ? Wouldn't it go through the loop and keep doing sum until the condition is falsified at which point it will exit the loop. So for example in first iteration i is 0 then sum will be 0 too so why isn't it in the output ?
So for example in first iteration i is 0 then sum will be 0 too so why isn't it in the output ?


It's a DIFFERENT i.

1
2
int i = 100, sum = 0;
for (int i = 0; i != 10; ++i)

See that i on the first line, the one you create (you create it there with the code int i) and set to 100? That's DIFFERENT to the i in the second line, the one you create (you create it there with the code int i) and set to zero. You created TWO different variables, holding TWO different values.

The second i exists ONLY inside the for loop. So here
std::cout << i << " " << sum << std::end;
we are OUTSIDE the for loop, so which i is that? Is it the one that exists ONLY inside the for loop? No, because we are not inside the for loop.

Is it the one you made on the first line? Yes. So what value does that have? 100.

The second i shadows the first i.

Read this:

https://en.wikipedia.org/wiki/Variable_shadowing
@Moschops, Yep I get that explanation thanks bro. But how about the value 45 that is printing as well ? Where does that come from ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {

	int i = 100;
	int sum = 0;

	for (int i = 0; i != 10; ++i) {
		sum += i;
	}

	std::cout << i << " " << sum << std::endl;

	return 0;
}


Line 5: Instatiating an integer i, initialized to 100.
Line 6: Instatiating an integer sum, initialized to 0.
Line 8: For loop - This integer i is different from the integer i instantiated on line 5. Unless you explicitly specify the use of the "more global" integer i, any usage of i within the scope of the for loop will apply to the integer instantiated on this line since it has more localized scope.
Line 9: With each iteration of the loop, the local integer i increases by 1. As this happens, the sum accumulates each of these values. Here are the values of sum for each iteration of the for loop:

0
1
3
6
10
15
21
28
36
45


When the for loop ends, sum will have a value of 45.

Line 12: First, you print integer i instantiated on line 5. This variable hasn't been changed, so it's value still is 100. Then, you print a space (" ") followed by sum.
Last edited on
@xismn, Ah, i get it now. Thanks bro. Just one question. What if I want to print out all what happened inside loop i.e 0 to 45. How can I do that ?
that's good
What if I want to print out all what happened inside loop i.e 0 to 45. How can I do that ?

Um... just print it out inside the loop. You already clearly know how to use std::cout to do that.
Write using namespace std; after include files so u dnt have to write std again anywhere in ur code!
Write using namespace std; after include files so u dnt have to write std again anywhere in ur code!

Don't.

The library is within a namespace for a good reason. You "advice" would undo that.

See, for example:
http://www.gotw.ca/gotw/053.htm
http://www.lonecpluspluscoder.com/2012/09/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
Topic archived. No new replies allowed.