CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    May 2000
    Location
    NJ
    Posts
    640

    Simple Question about a For Loop

    If I have the following:


    void SomeFunction()
    {

    for(int i=0; i<1000; i++)
    {
    // Do something
    }

    }


    My question is the variable i redeclared as integer for each successive loop?

    Is it best to declare i first for stack purposes..

    void SomeFunction()
    {
    int i;

    for( i=0; i<1000; i++)
    {
    // Do something
    }

    }

    I just thinking about better code execution.

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658
    If it were to be declared each successive loop, then your loop would never end cuz the value would always be reset

    it's kinda preference, really. If it's a small function, I'll often times declare it in the for loop. For larger functions, though, where I (may) have more than one loop and would want to re-use the i variable, I declare it at the top of the function with all my other variables
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    Dec 2003
    Location
    Republic of Ireland
    Posts
    383
    No it is not redeclared. Only diference that comes to my mind is that (1) will be visible only in the scope of the loop (in VS.NET) in VC6 it will be visible in and after the loop.

  4. #4
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by Mr. Tomaszek
    Only diference that comes to my mind is that (1) will be visible only in the scope of the loop (in VS.NET) in VC6 it will be visible in and after the loop.
    This is not true for the version of VC++.Net that I have (7.1).

    Code:
    for (int i=0; i<10; i++) {
    }
    
    for (i=0; i<10; i++) {
    }
    This compiles fine.

    I believe this is in violation of the ANSI spec though.

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    //NOT RELATED TO THE ORIGINAL QUESTION

    It is definitely an annoyance

    just limit the scope

    Code:
    {
         for (int i=0; i<10; i++) 
        {
        }
    }
    
    {
         for (int i=0; i<10; i++) 
        {
        }
    }
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Originally posted by wayside
    This is not true for the version of VC++.Net that I have (7.1).

    Code:
    for (int i=0; i<10; i++) {
    }
    
    for (i=0; i<10; i++) {
    }
    This compiles fine.

    I believe this is in violation of the ANSI spec though.
    That would be expected since the inner loop is in the scope of the outer. Where VC deviates from the spec is i is still visible following the outer loop (in your example). It should only exist in the scope of the for loop in which it's declared.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Originally posted by GCDEF
    That would be expected since the inner loop is in the scope of the outer. Where VC deviates from the spec is i is still visible following the outer loop (in your example). It should only exist in the scope of the for loop in which it's declared.
    There are no inner or outer loops in this example. If it were, you'd be in trouble modifying loop counter of the outer loop in the inner loop.
    ANSI states that the scope of loop variable (declared in the for() statement) is a body of that loop. MS maintains non-standard implementation for backward compatibility with the software already written in non-standard way. There is a compiler switch that makes current Visual C++ compilers compliant with ANSI.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Ah.. didn't know there was a switch. good info
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    It would be /Za (or disable lanquage extensions in the project settings)

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Right.
    Or, you could specify it like that (MSDN):
    /Zc:forScope (Force Conformance in for Loop Scope)

    Use /Zc:forScope if you want standard C++ behavior for for loops with Microsoft extensions (/Ze).
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Originally posted by VladimirF
    There are no inner or outer loops in this example. If it were, you'd be in trouble modifying loop counter of the outer loop in the inner loop.
    ANSI states that the scope of loop variable (declared in the for() statement) is a body of that loop. MS maintains non-standard implementation for backward compatibility with the software already written in non-standard way. There is a compiler switch that makes current Visual C++ compilers compliant with ANSI.
    You're right, I misread the brackets. I always find that style of coding very hard to read.

  12. #12
    Join Date
    Mar 2004
    Posts
    9
    The fact that non ISO for loop scoping is the default is disappointing. It will encourage people to continue to write non-standard code.

    There is a workaround to the for scope problem for VC6 - it limits the scope of variables declared in the for statement to the scope of the loop:

    #define for if (0) {} else for

    Adrian

  13. #13
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by GCDEF
    You're right, I misread the brackets. I always find that style of coding very hard to read.
    Hehe, I agree with you, but my company's coding standards specify this bracket style, so I've gotten used to it.

  14. #14
    Join Date
    Apr 2004
    Location
    India
    Posts
    70

    Smile

    IT IS BETTER TO DECLARE THE VARIABLE INITIALLY.

  15. #15
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Originally posted by S.DARWIN
    IT IS BETTER TO DECLARE THE VARIABLE INITIALLY.
    WHY???
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured