Click here to Skip to main content
15,395,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In this code the output is "0 1 18", but I can't understand why it's not "64 1 18". What does happen here?

C++
#include <iostream>
using namespace std;

int main ()
    {
        int sum = 0, i = 1, sum2=4;
            for(int sum = 10, i = 2; i <= 10; ++i)
            {
                //sum += i;
                sum = sum + i;
                cout << "i= " << i << " sum= " << sum << endl;
                sum2 = 18;

            }

        cout << sum << " " << i << " " << sum2;

        return 0;
    }


What I have tried:

I added a new variable called sum2 and checked whether it disappears after the FOR loop. But it doesn't.
Posted
Updated 21-Jun-22 18:17pm
Comments
Rick York 22-Jun-22 2:14am
   
I use Visual Studio and with my code I always use warning level 4 and it would flag this with a warning. I recommend trying that for yourself.

This is sometimes called "variable shadowing". This line
C++
for(int sum = 10, i = 2; i <= 10; ++i)
declares 2 new variables, sum and i which are only available to you within the bounds of the for loop. These are not the same variables as the ones declared before the for statement. Any access of variable with those names only access the variables declared in the for statement.
Consider:
C++
int sum = 0;
int i = 2;
for(int sum2 = 0, i2 = 2;  i2 < 10; ++i) {
   sum2 += i2;   // OK, sum2 is defined in this scope
   sum += i;     // Also OK, sum is defined outside the loop, so is i
}
// sum2 goes out of scope here.  If we try this:

std::cout << "sum2 = " << sum2 << '\n';  // compilation error sum2 is not defined in this scope 
   
Comments
CPallini 22-Jun-22 2:06am
   
5.
See if you get different result with:
C++
#include <iostream>
using namespace std;

int main ()
    {
        int sum = 0, i = 1, sum2=4;
            sum = 10;
            for(int i = 2; i <= 10; ++i)
            {
                //sum += i;
                sum = sum + i;
                cout << "i= " << i << " sum= " << sum << endl;
                sum2 = 18;

            }

        cout << sum << " " << i << " " << sum2;

        return 0;
    }

Scope of Variables in C++ - GeeksforGeeks[^]

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v3
Comments
CPallini 22-Jun-22 2:06am
   
5.
Patrice T 22-Jun-22 2:20am
   
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900