The last example we will cover in this section is the for loop. This statement is more complicated than if statements and even more complicated than while statement for loops. However, it's more widely used in C# and understanding it will help you understand other complicated statements in IL language. The C# code looks like this:
for (i = 0; i < 50; i++)
{//
X = i + 20;
}
The equivalent IL code will look like this:
00: ldc.i4.0 //pushes a constant with value 0
01: stloc.0 //stores it in local variable 0 (i). This represents i = 0
02: br 11 //unconditional branching to line 11
03: ldloc.0 //loads variable 0 (i) into stack
04: ldc.i4.s 20 //loads an int32 constant with value 20 into stack
05: add //adds both values from the stack and pushes the result back to stack (i + 20)
06: stloc.1 //stores the result in local variable 1 (X)
07: ldloc.0 //loads local variable 0 (i)
08: ldc.i4.1 //pushes a constant value of 1
09: add //adds both values
10: stloc.0 //stores the...