And to confirm André, this VB code:
Sub Main()
For i As Integer = 0 To 9
Dim j As Integer
j += 10
If j = 10 Then
Console.WriteLine("j is 10")
End If
Next
Dim jp As Integer
For i As Integer = 0 To 9
jp += 10
If jp = 10 Then
Console.WriteLine("j is 10")
End If
Next
End Sub
Gives this decompiled VB via ILSpy:
<stathread()>
Public Shared Sub Main()
Dim i As Integer = 0
Dim arg_2B_0 As Integer
Dim num As Integer
Do
Dim j As Integer
j += 10
Dim flag As Boolean = j = 10
If flag Then
Console.WriteLine("j is 10")
End If
i += 1
arg_2B_0 = i
num = 9
Loop While arg_2B_0 <= num
Dim k As Integer = 0
Dim arg_57_0 As Integer
Do
Dim jp As Integer
jp += 10
Dim flag As Boolean = jp = 10
If flag Then
Console.WriteLine("j is 10")
End If
k += 1
arg_57_0 = k
num = 9
Loop While arg_57_0 <= num
End Sub
I.e. both
Do...Loop While
blocks are identical.
Interestingly, the compiler decides that since the second
For...Next
loop uses the same identifier (
i
), it will use different ones, presumably since a
For...Loop
specifies a starting value (0 in this case), equivalent to
i = 0
, and it does that in preference to stuffing an
i = 0
line (that you didn't write) in there.
So, if you don't specify a new value for a variable, which a
For...Loop
does, then it will retain the prior value if you use it again - which is what André (MSDN) says, elephanting annoying folk they are, always elephanting right(ish).
The IL Code is:
.method public static
void Main () cil managed
{
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = (
01 00 00 00
)
// Method begins at RVA 0x2108
// Code size 91 (0x5b)
.maxstack 2
.entrypoint
.locals init (
[0] int32 jp,
[1] int32 i,
[2] int32 j,
[3] int32 i,
[4] bool VB$CG$t_bool$S0,
[5] int32 VB$CG$t_i4$S0
)
IL_0000: nop
IL_0001: ldc.i4.0
IL_0002: stloc.1
// loop start (head: IL_0003)
IL_0003: ldloc.2
IL_0004: ldc.i4.s 10
IL_0006: add.ovf
IL_0007: stloc.2
IL_0008: ldloc.2
IL_0009: ldc.i4.s 10
IL_000b: ceq
IL_000d: stloc.s VB$CG$t_bool$S0
IL_000f: ldloc.s VB$CG$t_bool$S0
IL_0011: brfalse.s IL_001e
IL_0013: ldstr "j is 10"
IL_0018: call void [mscorlib]System.Console::WriteLine(string)
IL_001d: nop
IL_001e: nop
IL_001f: nop
IL_0020: ldloc.1
IL_0021: ldc.i4.1
IL_0022: add.ovf
IL_0023: stloc.1
IL_0024: ldloc.1
IL_0025: ldc.i4.s 9
IL_0027: stloc.s VB$CG$t_i4$S0
IL_0029: ldloc.s VB$CG$t_i4$S0
IL_002b: ble.s IL_0003
// end loop
IL_002d: ldc.i4.0
IL_002e: stloc.3
// loop start (head: IL_002f)
IL_002f: ldloc.0
IL_0030: ldc.i4.s 10
IL_0032: add.ovf
IL_0033: stloc.0
IL_0034: ldloc.0
IL_0035: ldc.i4.s 10
IL_0037: ceq
IL_0039: stloc.s VB$CG$t_bool$S0
IL_003b: ldloc.s VB$CG$t_bool$S0
IL_003d: brfalse.s IL_004a
IL_003f: ldstr "j is 10"
IL_0044: call void [mscorlib]System.Console::WriteLine(string)
IL_0049: nop
IL_004a: nop
IL_004b: nop
IL_004c: ldloc.3
IL_004d: ldc.i4.1
IL_004e: add.ovf
IL_004f: stloc.3
IL_0050: ldloc.3
IL_0051: ldc.i4.s 9
IL_0053: stloc.s VB$CG$t_i4$S0
IL_0055: ldloc.s VB$CG$t_i4$S0
IL_0057: ble.s IL_002f
// end loop
IL_0059: nop
IL_005a: ret
} // end of method Module1::Main
M