2018-03-25 - In IL: Print the Alphabet (while)
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
The while loop is the simplest form of loop. It simply repeats the specified operations as long as the specified condition is true. To show off the while loop we are going to use a program that prints out the letters of the alphabet.
As you can see this is a very simple program. We start at the letter A and then print and increment it until it's the letter Z. This works because characters are actually just numbers and in the encoding used by C# all capital letters are continuous and in order.
Now let's compile this program and see what we get.
As you can see there's no special looping instructions. Instead we just get a couple of branching instructions. We start by initializing our variable to 65 ('A') and then we jump to a point near the end of the function. There we find a second branch that tests if the local variable is less than or equal to 90 ('Z') and jumps backwards in the function if it is. There we find the innards of our loop where we print out and increment the local variable. Then we find ourselves at the second branch point again and perform the test again. If it's true we repeat or loop again and if it's false we continue on and the function ends.
Next time we will look at other kinds of loops.
Comments: