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.
enthuware.ocajp.i.v8.2.1327
Moderator: admin
-
- Posts: 14
- Joined: Thu Dec 22, 2016 8:55 pm
- Contact:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocajp.i.v8.2.1327
i and j are defined before the for loop. So why do you think they will go out of scope after the for loop?
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu Dec 22, 2016 8:55 pm
- Contact:
Re: enthuware.ocajp.i.v8.2.1327
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: enthuware.ocajp.i.v8.2.1327
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Jul 08, 2017 4:42 am
- Contact:
Re: enthuware.ocajp.i.v8.2.1327
The answer out of scope would not appear if look carefully
for( j=0,i=0; i<j; ++j,i++)
for( j=0,i=0; i<j; ++j,i++)
Who is online
Users browsing this forum: rwilliams93 and 98 guests