We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
syntax (Read 550 times)
syntax
Apr 19th, 2009, 10:38pm
 
Hello friends,I suddenly come across an once neglected problem:
If you define a variable in sequence like below
int a=1;
int a=2;
then the error message saying "duplicate" will appear.

But when I write in this way,say,in a loop:

for(int i=0;i<2;i++)
{
int g=1;//here no duplicate?
println(g);
//i+=1;
}

It works.It seems that g is defined the same way as in the former example,but no duplicate error pop up.What's the reason?Any advice is appreciated,thank you in advance.
Re: syntax
Reply #1 - Apr 20th, 2009, 12:34am
 
Well, based on what you wrote, you have two variables named 'a'. If it were to compile, there would be no way to tell which 'a' you were referring to.

There are cases where two variables with the same name can exist:

http://mindprod.com/jgloss/scope.html
Re: syntax
Reply #2 - Apr 20th, 2009, 2:36am
 
If you have specified
int g = 0;
outside the for loop then it is true that you now have 2 variables called g and it will compile and run.

The 2 vaiables can store different values but inside the loop it will use the g variable declared inside the loop and when outside the loop it will use the one declared outside the loop.

Care should be used when using variables with the same name because it can cause confusion when programming about which one is being used.
Re: syntax
Reply #3 - Apr 20th, 2009, 5:34am
 
I usually define the variable once, the set it whenever I want.  So you could define it outside the loop and then set it inside.
Code:

int a = 0;
a = 1;

for .....{
a = 3;
}
Re: syntax
Reply #4 - Apr 20th, 2009, 5:53pm
 
JScriptChan wrote on Apr 19th, 2009, 10:38pm:
Hello friends,I suddenly come across an once neglected problem:
If you define a variable in sequence like below
int a=1;
int a=2;
then the error message saying "duplicate" will appear.

But when I write in this way,say,in a loop:

for(int i=0;i<2;i++)
{
int g=1;//here no duplicate
println(g);
//i+=1;
}

It works.It seems that g is defined the same way as in the former example,but no duplicate error pop up.What's the reason?Any advice is appreciated,thank you in advance.


The compiler checks syntax, if it were to actually run the code, you may get the duplicate error, but because it is inside the for loop, the syntax looks correct to the compiler (ie. the compiler doesn't run the loop). However, as mentioned, the value of g will be 1 and not available outside the for loop. So if you were to use it outside the loop,

for(int i=0;i<2;i++)
{
int g=1;//here no duplicate
println(g);
//i+=1;
}
if(g)
{}

you will get a compile error, so to prevent that error you will need to declare it outside the loop,

int g;
for(int i=0;i<2;i++)
{
int g=1;//here no duplicate
println(g);
//i+=1;
}
if(g)
{}

Now you will get your duplicate error, and to correct it,
Edit
Depending on the compiler, you may not get the duplicate error as the int g outside the loop will be a different variable than that inside the loop, but this is good for visualization

int g;
for(int i=0;i<2;i++)
{
g=1;
println(g);
//i+=1;
}
if(g)
{}
Re: syntax
Reply #5 - Apr 22nd, 2009, 3:26am
 
Thank you!Did you mean that in the for loop I can set two or more variables with the same name?I guess so,thank you for your reply!
Re: syntax
Reply #6 - Apr 22nd, 2009, 4:10am
 
JScriptChan wrote on Apr 22nd, 2009, 3:26am:
Thank you!Did you mean that in the for loop I can set two or more variables with the same name?I guess so,thank you for your reply!


Not necessarily inside just a for loop - it's any time you add a scope, or a set of {}.

This might help a little bit http://www.noobscorner.jassper.com/frm_scope.htm

It is a short explanation for a different language, nwscript, but the concept is the same.

After reading that, basically you can do this,

Code:

void draw()
{ // << Main scope
int i;
int x = 10;
for(i=0; i < x; i++)
{ // << New scope
int x = 1;
}
}


The loop will still run 10 times because the int x inside the scope for the loop is a different variable than the x in the main scope.

However - no matter what your reasoning is, I strongly recommend that you do not do this! Always name variables differently - it will save your hair on your head in the long run. There is never a case where different variables need to have the same name and even if there is, it would be extremely rare.

     
Re: syntax
Reply #7 - Apr 24th, 2009, 3:18am
 
Thank you so much! And I'd better take your advice to save my hair,really Smiley
Page Index Toggle Pages: 1