Using a variable in variable in various scopes
Dear All,
I have a for loop in which I use a variable as a counter. Is it good practice to use same variable in other loop therewith not having to use other variable?
thank you
[184 byte] By [
lontana] at [2007-11-20 11:53:04]

# 1 Re: Using a variable in variable in various scopes
If you mean like this:
for (int i = 0; i<10; i++)
{
//Do stuff...
}
for (i = 0; i<10; i++)
{
//Do something else
}
Then this is not allowed as by the C++ ISO standard. The int variable 'i' has its scope inside the first for loop.
Is it good practice to use same variable in other loop therewith not having to use other variable?No. It is simply not allowed.
Laitinen
# 2 Re: Using a variable in variable in various scopes
Maybe he means,
for (int i = 0; i<10; i++)
{
//Do stuff...
}
for (int i = 0; i<10; i++)
{
//Do something else
}
or
int i;
for (i = 0; i<10; i++)
{
//Do stuff...
}
for (i = 0; i<10; i++)
{
//Do something else
}
Personally, I prefer the first, although either will work.
GCDEF at 2007-11-11 4:03:27 >
