home comics writing pictures archive about

2018-09-01 - In IL: Print the Alphabet (Do Until)

In IL

In the last few parts we've been looking at C# loops, today we're going to look at a couple Visual Basic .NET loops. VB has a lot of the same loops as C#, with some syntax differences, but the Do Until and the Do ... Until loops are VB only. The Do Until loop is similar to the while loop except that instead of looping while the condition is true it loops until the condition is true, hence the Until keyword.

Module1.vb
Module Module1
Sub Main()
Dim ch As Char = "A"c
Do Until ch > "Z"c
Console.Write(ch)
ch = Chr(Asc(ch) + 1)
Loop
Console.WriteLine()
End Sub
End Module
1
2
3
4
5
6
7
8
9
10
11
12

One thing you might notice about this code is the strange way the character is incremented. This is because Visual Basic .NET has different rules about implicitly converting characters to integers which requires us to explicitly convert the value to an integer, increment it, and then convert it back to being a character. Now let's compile it and see how that looks.

Main
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 36 (0x24)
.maxstack 2
.locals init ([0] char ch)
IL_0000: ldc.i4.s 65
IL_0002: stloc.0
IL_0003: br.s IL_0019
IL_0005: ldloc.0
IL_0006: call void [mscorlib]System.Console::Write(char)
IL_000b: ldloc.0
IL_000c: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::Asc(char)
IL_0011: ldc.i4.1
IL_0012: add.ovf
IL_0013: call char [Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::Chr(int32)
IL_0018: stloc.0
IL_0019: ldloc.0
IL_001a: ldc.i4.s 90
IL_001c: ble.s IL_0005
IL_001e: call void [mscorlib]System.Console::WriteLine()
IL_0023: ret
} // end of method Module1::Main
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

Except for the different instructions to increment the character this looks almost identical to the while loop version we saw previously.

Now let's look at the other version of the Do Until loop which puts the Until after the loop similar to the do ... while loop.

Module1.vb
Module Module1
Sub Main()
Dim ch As Char = "A"c
Do
Console.Write(ch)
ch = Chr(Asc(ch) + 1)
Loop Until ch > "Z"c
Console.WriteLine()
End Sub
End Module
1
2
3
4
5
6
7
8
9
10
11
12

and if we compile that we get.

Main
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 34 (0x22)
.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: call int32 [Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::Asc(char)
IL_000f: ldc.i4.1
IL_0010: add.ovf
IL_0011: call char [Microsoft.VisualBasic]Microsoft.VisualBasic.Strings::Chr(int32)
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: ldc.i4.s 90
IL_001a: ble.s IL_0003
IL_001c: call void [mscorlib]System.Console::WriteLine()
IL_0021: ret
} // end of method Module1::Main
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

Again that looks very much like the do ... while loop we saw last time. The language gives you two ways of doing something but the compiler converts them into a single way by reversing your logic. The Until syntax is simply a tool for you to better describe your program's intentions using words.

Next time we will look at continue and break statements.

Comments: