For loop scope

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flyvin
    New Member
    • Oct 2008
    • 11

    For loop scope

    Is there anyway of getting around the for loop scope without making the variable global?
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    If you want a variable to be accesible outside of a for loop, then declare it outside of the for loop. It doesn't have to be global. It's local to the function that the for loop is in.

    Comment

    • flyvin
      New Member
      • Oct 2008
      • 11

      #3
      Oh, I thought I tried that once. oh well, thanks!

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by boxfish
        If you want a variable to be accesible outside of a for loop, then declare it outside of the for loop. It doesn't have to be global. It's local to the function that the for loop is in.
        In C89 you have no choice, the loop variable has to be declared outside the loop. C99 added an option to localize the declaration to just the loop. I don't know that capabilities C++ has in that regard.

        Comment

        Working...