Out of Scope?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Out of Scope?

Post by XianForce »

Well I was reading my Sams Teach Yourself C++ in an Hour a Day and one of the questions asked this:

What is the value of x when the for loop completes?

Code: Select all

for (int x = 0; x < 100; x++)
I thought it would be 100, because the first statement it defines and initializes the variable, so I would think it should be 100.

The answer in the book says: "The variable is out of scope and thus has no valid value"

I'm a little confused by this. Is this just meaning after the for loop completely ends? or the final value of x?

My best guess is the variable was created within the loop so once the loop ends, its out of scope, and the question is referring to after the loop ends.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Out of Scope?

Post by avansc »

yes, any variables inside loops are just local to that loop

so if you were to do

for(int a = 0;a< 100;a++)
{
}

int x = a;

you would get an error.

however

int a = 0;
for(a;a<100;a++)
{
}
int x = a;

that will work.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Out of Scope?

Post by XianForce »

avansc wrote:yes, any variables inside loops are just local to that loop

so if you were to do

for(int a = 0;a< 100;a++)
{
}

int x = a;

you would get an error.

however

int a = 0;
for(a;a<100;a++)
{
}
int x = a;

that will work.

Ok, so my last statement was right?
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Out of Scope?

Post by avansc »

yup
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Out of Scope?

Post by XianForce »

avansc wrote:yup
Alright thank you :mrgreen:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Out of Scope?

Post by MarauderIIC »

Just for fun...

Code: Select all

int a = 100;
{
    int a = 50;
    cout << a << endl;
}
cout << a << endl; //this a is not that a :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Out of Scope?

Post by XianForce »

MarauderIIC wrote:Just for fun...

Code: Select all

int a = 100;
{
    int a = 50;
    cout << a << endl;
}
cout << a << endl; //this a is not that a :)

Easy, there are 2 variables named a with this one. You declared a new a within the brackets so it would display 50, but after the brackets end, that a would go out of scope. Thus, a would still equal 100.
Post Reply