Page 1 of 1

enthuware.ocajp.i.v8.2.1327

Posted: Sun Jan 08, 2017 9:49 pm
by sulakshana
how can it print 0 0;
i = 0 and j = 0 are out of scope after for statement
int i, int j are local variables in main method since it cant get default values like instance variables
please explain............
thanx.

Re: enthuware.ocajp.i.v8.2.1327

Posted: Sun Jan 08, 2017 11:51 pm
by admin
i and j are defined before the for loop. So why do you think they will go out of scope after the for loop?

Re: enthuware.ocajp.i.v8.2.1327

Posted: Mon Jan 09, 2017 12:32 am
by sulakshana
Yes it is defined before for loop but values are assigned within for loop which will go out of scope when reaching second println statement. Since local variables are not assigned default 0 values how can it print 0 0s by using second println statement?

Re: enthuware.ocajp.i.v8.2.1327

Posted: Mon Jan 09, 2017 12:51 am
by admin
You seem to be confused between the scope of variable and assignment. If a variable is in scope, you can assign it a value, which is what the initialization section of the for loop is doing in this case. The value that you assign doesn't go out of scope. So you will see those values even after the for loop.

Re: enthuware.ocajp.i.v8.2.1327

Posted: Mon Jul 10, 2017 12:04 pm
by scyber
The answer out of scope would not appear if look carefully
for( j=0,i=0; i<j; ++j,i++)