Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Proper For Loops?

for loop loop

  • Please log in to reply
2 replies to this topic

#1 thieflock

thieflock

    CC Regular

  • Just Joined
  • PipPipPip
  • 29 posts

Posted 12 November 2007 - 06:28 AM

When declaring variables for a For Loop is it better to declare them at the top after the main method or only within the For Loop.

Thank you.
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 November 2007 - 09:17 AM

It depends on where else you will use them, and your coding style.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Guest_lamagra_*

Guest_lamagra_*
  • Guest

Posted 13 November 2007 - 12:04 PM

thieflock is right.

If you plan to use your variable in any part of your program you need to create it outside of the loop (unless you are going to pass it to methods within the loop). If it is only being used for a counter or temp storage then do it inside the loop.
  • 0





Also tagged with one or more of these keywords: for loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download