hey guys, doing some exercises after a section on scopes.
the question was
Is the following program legal? if so, what values are printed?
Code:
int i = 100, sum = 0;
for (int = 0; i != 0; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
so i fired up emacs and put it in...
Code:
#include <iostream>
int main()
{
int i = 100, sum = 0;
for (int i = 0; i <= 10; ++i)
sum += i;
std::cout << i << " " << sum << std::endl;
return 0;
}
now i anticipated "int i = 0" to remove 100 and put 0.
Then i expected it to print
1 1
2 3
3 6
...
10 55 but i got 100 55. Now as im typing this i am facepalming, because i think the i=0 is only defined in the for loop, and the std::cout line is calling the block? scope variable?
is that right? man this stuff is tricky lol