Reputation: 49
When we create a loop and define variables within the loop, how are these treated by the system each time you go through an iteration. In other words, the first time through a loop, the computer creates the variable storage space for the declared variable, then when the it goes through the loop a second time it encounters the variable declaration again, but this has already been created, so is the declaration ignored for all subsequent loops?
Upvotes: 1
Views: 453
Reputation: 19545
When you have a loop which declares a variable, from the perspective of the compiled code, this variable will not be declared/allocated/created inside the loop when the code is executed. Variables will (technically) not be declared dynamically while the method is running, instead they are declared at the start of the method. Which local variables are needed for the method to work are decided by the compiler. See the following example code:
using System;
public class C {
public void M() {
Random r = new Random();
Console.WriteLine("test");
while (r.Next() > 0.25) {
int x = 1;
int y = 2;
x += 10;
x += y;
Console.WriteLine(x);
}
}
}
This can generate the following IL (see code on SharpLab.io):
.locals init (
[0] class [System.Private.CoreLib]System.Random r,
[1] int32 y
)
[...]
// loop start (head: IL_001f)
IL_0012: ldc.i4.1
IL_0013: ldc.i4.2
IL_0014: stloc.1
IL_0015: ldc.i4.s 10
IL_0017: add
IL_0018: ldloc.1
IL_0019: add
IL_001a: call void [System.Console]System.Console::WriteLine(int32)
The compiler created two local variables for this method, one for the variable r
, the other one for the variable y
. This is done at the beginning of the method, memory must be reserved at the beginning of the method, even when the while()
loop is not entered. Notice the stloc.1
instruction, which will save (in this case) the value 2
inside the local variable 1
, which is y
. Later it will load that value with ldloc.1
again from that variable 1
. Also notice that there is no local variable for x
because the compiler generated the code in such a way that it doesn't need it, it can work with values on the stack itself.
To answer your question: The variables inside a loop are not declared/allocated/created each time the loop is entered. Instead, when the compiler decides it needs local variables (for the whole method), it will do so at the beginning of the method in the .locals
block.
Upvotes: 0
Reputation: 2946
The comments under your question are correct that any variables you declare within your loop are "scoped" to the inside of your loop. This means as soon as your loop cycle ends, anything declared within the loop leaves scope, (will eventually be deleted), and the variable names are once again available to be used.
When the loop begins again, your local variables are re-initialised fresh, as it's a new scope again, your code in the loop has no idea that previous versions of that variable ever existed.
Intrestingly, this is slightly different for the variable declared in the opening of a for
loop.
for(int i =0; ...
This one actually works a bit differently.
If we look at the wonderful sharplab.io we can see what the compiler does to for
loops before it compiles the code.
A process known as "lowering" occurs where higher level features of the language are replaved by simpler features, and for loops are one of them!
If we take this code here
for(int i =0; i < 10; i++)
{
Console.WriteLine(i);
}
The compiler actially converts that to
int num = 0;
while (num < 10)
{
Console.WriteLine(num);
num++;
}
So this is the one variable that, while to you as a programmer it seems to be declared and scoped entierly within your loop, it actually is only declared once and will not be re-initialised each cycle.
Upvotes: 1