home comics writing pictures archive about

2018-06-17 - In IL: Print the Alphabet (do while, for)

In IL

Last time we looked at printing the alphabet using the while look. Today we are going to look at the do...while and for loops. First up we have the do...while loop which is vary similar to the while loop except that it checks the condition at the end of the loop instead of the beginning.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsDoWhile
{
class Program
{
static void Main(string[] args)
{
char ch = 'A';
do
{
Console.Write(ch);
ch++;
}
while (ch <= 'Z');
Console.WriteLine();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

Note that because the condition is checked at the end of the loop the ch will be printed at least once regardless of its value. Now let's look at the compiled program.

Main
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 25 (0x19)
.maxstack 2
.locals init ([0] char ch)
IL_0000: ldc.i4.s 65
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: call void [mscorlib]System.Console::Write(char)
IL_0009: ldloc.0
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: conv.u2
IL_000d: stloc.0
IL_000e: ldloc.0
IL_000f: ldc.i4.s 90
IL_0011: ble.s IL_0003
IL_0013: call void [mscorlib]System.Console::WriteLine()
IL_0018: ret
} // end of method Program::Main
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

This looks very similar to the while loop we had last time. The only difference is the lack of the branch to the condition check at the start of the loop which makes sense since the do while loop doesn't check the condition at the start of the loop.

Now let's look at the for loop version.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsFor
{
class Program
{
static void Main(string[] args)
{
for(char ch = 'A'; ch <= 'Z'; ch++)
{
Console.Write(ch);
}
Console.WriteLine();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

and the compiled code.

Main
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] char ch)
IL_0000: ldc.i4.s 65
IL_0002: stloc.0
IL_0003: br.s IL_0010
IL_0005: ldloc.0
IL_0006: call void [mscorlib]System.Console::Write(char)
IL_000b: ldloc.0
IL_000c: ldc.i4.1
IL_000d: add
IL_000e: conv.u2
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: ldc.i4.s 90
IL_0013: ble.s IL_0005
IL_0015: call void [mscorlib]System.Console::WriteLine()
IL_001a: ret
} // end of method Program::Main
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

This looks identical to the while loop version which makes sense given that they are doing the exact same thing. The main difference between the for loop and the while loop is where things are put. In the while case only the condition is a part of the loop syntax, the initialization and stepping of the variable are done separately. In the for loop case all of these actions are a part of the syntax of the loop. We'll see more about the implications of this later on.

Next time we will look at some Visual Basic .NET loops.

Comments: