bhupinder
bhupinder

Reputation: 335

Scope of variable

suppose

while(1)
{
    {

        {
            int a=10;

            a=5;
        }

    }
}

now as I cannot refer to "a" after its block's brace. I want to know when control moves up to first brace and then running down visited third brace again will "a" declared again or "a" hold value 5.

Upvotes: 0

Views: 112

Answers (7)

RichardPlunkett
RichardPlunkett

Reputation: 2988

Logically, the a cant be reference before its declaration, or after the close of the brace pair its declared in. It also gets destructed at the close, but thats a NOP for ints. Each time around the loop it will be re-initialized to 10.

But, you missed the fun part of the question, about what happens if the declaration doesnt set the value:

while(1)
{
        {
            int a;    // Here Scope Starts  

            a=5;
        }// Here Scope Ends
}

Here, a will be undefined before it is set the first time. But, it will also almost always be in the same place on the stack, so it might possibly retain value from one iteration to the next, if nothing else uses that space. But whether or not it does can be execution dependant, which makes this an exciting source of bugs.

Upvotes: 1

Roee Gavirel
Roee Gavirel

Reputation: 19445

a will be declared again.

usually you will define parameter as a in the loop if you do want it to reset each loop so you won't carry values from iteration to iteration.

while(1)
{
    {
        {
            //Define a new `a`
            int a=10;
            a=5;
        }
    }
}

But if you wish to use the same parameter and value for all the iterations, you need to define it out side the loop:

int a=0;
while(1)
{
    {
        {
            //`a` will count the number of iterations.
            a = a + 1;
        }
    }
}

Upvotes: 0

Anis_Stack
Anis_Stack

Reputation: 3442

each iteration of while loop , you will define new variable a and value of "a" change also but if you loop finish by any way you will lose this variable (you can't use it even in same function)!!

Upvotes: 0

Ned
Ned

Reputation: 957

The variable will logically be reallocated on the stack with each iteration -- in practice, the memory location will simply be reused. But note, in your trivial example, the value of "a" would never "visibly" hold the value of "5" when the loop returns to the declaration on the next iteration because it is explicitly initialized to "10" at the point it is declared. And if C# follows the same practice as Java, it would be implicitly reinitialized to a default value at that point if the compiler does not complain first that an uninitialized variable is being accessed if you do not initialize it yourself.

Upvotes: 0

Vinay Jain
Vinay Jain

Reputation: 2634

while(1)
{
    {

        {// Here Scope Starts
            int a=10;      

            a=5;
        }// Here Scope Ends

    }
}

Upvotes: 0

Lundin
Lundin

Reputation: 213358

a will only be declared when you declare it...

You declared it inside an inner scope, so any code referring to a will only be valid inside the same scope. Once you leave that scope, there exists nothing in your program called 'a' any longer. The memory that used to hold the value 5 may now be used by the rest of the program for other purposes.

Upvotes: 1

ForEveR
ForEveR

Reputation: 55887

A new a variable will be defined in each step of loop.

Upvotes: 2

Related Questions