Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable reassignment

Status
Not open for further replies.

osuman

Technical User
Nov 22, 2000
281
US
If I declare a variable, such as an int and set it equal to an initial value, will it get reset if it is in a loop to it's initial value on each iteration. For example, consider the following code:
Code:
for(int i=0; i<whatever; i++) {
int j = 0;

some other code that changes the value of j...
}
Now if I want j to get set to 0 each time the loop iterates, will it do so with the above example? Obviously, a simple solution would just be to split up the declaration and instantiation into seperate lines. But I was curious what the deal is.

Thanks.

 
Yes, it will create a new variable each time and initialise it to 0.

Durkin
 
I don't think you create a new variable each time. Only the initialisation will happen each time. ??
 
I didn't think it would create a new variable each time. That seems like it would use a lot of unnecessary space. I figured if it had already been initialized it wouldn't do it again on successive loop iterations. This led me to wonder if it would set it equal to 0 or skip the statement entirely. Does anyone know for sure?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top