Thread: Simple question about scope

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    Simple question about scope

    Hi all,

    I need to declare a variable that is only used in a do-while loop, and my question is where to best declare and initialise it. I have been reading that it is best to declare variables at the inner most scope, and therefore in the case of do loops it will be like this:
    Code:
        do {
    
            double t = 0; /* variable declared and initialised inside the loop */
    
            statements;
    
        } while (expression);
    Because this is a loop, does this mean that "t" will be declared and initialised at every cycle?, or will it be declared and initialised only at the first cycle of the do loop?

    Obviously I could use this:
    Code:
        double t = 0; /* variable declared and initialised outside the loop */
    
        do {
    
            statements;
    
        } while (expression);
    but I am just wondering about declaring/initialising variables inside loops.


    Thanks,
    Spiros

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Well, how about writing out some code and seeing what happens? In short, yes, i would be re-initialised to 0 every time through the loop, if it is inside of the loop body.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    thanks for your reply, I think it was late at night when I posted this and was too tired to think straight... I guess sometimes it happens!

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    68
    If you declare a variable inside of a loop, the compiler should throw an error, though it may only do it if the loop is going to go more than once. Sometimes you will need to initialize a variable to 0 several times throughout a loop. I have only done this a handful of times in the past.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by madmax2006 View Post
    If you declare a variable inside of a loop, the compiler should throw an error, though it may only do it if the loop is going to go more than once. Sometimes you will need to initialize a variable to 0 several times throughout a loop. I have only done this a handful of times in the past.
    Everything in your post is false.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by bithub View Post
    Everything in your post is false.
    Well, maybe not everything. I'd believe that last sentence:
    Quote Originally Posted by madmax2006 View Post
    I have only done this a handful of times in the past.
    But yeah, none of the rest.

    Declare and initialise it inside the loop if it is only needed within the loop, and its value is not intended to be the same on every iteration, and it isn't needing to appear inside the while part (which comes just after the closing brace). Otherwise, declare and initialise it outside/before the loop.
    Last edited by iMalc; 09-14-2010 at 01:06 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-02-2010, 12:19 PM
  2. Simple C programming question
    By Boxo in forum C Programming
    Replies: 3
    Last Post: 03-27-2010, 09:44 PM
  3. simple scope question
    By 7stud in forum C++ Programming
    Replies: 18
    Last Post: 01-30-2005, 07:45 AM
  4. A simple question about scope
    By dwylie in forum C Programming
    Replies: 3
    Last Post: 12-10-2004, 01:16 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM