• Unexpected result in simple program

    From Joseph Hesse@21:1/5 to All on Tue Nov 30 14:09:05 2021
    The following program produces an unexpected result. The variable x is
    only defined once with an initial value of 100 and is given a new value
    of 20 in the initialization part of the "for" loop. The "for" loop
    decrements x 5 times and the final value of x should be 15, not 100.

    Thank you,
    Joe

    =================================================================
    $ more Joe.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    for(int i = 0, x = 20; i < 5; ++i)
    {
    --x;
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }
    $ g++ Joe.cpp
    $ ./a.out
    x = 100

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Joseph Hesse on Tue Nov 30 08:12:39 2021
    Corrected version. I apologize to those who saw the original before I
    deleted it.

    On 11/30/21 7:09 AM, Joseph Hesse wrote:
    The following program produces an unexpected result. The variable x is
    only defined once with an initial value of 100 and is given a new value
    of 20 in the initialization part of the "for" loop. The "for" loop decrements x 5 times and the final value of x should be 15, not 100.

    Thank you,
    Joe

    =================================================================
    $ more Joe.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    for(int i = 0, x = 20; i < 5; ++i)
    {
    --x;
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }
    $ g++ Joe.cpp
    $ ./a.out
    x = 100

    I suspect that you don't realize that your code is equivalent to the
    following:

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    {
    int i, x;
    for(i = 0, x = 20; i < 5; ++i)
    {
    --x;
    }
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }

    The second declaration of 'x' has a scope that's restricted to the for-statement. It hides the 'x' declared in the outer scope. Therefore,
    the expressions x=20 and --x change that 'x', and have no effect on the
    first 'x'.
    There's two ways to fix this to make it do what you want:

    int i;
    for(i=0, x=20; ...

    or

    x = 20;
    for(int i=0; ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Joseph Hesse on Tue Nov 30 08:09:01 2021
    On 11/30/21 7:09 AM, Joseph Hesse wrote:
    The following program produces an unexpected result. The variable x is
    only defined once with an initial value of 100 and is given a new value
    of 20 in the initialization part of the "for" loop. The "for" loop decrements x 5 times and the final value of x should be 15, not 100.

    Thank you,
    Joe

    =================================================================
    $ more Joe.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    for(int i = 0, x = 20; i < 5; ++i)
    {
    --x;
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }
    $ g++ Joe.cpp
    $ ./a.out
    x = 100

    I suspect that you don't realize that your code is equivalent to the
    following:

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    int i, x;
    for(i = 0, x = 20; i < 5; ++i)
    {
    --x;
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }

    The second declaration of 'x' has a scope that's restricted to the for-statement. It hides the 'x' declared in the outer scope. Therefore,
    the expressions x=20 and --x change that 'x', and have no effect on the
    first 'x'.
    There's two ways to fix this to make it do what you want:

    int i;
    for(i=0, x=20; ...

    or

    x = 20;
    for(int i=0; ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Miguel_Gim=c3=a9nez?=@21:1/5 to All on Tue Nov 30 13:40:52 2021
    El 30/11/2021 a las 13:09, Joseph Hesse escribió:
    The following program produces an unexpected result.  The variable x is
    only defined once with an initial value of 100 and is given a new value
    of 20 in the initialization part of the "for" loop.  The "for" loop decrements x 5 times and the final value of x should be 15, not 100.

    Thank you,
    Joe

    =================================================================
    $ more Joe.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
      int x = 100;

      if(2 > 1)
      {
        for(int i = 0, x = 20; i < 5; ++i)
        {
          --x;
        }
        cout << "x = " << x << endl;  // I expect x to be 15, not 100
      }
    }
    $ g++ Joe.cpp
    $ ./a.out
    x = 100

    The x defined in the for loop shadows the other x while inside the loop.

    --
    Saludos
    Miguel Giménez

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Nikolaj Lazic@21:1/5 to All on Tue Nov 30 12:45:57 2021
    Dana Tue, 30 Nov 2021 14:09:05 +0200, Joseph Hesse <joeh@gmail.com> napis'o:
    The following program produces an unexpected result. The variable x is
    only defined once with an initial value of 100 and is given a new value
    of 20 in the initialization part of the "for" loop. The "for" loop decrements x 5 times and the final value of x should be 15, not 100.

    Thank you,
    Joe

    =================================================================
    $ more Joe.cpp
    #include <iostream>
    using namespace std;

    int main()
    {
    int x = 100;

    if(2 > 1)
    {
    for(int i = 0, x = 20; i < 5; ++i)

    This int makes fresh i and x for this scope.

    {
    --x;
    }
    cout << "x = " << x << endl; // I expect x to be 15, not 100
    }
    }
    $ g++ Joe.cpp
    $ ./a.out
    x = 100

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Joseph Hesse on Tue Nov 30 15:30:54 2021
    Joseph Hesse <joeh@gmail.com> writes:

    The following program produces an unexpected result.

    You've had an explanation, but to catch this sort of thing early, use
    more warnings. I use:

    $ g++ -std=c++11 -pedantic -fno-common -fno-diagnostics-show-option \
    -fsanitize=undefined -Wall -Wextra -Wformat-nonliteral -Wcast-align \
    -Winline -Wundef -Wcast-qual -Wshadow -Wconversion -ffloat-store -o j j.cc

    j.cc: In function ‘int main()’:
    j.cc:10:20: warning: declaration of ‘x’ shadows a previous local
    10 | for(int i = 0, x = 20; i < 5; ++i)
    | ^
    j.cc:6:7: note: shadowed declaration is here
    6 | int x = 100;
    | ^

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)